ArrayHash.php
2 years ago
ArrayList.php
2 years ago
Arrays.php
2 years ago
Callback.php
2 years ago
DateTime.php
2 years ago
FileSystem.php
2 years ago
Floats.php
2 years ago
Helpers.php
2 years ago
Html.php
2 years ago
Image.php
2 years ago
Json.php
2 years ago
ObjectHelpers.php
2 years ago
ObjectMixin.php
2 years ago
Paginator.php
2 years ago
Random.php
2 years ago
Reflection.php
2 years ago
Strings.php
2 years ago
Type.php
2 years ago
Validators.php
2 years ago
exceptions.php
2 years ago
ArrayHash.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Nette Framework (https://nette.org) |
| 5 | * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | */ |
| 7 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Utils; |
| 9 | |
| 10 | use FapiMember\Library\Nette; |
| 11 | /** |
| 12 | * Provides objects to work as array. |
| 13 | * @template T |
| 14 | * @implements \RecursiveArrayIterator<array-key, T> |
| 15 | * @implements \ArrayAccess<array-key, T> |
| 16 | */ |
| 17 | class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate |
| 18 | { |
| 19 | /** |
| 20 | * Transforms array to ArrayHash. |
| 21 | * @param array<T> $array |
| 22 | * @return static |
| 23 | */ |
| 24 | public static function from(array $array, bool $recursive = \true) |
| 25 | { |
| 26 | $obj = new static(); |
| 27 | foreach ($array as $key => $value) { |
| 28 | $obj->{$key} = ($recursive && is_array($value)) ? static::from($value, \true) : $value; |
| 29 | } |
| 30 | return $obj; |
| 31 | } |
| 32 | /** |
| 33 | * Returns an iterator over all items. |
| 34 | * @return \RecursiveArrayIterator<array-key, T> |
| 35 | */ |
| 36 | public function getIterator(): \RecursiveArrayIterator |
| 37 | { |
| 38 | return new \RecursiveArrayIterator((array) $this); |
| 39 | } |
| 40 | /** |
| 41 | * Returns items count. |
| 42 | */ |
| 43 | public function count(): int |
| 44 | { |
| 45 | return count((array) $this); |
| 46 | } |
| 47 | /** |
| 48 | * Replaces or appends a item. |
| 49 | * @param array-key $key |
| 50 | * @param T $value |
| 51 | */ |
| 52 | public function offsetSet($key, $value): void |
| 53 | { |
| 54 | if (!is_scalar($key)) { |
| 55 | // prevents null |
| 56 | throw new Nette\InvalidArgumentException(sprintf('Key must be either a string or an integer, %s given.', gettype($key))); |
| 57 | } |
| 58 | $this->{$key} = $value; |
| 59 | } |
| 60 | /** |
| 61 | * Returns a item. |
| 62 | * @param array-key $key |
| 63 | * @return T |
| 64 | */ |
| 65 | #[\ReturnTypeWillChange] |
| 66 | public function offsetGet($key) |
| 67 | { |
| 68 | return $this->{$key}; |
| 69 | } |
| 70 | /** |
| 71 | * Determines whether a item exists. |
| 72 | * @param array-key $key |
| 73 | */ |
| 74 | public function offsetExists($key): bool |
| 75 | { |
| 76 | return isset($this->{$key}); |
| 77 | } |
| 78 | /** |
| 79 | * Removes the element from this list. |
| 80 | * @param array-key $key |
| 81 | */ |
| 82 | public function offsetUnset($key): void |
| 83 | { |
| 84 | unset($this->{$key}); |
| 85 | } |
| 86 | } |
| 87 |