Skip to content

Feat/arrayable filters - #7

Draft
gwillz wants to merge 11 commits into
masterfrom
feat/arrayable-filters
Draft

Feat/arrayable filters#7
gwillz wants to merge 11 commits into
masterfrom
feat/arrayable-filters

Conversation

@gwillz

@gwillz gwillz commented Feb 1, 2024

Copy link
Copy Markdown
Collaborator

A preface

  • Arrayable introduces the toArray() method for objects to convert into JSON-ish objects.
  • ArrayableFields adds filtering and optional 'extra' fields.

This PR introduces deep filtering of child items to ArrayableFields as well as filtering to the generic Arrays::toArray() helper.

An example

A tree like:

root
   => field1
   => field2
   => child1
   => child2 => [deep1]
   => child3 => [deep2, deep3]

Now toArray() will give you all the fields of the root and of every child - with respect to the fields() config of each object.

Filtering exists already, but was limited to the root object. So although one can restrict whole fields/children the entire array conversion of each 'child' field is included if specified. In the example above, one couldn't get only deep2 without also getting deep3.

So we've introduced this syntax:

$root->toArray(['field1', 'child3.deep2' ]);

Which produces this:

[
  'field1' => 'test',
  'child3' => [ 'deep2' => 'test' ],
]

Extra fields

Extra fields are things not included in the standard output of the fields() config, perhaps they're too big, or they're expensive to compute. One can 'add' these to their output using the 2nd parameter:

$root->toArray(null, ['extra1']);

But again, we're restricted to full objects and only the extra fields of the root item. The filtering updates from fields also resolve this.

$root->toArray(null, ['extra1.name']);

At times we will also want to indiscriminately get an extra field from all items in the tree.

$root->toArray(null, ['*.extra1']);

Draft because

My tests are still broken for wildcards :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant