PluginProbe
Age Gate / 3.7.3
Age Gate v3.7.3
3.7.3 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 113 releases
age-gate / vendor / nette / utils / src / Utils / ArrayHash.php

ArrayHash.php in Age Gate 3.7.3, at vendor/nette/utils/src/Utils/ArrayHash.php

106 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
8 declare(strict_types=1);
9
10 namespace Nette\Utils;
11
12 use Nette;
13
14
15 /**
16 * Provides objects to work as array.
17 * @template T
18 * @implements \RecursiveArrayIterator<array-key, T>
19 * @implements \ArrayAccess<array-key, T>
20 */
21 class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate
22 {
23 /**
24 * Transforms array to ArrayHash.
25 * @param array<T> $array
26 * @return static
27 */
28 public static function from(array $array, bool $recursive = true)
29 {
30 $obj = new static;
31 foreach ($array as $key => $value) {
32 $obj->$key = $recursive && is_array($value)
33 ? static::from($value, true)
34 : $value;
35 }
36
37 return $obj;
38 }
39
40
41 /**
42 * Returns an iterator over all items.
43 * @return \RecursiveArrayIterator<array-key, T>
44 */
45 public function getIterator(): \RecursiveArrayIterator
46 {
47 return new \RecursiveArrayIterator((array) $this);
48 }
49
50
51 /**
52 * Returns items count.
53 */
54 public function count(): int
55 {
56 return count((array) $this);
57 }
58
59
60 /**
61 * Replaces or appends a item.
62 * @param array-key $key
63 * @param T $value
64 */
65 public function offsetSet($key, $value): void
66 {
67 if (!is_scalar($key)) { // prevents null
68 throw new Nette\InvalidArgumentException(sprintf('Key must be either a string or an integer, %s given.', gettype($key)));
69 }
70
71 $this->$key = $value;
72 }
73
74
75 /**
76 * Returns a item.
77 * @param array-key $key
78 * @return T
79 */
80 #[\ReturnTypeWillChange]
81 public function offsetGet($key)
82 {
83 return $this->$key;
84 }
85
86
87 /**
88 * Determines whether a item exists.
89 * @param array-key $key
90 */
91 public function offsetExists($key): bool
92 {
93 return isset($this->$key);
94 }
95
96
97 /**
98 * Removes the element from this list.
99 * @param array-key $key
100 */
101 public function offsetUnset($key): void
102 {
103 unset($this->$key);
104 }
105 }
106