1: <?php
2:
3: namespace Deimos\Flow;
4:
5: class ForeachState
6: {
7:
8: /**
9: * @var string
10: */
11: protected $_iteration;
12:
13: /**
14: * @var string
15: */
16: protected $_key;
17:
18: /**
19: * @var string
20: */
21: protected $_firstKey;
22:
23: /**
24: * @var string
25: */
26: protected $_lastKey;
27:
28: /**
29: * @var string
30: */
31: protected $_total;
32:
33: /**
34: * @var array
35: */
36: protected $storage;
37:
38: /**
39: * State constructor.
40: *
41: * @param array $storage
42: */
43: public function __construct(array &$storage)
44: {
45: $this->storage = &$storage;
46: $this->_total = count($storage);
47:
48: $this->_iteration = 0;
49: $this->_key = key($storage);
50: $this->_firstKey = key($storage);
51:
52: end($storage);
53: $this->_lastKey = key($storage);
54:
55: reset($storage);
56: }
57:
58: /**
59: * @param $name
60: *
61: * @return mixed
62: */
63: public function __get($name)
64: {
65: return $this->{$name}();
66: }
67:
68: /**
69: * @param $name
70: * @param $value
71: *
72: * @throws \BadFunctionCallException
73: */
74: public function __set($name, $value)
75: {
76: throw new \BadFunctionCallException(__METHOD__);
77: }
78:
79: /**
80: * @param $name
81: *
82: * @return bool
83: */
84: public function __isset($name)
85: {
86: return method_exists($this, $name);
87: }
88:
89: /**
90: * @return bool
91: */
92: public function odd()
93: {
94: return !$this->even();
95: }
96:
97: /**
98: * @return bool
99: */
100: public function even()
101: {
102: return !($this->iteration() & 1);
103: }
104:
105: /**
106: * @return bool
107: */
108: public function isFirst()
109: {
110: return $this->key() === $this->firstKey();
111: }
112:
113: /**
114: * @return bool
115: */
116: public function first()
117: {
118: return $this->isFirst();
119: }
120:
121: /**
122: * @return bool
123: */
124: public function isLast()
125: {
126: return $this->key() === $this->lastKey();
127: }
128:
129: /**
130: * @return bool
131: */
132: public function last()
133: {
134: return $this->isLast();
135: }
136:
137: /**
138: * @return int
139: */
140: public function iteration()
141: {
142: return $this->_iteration;
143: }
144:
145: /**
146: * @return string|int
147: */
148: public function key()
149: {
150: return $this->_key;
151: }
152:
153: /**
154: * @return mixed
155: */
156: public function value()
157: {
158: return $this->storage[$this->key()];
159: }
160:
161: /**
162: * @return string|int
163: */
164: public function firstKey()
165: {
166: return $this->_firstKey;
167: }
168:
169: /**
170: * @return string|int
171: */
172: public function lastKey()
173: {
174: return $this->_lastKey;
175: }
176:
177: /**
178: * @return mixed
179: */
180: public function firstValue()
181: {
182: return $this->storage[$this->firstKey()];
183: }
184:
185: /**
186: * @return mixed
187: */
188: public function lastValue()
189: {
190: return $this->storage[$this->lastKey()];
191: }
192:
193: /**
194: * @return int
195: */
196: public function total()
197: {
198: return $this->_total;
199: }
200:
201: /**
202: * @param $key
203: */
204: public function __invoke($key)
205: {
206: $this->_key = $key;
207: }
208:
209: }