1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Inputs implements \Iterator
21: {
22: 23: 24: 25: 26:
27: private $datas = array();
28:
29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: public function __call($name, $arguments)
40: {
41: if (method_exists('Kotchasan\InputItem', $name)) {
42: $result = array();
43: foreach ($this->datas as $key => $item) {
44: $result[$key] = $this->collectInputs($item, $name, $arguments);
45: }
46: return $result;
47: } else {
48: throw new \InvalidArgumentException('Method '.$name.' not found');
49: }
50: }
51:
52: 53: 54: 55: 56: 57:
58: public function __construct(array $items = array(), $type = null)
59: {
60: foreach ($items as $key => $value) {
61: if (is_array($value)) {
62: foreach ($value as $k => $v) {
63: $this->datas[$k][$key] = InputItem::create($v, $type);
64: }
65: } else {
66: $this->datas[$key] = InputItem::create($value, $type);
67: }
68: }
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79:
80: private function collectInputs($item, $name, $arguments)
81: {
82: if (is_array($item)) {
83: $array = array();
84: foreach ($item as $k => $v) {
85: $array[$k] = $this->collectInputs($v, $name, $arguments);
86: }
87: return $array;
88: }
89: if (isset($arguments[1])) {
90: return $item->$name($arguments[0], $arguments[1]);
91: } elseif (isset($arguments[0])) {
92: return $item->$name($arguments[0]);
93: } else {
94: return $item->$name($arguments);
95: }
96: }
97:
98: 99: 100: 101: 102:
103: public function current()
104: {
105: $var = current($this->datas);
106:
107: return $var;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: public function get($key)
118: {
119: return $this->datas[$key];
120: }
121:
122: 123: 124: 125: 126:
127: public function key()
128: {
129: $var = key($this->datas);
130: return $var;
131: }
132:
133: 134: 135: 136: 137:
138: public function next()
139: {
140: $var = next($this->datas);
141: return $var;
142: }
143:
144: 145: 146:
147: public function rewind()
148: {
149: reset($this->datas);
150: }
151:
152: 153: 154:
155: public function valid()
156: {
157: $key = key($this->datas);
158: return $key !== null && $key !== false;
159: }
160: }
161: