ArrayableTrait
Adds a `toArray()` method that returns an array copy of this object.
- Nested 'Arrayable' objects are also converted to arrays.
- Object can declare a
fields()to include virtual fields. - This requires an iterable object (
IteratorAggregateinterface).
Objects should extend the ArrayableFields interface to get additional type safety and some goodies from extraFields().
Table of Contents
Methods
- extraFields() : array<string|int, callable>
- Define extra fields that you _might_ want included in the `toArray()` output.
- fields() : array<string|int, callable|bool>
- These are fields for use in `toArray()`.
- toArray() : array<string|int, mixed>
- Convert this object to an array.
Methods
extraFields()
Define extra fields that you _might_ want included in the `toArray()` output.
public
extraFields() : array<string|int, callable>
These are virtual fields that are not included by default like the
ones defined in fields(). Instead, use the second parameter of
the toArray(null, ['name']) method to specify which extra fields
you want in the output.
A virtual field is defined as a pair like 'name' => callback.
Tags
Return values
array<string|int, callable>fields()
These are fields for use in `toArray()`.
public
fields() : array<string|int, callable|bool>
By default this will include all public fields of the object.
Given 'name' => true a field will appear in the array output. Given
false|null|0|unset|... the field will not be present.
Define a pair like 'name' => callback create a 'virtual' field. This
can override existing properties or create whole new fields.
Note, if not implementing the ArrayableFields interface, this method will only define 'virtual' fields. That said, inheriting the parent fields is safe and will future-proof the code.
// Inheriting fields from the parent:
$fields = parent::fields();
$fields['hide_this_field'] = false;
unset($fields['this_also_works']);
return $fields;
// Declaring explicit fields, ignoring inherited:
return [
'real_field' => true,
'and_this_field' => true,
];
// Adding virtual fields:
$fields = parent::fields();
$fields['virtual_thing'] = [$this, 'getMyVirtualThing'];
$fields['inline_thing'] = function() {
return 'hey look: ' . time();
};
return $fields;
Tags
Return values
array<string|int, callable|bool>toArray()
Convert this object to an array.
public
toArray([array<string|int, string>|null $filter = null ][, array<string|int, string>|null $extra = null ][, bool $nulls = false ]) : array<string|int, mixed>
Specify a 'filter' to restrict the fields included in the output.
Specify an 'extra' to include additional fields if implementing the
ArrayableFields interface. This will add properties that were excluded
from fields() and includes any virtual fields defined in extraFields().
included in fields() and any virtual fields defined in extraFields().
Parameters
- $filter : array<string|int, string>|null = null
- $extra : array<string|int, string>|null = null
- $nulls : bool = false
-
- include null fields in the output