Skip to content

Commit

Permalink
[update] move to onlyField group of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehristov committed Aug 5, 2020
1 parent 35e1f92 commit 945d6b9
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,22 @@ public function allFields()
return $this;
}

private function checkOnlyFieldsField(string $field)
private function checkOnlyFieldsField(string $fieldName)
{
$this->getField($field); // test if field exists
$field = $this->getField($fieldName); // test if field exists

if ($this->only_fields) {
if (!in_array($field, $this->only_fields, true) && !$this->getField($field)->system) {
throw (new Exception('Attempt to use field outside of those set by onlyFields'))
->addMoreInfo('field', $field)
->addMoreInfo('only_fields', $this->only_fields);
}
if (!$this->isOnlyFieldsField($fieldName) && !$field->system) {
throw (new Exception('Attempt to use field outside of those set by onlyFields'))
->addMoreInfo('field', $fieldName)
->addMoreInfo('only_fields', $this->only_fields);
}
}

public function isOnlyFieldsField(string $fieldName)
{
return !$this->only_fields || in_array($fieldName, $this->only_fields, true);
}

/**
* Will return true if specified field is dirty.
*/
Expand Down Expand Up @@ -619,24 +622,24 @@ public function getFields($filter = null): array

$filterPresets = [
'system' => function (Field $field) {
return $this->isOnlyField($field->short_name) && $field->system;
return $this->isOnlyFieldsField($field->short_name) && $field->system;
},
'not system' => function (Field $field) {
return $this->isOnlyField($field->short_name) && !$field->system;
return $this->isOnlyFieldsField($field->short_name) && !$field->system;
},
'editable' => function (Field $field) {
return $this->isOnlyField($field->short_name) && $field->isEditable();
return $this->isOnlyFieldsField($field->short_name) && $field->isEditable();
},
'visible' => function (Field $field) {
return $this->isOnlyField($field->short_name) && $field->isVisible();
return $this->isOnlyFieldsField($field->short_name) && $field->isVisible();
},
'persisting' => function (Field $field) {
if ($field->never_persist) {
return false;
}

if (!$field->system) {
return $this->isOnlyField($field->short_name);
return $this->isOnlyFieldsField($field->short_name);
}

return true;
Expand All @@ -663,11 +666,6 @@ public function getFields($filter = null): array
}, ARRAY_FILTER_USE_BOTH);
}

public function isOnlyField($fieldName)
{
return !$this->only_fields || in_array($fieldName, $this->only_fields, true);
}

/**
* Set field value.
*
Expand Down

0 comments on commit 945d6b9

Please sign in to comment.