PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / utils / src / Utils / ArrayHash.php

ArrayHash.php in Packeta 2.1, at deps/nette/utils/src/Utils/ArrayHash.php

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