Repository: Allow for some more fine-grained control

Filter columns, search columns and sort rules can now be returned
dependent on a query's current target
This commit is contained in:
Johannes Meyer 2016-08-18 13:35:52 +02:00
parent c899456d93
commit ccb8bcc6e3
2 changed files with 43 additions and 20 deletions

View File

@ -323,25 +323,30 @@ abstract class Repository implements Selectable
* *
* Calls $this->initializeBlacklistedQueryColumns() in case $this->blacklistedQueryColumns is null. * Calls $this->initializeBlacklistedQueryColumns() in case $this->blacklistedQueryColumns is null.
* *
* @param string $table
*
* @return array * @return array
*/ */
public function getBlacklistedQueryColumns() public function getBlacklistedQueryColumns($table = null)
{ {
if ($this->blacklistedQueryColumns === null) { if ($this->blacklistedQueryColumns === null) {
$this->blacklistedQueryColumns = $this->initializeBlacklistedQueryColumns(); $this->blacklistedQueryColumns = $this->initializeBlacklistedQueryColumns($table);
} }
return $this->blacklistedQueryColumns; return $this->blacklistedQueryColumns;
} }
/** /**
* Overwrite this in your repository implementation in case you * Overwrite this in your repository implementation in case you need to initialize the
* need to initialize the blacklisted query columns lazily * blacklisted query columns lazily or dependent on a query's current base table
*
* @param string $table
* *
* @return array * @return array
*/ */
protected function initializeBlacklistedQueryColumns() protected function initializeBlacklistedQueryColumns()
{ {
// $table is not part of the signature due to PHP strict standards
return array(); return array();
} }
@ -350,24 +355,30 @@ abstract class Repository implements Selectable
* *
* Calls $this->initializeFilterColumns() in case $this->filterColumns is null. * Calls $this->initializeFilterColumns() in case $this->filterColumns is null.
* *
* @param string $table
*
* @return array * @return array
*/ */
public function getFilterColumns() public function getFilterColumns($table = null)
{ {
if ($this->filterColumns === null) { if ($this->filterColumns === null) {
$this->filterColumns = $this->initializeFilterColumns(); $this->filterColumns = $this->initializeFilterColumns($table);
} }
return $this->filterColumns; return $this->filterColumns;
} }
/** /**
* Overwrite this in your repository implementation in case you need to initialize the filter columns lazily * Overwrite this in your repository implementation in case you need to initialize
* the filter columns lazily or dependent on a query's current base table
*
* @param string $table
* *
* @return array * @return array
*/ */
protected function initializeFilterColumns() protected function initializeFilterColumns()
{ {
// $table is not part of the signature due to PHP strict standards
return array(); return array();
} }
@ -376,24 +387,30 @@ abstract class Repository implements Selectable
* *
* Calls $this->initializeSearchColumns() in case $this->searchColumns is null. * Calls $this->initializeSearchColumns() in case $this->searchColumns is null.
* *
* @param string $table
*
* @return array * @return array
*/ */
public function getSearchColumns() public function getSearchColumns($table = null)
{ {
if ($this->searchColumns === null) { if ($this->searchColumns === null) {
$this->searchColumns = $this->initializeSearchColumns(); $this->searchColumns = $this->initializeSearchColumns($table);
} }
return $this->searchColumns; return $this->searchColumns;
} }
/** /**
* Overwrite this in your repository implementation in case you need to initialize the search columns lazily * Overwrite this in your repository implementation in case you need to initialize
* the search columns lazily or dependent on a query's current base table
*
* @param string $table
* *
* @return array * @return array
*/ */
protected function initializeSearchColumns() protected function initializeSearchColumns()
{ {
// $table is not part of the signature due to PHP strict standards
return array(); return array();
} }
@ -402,24 +419,30 @@ abstract class Repository implements Selectable
* *
* Calls $this->initializeSortRules() in case $this->sortRules is null. * Calls $this->initializeSortRules() in case $this->sortRules is null.
* *
* @param string $table
*
* @return array * @return array
*/ */
public function getSortRules() public function getSortRules($table = null)
{ {
if ($this->sortRules === null) { if ($this->sortRules === null) {
$this->sortRules = $this->initializeSortRules(); $this->sortRules = $this->initializeSortRules($table);
} }
return $this->sortRules; return $this->sortRules;
} }
/** /**
* Overwrite this in your repository implementation in case you need to initialize the sort rules lazily * Overwrite this in your repository implementation in case you need to initialize
* the sort rules lazily or dependent on a query's current base table
*
* @param string $table
* *
* @return array * @return array
*/ */
protected function initializeSortRules() protected function initializeSortRules()
{ {
// $table is not part of the signature due to PHP strict standards
return array(); return array();
} }
@ -900,7 +923,7 @@ abstract class Repository implements Selectable
throw new ProgrammingError('Table name "%s" not found', $table); throw new ProgrammingError('Table name "%s" not found', $table);
} }
$blacklist = $this->getBlacklistedQueryColumns(); $blacklist = $this->getBlacklistedQueryColumns($table);
$columns = array(); $columns = array();
foreach ($queryColumns[$table] as $alias => $column) { foreach ($queryColumns[$table] as $alias => $column) {
$name = is_string($alias) ? $alias : $column; $name = is_string($alias) ? $alias : $column;
@ -994,7 +1017,7 @@ abstract class Repository implements Selectable
return false; return false;
} }
return !in_array($alias, $this->getBlacklistedQueryColumns()) return !in_array($alias, $this->getBlacklistedQueryColumns($table))
&& $this->validateQueryColumnAssociation($table, $name); && $this->validateQueryColumnAssociation($table, $name);
} }
@ -1019,7 +1042,7 @@ abstract class Repository implements Selectable
throw new QueryException(t('Query column "%s" not found'), $name); throw new QueryException(t('Query column "%s" not found'), $name);
} }
if (in_array($alias, $this->getBlacklistedQueryColumns())) { if (in_array($alias, $this->getBlacklistedQueryColumns($table))) {
throw new QueryException(t('Column "%s" cannot be queried'), $name); throw new QueryException(t('Column "%s" cannot be queried'), $name);
} }
@ -1107,7 +1130,7 @@ abstract class Repository implements Selectable
throw new StatementException('Statement column "%s" not found', $name); throw new StatementException('Statement column "%s" not found', $name);
} }
if (in_array($alias, $this->getBlacklistedQueryColumns())) { if (in_array($alias, $this->getBlacklistedQueryColumns($table))) {
throw new StatementException('Column "%s" cannot be referenced in a statement', $name); throw new StatementException('Column "%s" cannot be referenced in a statement', $name);
} }

View File

@ -204,7 +204,7 @@ class RepositoryQuery implements QueryInterface, SortRules, FilterColumns, Itera
*/ */
public function getFilterColumns() public function getFilterColumns()
{ {
return $this->repository->getFilterColumns(); return $this->repository->getFilterColumns($this->target);
} }
/** /**
@ -214,7 +214,7 @@ class RepositoryQuery implements QueryInterface, SortRules, FilterColumns, Itera
*/ */
public function getSearchColumns() public function getSearchColumns()
{ {
return $this->repository->getSearchColumns(); return $this->repository->getSearchColumns($this->target);
} }
/** /**
@ -294,7 +294,7 @@ class RepositoryQuery implements QueryInterface, SortRules, FilterColumns, Itera
*/ */
public function getSortRules() public function getSortRules()
{ {
return $this->repository->getSortRules(); return $this->repository->getSortRules($this->target);
} }
/** /**