1: <?php
2:
3: namespace Deimos\Helper\Helpers\Arr;
4:
5: use Deimos\Helper\Exceptions\ExceptionEmpty;
6:
7: trait StackTrait
8: {
9:
10: 11: 12: 13: 14: 15:
16: public function push(array &$storage, $mixed)
17: {
18: return array_push($storage, $mixed);
19: }
20:
21: 22: 23: 24: 25:
26: public function pop(array &$storage)
27: {
28: return array_pop($storage);
29: }
30:
31: 32: 33: 34: 35:
36: public function shift(array &$storage)
37: {
38: return array_shift($storage);
39: }
40:
41: 42: 43: 44: 45: 46:
47: public function unShift(array &$storage, $mixed)
48: {
49: return array_unshift($storage, $mixed);
50: }
51:
52: 53: 54: 55: 56:
57: public function first(array $storage)
58: {
59: reset($storage);
60:
61: return current($storage);
62: }
63:
64: 65: 66: 67: 68:
69: public function last(array $storage)
70: {
71: return end($storage);
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function odd(array $storage)
82: {
83: return $this->filter($storage, function ($value)
84: {
85: return $this->helper->math()->isOdd($value);
86: });
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: public function even(array $storage)
97: {
98: return $this->filter($storage, function ($value)
99: {
100: return $this->helper->math()->isEven($value);
101: });
102: }
103:
104: 105: 106: 107: 108: 109: 110:
111: public function get(array $storage, $key, $default = null)
112: {
113: try
114: {
115: return $this->findPath($storage, $this->keys($key));
116: }
117: catch (ExceptionEmpty $empty)
118: {
119: return $default;
120: }
121: }
122:
123: 124: 125: 126: 127:
128: public function initOrPush(array &$storage, $key, $value)
129: {
130: if (empty($storage[$key]))
131: {
132: $storage[$key] = [];
133: }
134:
135: $storage[$key][] = $value;
136: }
137:
138: }
139: