Skip to content

Commit

Permalink
[update] introduce BC way for intuitive filtering of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehristov committed Aug 18, 2020
1 parent 9c0472a commit d6307b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,21 @@ public function isDirty(string $field): bool
*
* @return Field[]
*/
public function getFields($filters = null): array
public function getFields($filters = null, bool $onlyFields = null): array
{
if ($filters === null) {
return $this->fields;
return $onlyFields ? $this->getFields(self::FIELD_FILTER_ONLY_FIELDS) : $this->fields;
} elseif (!is_array($filters)) {
$filters = [$filters];
}

return array_filter($this->fields, function (Field $field, $name) use ($filters) {
$onlyFields = $onlyFields ?? true;

return array_filter($this->fields, function (Field $field, $name) use ($filters, $onlyFields) {
if ($onlyFields && !$this->isOnlyFieldsField($field->short_name)) {
return false;
}

foreach ($filters as $filter) {
if ($this->fieldMatchesFilter($field, $filter)) {
return true;
Expand All @@ -648,13 +654,13 @@ protected function fieldMatchesFilter(Field $field, string $filter): bool
{
switch ($filter) {
case self::FIELD_FILTER_SYSTEM:
return $this->isOnlyFieldsField($field->short_name) && $field->system;
return $field->system;
case self::FIELD_FILTER_NOT_SYSTEM:
return $this->isOnlyFieldsField($field->short_name) && !$field->system;
return !$field->system;
case self::FIELD_FILTER_EDITABLE:
return $this->isOnlyFieldsField($field->short_name) && $field->isEditable();
return $field->isEditable();
case self::FIELD_FILTER_VISIBLE:
return $this->isOnlyFieldsField($field->short_name) && $field->isVisible();
return $field->isVisible();
case self::FIELD_FILTER_ONLY_FIELDS:
return $this->isOnlyFieldsField($field->short_name);
case self::FIELD_FILTER_PERSIST:
Expand Down Expand Up @@ -1649,7 +1655,7 @@ public function export(array $fields = null, $key_field = null, $typecast_data =

// prepare array with field names
if ($fields === null) {
$fields = array_keys($this->getFields(self::FIELD_FILTER_PERSIST));
$fields = array_keys($this->getFields(self::FIELD_FILTER_PERSIST, false));
}

// add key_field to array if it's not there
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function initSelect($fields = null): void
}

if (!is_array($fields)) {
$fields = array_keys($this->model->getFields(Model::FIELD_FILTER_PERSIST));
$fields = array_keys($this->model->getFields(Model::FIELD_FILTER_PERSIST, false));
}

foreach ($fields as $fieldName) {
Expand Down

0 comments on commit d6307b3

Please sign in to comment.