1: <?php
2:
3: namespace Deimos\Helper\Helpers\Arr;
4:
5: use Deimos\Helper\AbstractHelper;
6: use Deimos\Helper\Exceptions\ExceptionEmpty;
7:
8: class Arr extends AbstractHelper
9: {
10:
11: use KeyTrait;
12: use StackTrait;
13:
14: 15: 16: 17: 18: 19:
20: public function map(array $storage, $callback)
21: {
22: return array_map($callback, $storage);
23: }
24:
25: 26: 27: 28: 29: 30:
31: public function filter(array $storage, $callback)
32: {
33: return array_filter($storage, $callback, ARRAY_FILTER_USE_BOTH);
34: }
35:
36: 37: 38: 39: 40: 41: 42:
43: public function fill($string, $length, $start = 0)
44: {
45: return array_fill($start, $length, $string);
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: public function range($begin, $end, $step = 1)
56: {
57: return range($begin, $end, $step);
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function inStrict(array $storage, $needle)
67: {
68: return in_array($needle, $storage, true);
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function in(array $storage, $needle)
78: {
79: return in_array($needle, $storage, false);
80: }
81:
82: 83: 84: 85: 86:
87: public function shuffle(array &$storage)
88: {
89: return shuffle($storage);
90: }
91:
92: 93: 94: 95: 96:
97: public function set(array &$storage, $path, $value)
98: {
99: $keys = $this->keys($path);
100:
101: $rows = &$storage;
102: foreach ($keys as $key)
103: {
104: if (!$this->keyExists($rows, $key))
105: {
106: $rows[$key] = [];
107: }
108:
109: $rows = &$rows[$key];
110: }
111:
112: $rows = $value;
113: }
114:
115: 116: 117: 118: 119: 120: 121:
122: protected function findPath(array $storage, array $keys)
123: {
124: if (empty($keys))
125: {
126: throw new ExceptionEmpty('Not found keys');
127: }
128:
129: $rows = &$storage;
130:
131: foreach ($keys as $key)
132: {
133: if (!$this->keyExists($rows, $key))
134: {
135: throw new ExceptionEmpty("Key {$key} not found");
136: }
137:
138: $rows = &$rows[$key];
139: }
140:
141: return $rows;
142: }
143:
144: 145: 146: 147: 148: 149: 150:
151: public function getRequired(array $storage, $key = null)
152: {
153: return $this->findPath($storage, $this->keys($key));
154: }
155:
156: }