Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Model::getFields #691

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

georgehristov
Copy link
Collaborator

@georgehristov georgehristov commented Aug 5, 2020

  • Introduce constants to declare Model field filter presets
  • Adds Model::FIELD_FILTER_PERSIST filter preset
  • Adds Model::FIELD_FILTER_ONLY_FIELDS filter preset
  • uses new presets to remove duplicate code
  • Provide the possibility to filter fields by Closure

@georgehristov georgehristov force-pushed the feature/model-fields-filtering-persisting branch from 1132e47 to 945d6b9 Compare August 5, 2020 14:19
@georgehristov georgehristov marked this pull request as ready for review August 5, 2020 14:21
src/Model.php Outdated Show resolved Hide resolved
@mvorisek
Copy link
Member

mvorisek commented Aug 5, 2020

Provides possibility to filter fields by Closure

I do not get this - we should get all and filter then, what is the advantage of your approach?

@georgehristov
Copy link
Collaborator Author

This is also true. Main point was to introduce the 'persisting' preset to avoid duplicate code.
And streamline the code a bit with the ifs and buts.
Passing a Closure as filter is a side-effect

src/Model.php Outdated Show resolved Hide resolved
@georgehristov georgehristov force-pushed the feature/model-fields-filtering-persisting branch 2 times, most recently from d5de024 to 0d962c3 Compare August 6, 2020 08:00
@georgehristov georgehristov force-pushed the feature/model-fields-filtering-persisting branch from 2d89841 to 40cbf73 Compare August 6, 2020 08:17
src/Model.php Outdated Show resolved Hide resolved
src/Model.php Outdated Show resolved Hide resolved
src/Model.php Show resolved Hide resolved
src/Model.php Outdated Show resolved Hide resolved
@georgehristov georgehristov force-pushed the action_to_toquery_with_interfaces branch from c067309 to 66cb0b7 Compare August 17, 2020 13:39
@georgehristov georgehristov force-pushed the feature/model-fields-filtering-persisting branch from 2c70758 to 9c0472a Compare August 18, 2020 09:00
src/Model.php Outdated
@@ -625,15 +625,21 @@ public function isDirty(string $field): bool
*
* @return Field[]
*/
public function getFields($filters = null): array
public function getFields($filters = null, $onlyFields = null): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool $onlyFields?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why to null by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep BC

$model->getFields() returns all fields historically
$model->getFields('system') returns system fields in onlyFields

So if $filter argument set $onlyFields defaults to true

@georgehristov georgehristov force-pushed the feature/model-fields-filtering-persisting branch from 1b67214 to d6307b3 Compare August 18, 2020 09:31
} elseif (is_string($filter)) {
$filter = [$filter];
if ($filters === null) {
return $onlyFields ? $this->getFields(self::FIELD_FILTER_ONLY_FIELDS) : $this->fields;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to recurse, just set $filter...

return array_filter($this->fields, function (Field $field, $name) use ($filter) {
// do not return fields outside of "only_fields" scope
if ($this->only_fields && !in_array($name, $this->only_fields, true)) {
$onlyFields = $onlyFields ?? true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this then

return $field->isEditable();
case self::FIELD_FILTER_VISIBLE:
return $field->isVisible();
case self::FIELD_FILTER_ONLY_FIELDS:
Copy link
Member

@mvorisek mvorisek Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not need FIELD_FILTER_ONLY_FIELDS then

@georgehristov
Copy link
Collaborator Author

I have another idea:
Presently filters are in first argument as array and considered OR.

We can introduce multiple arguments which will be filters merged with AND.

So $model->getFields('system', 'only_fields') will return fields system AND only fields
So $model->getFields(['system', 'only_fields']) will return fields system OR only fields

It will not be BC though I believe.

@mvorisek
Copy link
Member

I have another idea:
Presently filters are in first argument as array and considered OR.

I like minimalistic APIs. We extend Model more and more for BC... And the questions is what this more and more code provides? Does it really help and reduce the maintanence requirements? If yes, go ahead!

@georgehristov georgehristov force-pushed the action_to_toquery_with_interfaces branch 2 times, most recently from fbff6c0 to d64c4c0 Compare September 11, 2020 20:19
@georgehristov georgehristov force-pushed the action_to_toquery_with_interfaces branch from d64c4c0 to 482fa01 Compare September 11, 2020 20:26
@georgehristov georgehristov changed the title [update] accept Closure for Model:getFields filter Refactor Model:getFields Sep 16, 2020
@romaninsh romaninsh added this to the 2.3 milestone Sep 16, 2020
Copy link
Member

@mvorisek mvorisek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to deduplicate the code.

However, I am not sure if we should mix very unrelated attributes together and offer partial "not" constants

Copy link
Member

@romaninsh romaninsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement.

Do address feedback from @mvorisek

@mvorisek mvorisek changed the title Refactor Model:getFields Refactor Model::getFields Apr 15, 2022
@mvorisek mvorisek removed this from the 2.3 milestone Apr 15, 2022
@mvorisek mvorisek marked this pull request as draft August 31, 2022 20:32
@mvorisek mvorisek changed the base branch from action_to_toquery_with_interfaces to develop September 7, 2023 14:52
@mvorisek
Copy link
Member

mvorisek commented Sep 7, 2023

TODO rebase on develop, it is based on #690, only the last 10 commits are relevant (others are from the mentioned PR) - be3cb50...d6307b3

@mvorisek mvorisek force-pushed the feature/model-fields-filtering-persisting branch from d6307b3 to 01375df Compare March 4, 2024 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants