Skip to content

Latest commit

 

History

History
574 lines (369 loc) · 13.2 KB

ArrayAccessorTrait.md

File metadata and controls

574 lines (369 loc) · 13.2 KB

ArrayAccessorTrait Documentation

\Greg\Support\Accessor\ArrayAccessorTrait is a trait for public usage of an storage in a class.

Example:

class Storage
{
    private $storage = [];
    
    use \Greg\Support\Accessor\ArrayAccessorTrait;

    private function &getAccessor()
    {
        return $this->storage;
    }
}

Table of contents:

Methods:

  • has - Determine if a key or an array of keys exists in accessor;
  • hasIndex - Determine if an index or an array of indexes exists in accessor;
  • set - Set a value to accessor;
  • setRef - Set a value reference to accessor;
  • setIndex - Set a value to accessor, using index;
  • setIndexRef - Set a value reference to accessor, using index;
  • get - Get a value or an array of values from accessor;
  • getRef - Get a value reference or an array of values reference from accessor;
  • getForce - Get a value or an array of values from accessor. If the key does not exists, it is added to the array;
  • getForceRef - Get a value reference or an array of values reference from accessor. If the key does not exists, it is added to the array;
  • getArray - Get a value as array or accessor of values as array from accessor;
  • getArrayRef - Get a value reference as array or an array of values reference as array from accessor;
  • getArrayForce - Get a value as array or an array of values as array from accessor. If the key does not exists, it is added to the array;
  • getArrayForceRef - Get a value reference as array from accessor. If the key does not exists, it is added to the array;
  • getIndex - Get a value or an array of values from accessor, using index;
  • getIndexRef - Get a value reference or an array of values reference from accessor, using index;
  • getIndexForce - Get a value or an array of values from accessor, using index. If the index does not exists, it is added to the array;
  • getIndexForceRef - Get a value reference or an array of values reference from accessor, using index. If the index does not exists, it is added to the array;
  • getIndexArray - Get a value as array or an array of values as array from accessor, using index;
  • getIndexArrayRef - Get a value reference as array or an array of values reference as array from accessor, using index;
  • getIndexArrayForce - Get a value as array or an array of values as array from accessor, using index. If the index does not exists, it is added to the array;
  • getIndexArrayForceRef - Get a value reference as array or an array of values reference as array from accessor, using index. If the index does not exists, it is added to the array;
  • remove - Remove a value or an array of values from accessor;
  • removeIndex - Remove a value or an array of values from accessor, using index.

has

Determine if a key or an array of keys exists in accessor.

has(string|array $key): boolean

$key - Could be a key or an array of keys.

Example:

// accessor: ['foo' => 'FOO', 'bar' => 'BAR'];

$storage->has('foo'); // result: true

hasIndex

Determine if an index or an array of indexes exists in accessor.

hasIndex(string|array $index, string $delimiter = self::INDEX_DELIMITER): boolean

$index - Could be an index or an array of indexes;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'bar' => 'BAR'];

$storage->hasIndex('foo.bar'); // result: true

set

Set a value to accessor.

set(string $key, mixed $value): array

$key - Key;
$value - Value.

Example:

// accessor: ['foo' => 'FOO'];

$storage->set('bar', 'BAR'); // result: ['foo' => 'FOO', 'bar' => 'BAR']

setRef

Set a value reference to accessor.

setRef(string $key, mixed &$value): array

$key - Key;
$value - Value reference.

Example:

// accessor: ['foo' => 'FOO'];

$bar = 'BAR';

$storage->setRef('bar', $bar); // result: ['foo' => 'FOO', 'bar' => 'BAR']

$bar = 'BAR2';

// accessor: ['foo' => 'FOO', 'bar' => 'BAR2'];

setIndex

Set a value to accessor, using index.

setIndex(string $index, mixed $value, string $delimiter = self::INDEX_DELIMITER): array

$index - Index;
$value - Value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$storage->setIndex('bar.baz', 'BAZ'); // result: ['foo' => 'FOO', 'bar' => ['baz' => 'BAZ']]

setIndexRef

Set a value reference to accessor, using index.

setIndexRef(string $index, mixed &$value, string $delimiter = self::INDEX_DELIMITER): array

$index - Index;
$value - Value reference;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$baz = 'BAZ';

$storage->setIndexRef('bar.baz', $baz); // result: ['foo' => 'FOO', 'bar' => ['baz' => 'BAZ']]

$baz = 'BAZ2';

// accessor: ['foo' => 'FOO', 'bar' => ['baz' => 'BAZ2']];

get

Get a value or an array of values from accessor.

get(string|array $key, mixed|array<mixed> $else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$storage->get('foo'); // result: FOO

getRef

Get a value reference or an array of values reference from accessor.

getRef(string|array $key, mixed|array<mixed> &$else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$foo = &$storage->getRef('foo'); // result: FOO

$foo = 'FOO2';

// accessor: ['foo' => 'FOO2']

getForce

Get a value or an array of values from accessor. If the key does not exists, it is added to the array.

getForce(string|array $key, mixed|array<mixed> $else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$storage->getForce('bar'); // result: null

// accessor: ['foo' => 'FOO', 'bar' => null]

getForceRef

Get a value reference or an array of values reference from accessor. If the key does not exists, it is added to the array.

getForceRef(string|array $key, mixed|array<mixed> &$else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$bar = &$storage->getForceRef('bar'); // result: null

$bar = 'BAR';

// accessor: ['foo' => 'FOO', 'bar' => 'BAR']

getArray

Get a value as array or an array of values as array from accessor.

getArray(string|array $key, mixed|array<mixed> $else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$storage->getArray('foo'); // result: ['FOO']

getArrayRef

Get a value reference as array or an array of values reference as array from accessor.

getArrayRef(string|array $key, mixed|array<mixed> &$else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$foo = &$storage->getArrayRef('foo'); // result: ['FOO']

// accessor: ['foo' => ['FOO']]

$foo[0] = 'FOO2';

// accessor: ['foo' => ['FOO2']]

getArrayForce

Get a value as array or an array of values as array from accessor. If the key does not exists, it is added to the array.

getArrayForce(string|array $key, mixed|array<mixed> $else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$storage->getArrayForce('bar'); // result: []

// accessor: ['foo' => 'FOO', 'bar' => []]

getArrayForceRef

Get a value reference as array or an array of values reference as array from accessor. If the key does not exists, it is added to the array.

getArrayForceRef(string|array $key, mixed|array<mixed> &$else = null): mixed

$key - Key;
$else - If the key does not exists, return this value.

Example:

// accessor: ['foo' => 'FOO'];

$bar = &$storage->getArrayForceRef('bar'); // result: []

$bar[0] = 'BAR';

// accessor: ['foo' => 'FOO', 'bar' => ['BAR']]

getIndex

Get a value or an array of values from accessor, using index.

getIndex(string|array $index, mixed|array<mixed> $else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => ['bar' => 'BAR']];

$storage->getIndex('foo.bar'); // result: BAR

getIndexRef

Get a value reference or an array of values reference from accessor, using index.

getIndexRef(string|array $index, mixed|array<mixed> &$else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => ['bar' => 'BAR']];

$bar = &$storage->getIndexRef('foo.bar'); // result: BAR

$bar = 'BAR2';

// accessor: ['foo' => ['bar' => 'BAR2']]

getIndexForce

Get a value or an array of values from accessor, using index. If the index does not exists, it is added to the array.

getIndexForce(string|array $index, mixed|array<mixed> $else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$storage->getIndexForce('bar.baz'); // result: null

// accessor: ['foo' => 'FOO', 'bar' => ['baz' => null]]

getIndexForceRef

Get a value reference or an array of values reference from accessor, using index. If the index does not exists, it is added to the array.

getIndexForceRef(string|array $index, mixed|array<mixed> &$else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$baz = &$storage->getIndexForceRef('bar.baz'); // result: null

$baz = 'BAZ';

// accessor: ['foo' => 'FOO', 'bar' => ['baz' => 'BAZ']]

getIndexArray

Get a value as array or an array of values as array from accessor, using index.

getIndexArray(string|array $index, mixed|array<mixed> $else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => ['bar' => 'BAR']];

$storage->getIndexArray('foo.bar'); // result: ['BAR']

getIndexArrayRef

Get a value reference as array or an array of values reference as array from accessor, using index.

getIndexArrayRef(string|array $index, mixed|array<mixed> &$else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => ['bar' => 'BAR']];

$foo = &$storage->getIndexArrayRef('foo.bar'); // result: ['BAR']

// accessor: ['foo' => ['bar' => ['BAR']]]

$foo[0] = 'BAR2';

// accessor: ['foo' => ['bar' => ['BAR2']]]

getIndexArrayForce

Get a value as array or an array of values as array from accessor, using index. If the index does not exists, it is added to the array.

getIndexArrayForce(string|array $index, mixed|array<mixed> $else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$storage->getIndexArrayForce('bar.baz'); // result: []

// accessor: ['foo' => 'FOO', 'bar' => ['baz' => []]]

getIndexArrayForceRef

Get a value reference as array or an array of values reference as array from accessor, using index. If the index does not exists, it is added to the array.

getIndexArrayForceRef(string|array $index, mixed|array<mixed> &$else = null, string $delimiter = self::INDEX_DELIMITER): mixed

$index - Index;
$else - If the index does not exists, return this value;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => 'FOO'];

$baz = &$storage->getIndexArrayForceRef('bar.baz'); // result: []

$baz[0] = 'BAZ';

// accessor: ['foo' => 'FOO', 'bar' => ['baz' => ['BAZ']]]

remove

Remove a value or an array of values from accessor.

remove(string|array $key): array

$key - Key.

Example:

// accessor: ['foo' => 'FOO'];

$storage->remove('foo'); // result: []

removeIndex

Remove a value or an array of values from accessor, using index.

removeIndex(string|array $index, string $delimiter = self::INDEX_DELIMITER): array

$index - Index;
$delimiter - Index delimiter.

Example:

// accessor: ['foo' => ['bar' => 'BAR']];

$storage->remove('foo.bar'); // result: ['foo' => []]