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