1: <?php
2:
3: namespace Chromabits\Nucleus\Data\Factories;
4:
5: use ArrayAccess;
6: use ArrayObject;
7: use Chromabits\Nucleus\Data\ArrayAccessMap;
8: use Chromabits\Nucleus\Data\ArrayList;
9: use Chromabits\Nucleus\Data\ArrayMap;
10: use Chromabits\Nucleus\Data\Interfaces\FoldableInterface;
11: use Chromabits\Nucleus\Data\Interfaces\FunctorInterface;
12: use Chromabits\Nucleus\Data\Interfaces\LeftFoldableInterface;
13: use Chromabits\Nucleus\Data\Interfaces\ListInterface;
14: use Chromabits\Nucleus\Data\Interfaces\MapInterface;
15: use Chromabits\Nucleus\Data\Interfaces\ReadMapInterface;
16: use Chromabits\Nucleus\Data\TraversableLeftFoldable;
17: use Chromabits\Nucleus\Exceptions\CoreException;
18: use Chromabits\Nucleus\Foundation\StaticObject;
19: use Chromabits\Nucleus\Meditation\Arguments;
20: use Chromabits\Nucleus\Meditation\Boa;
21: use Chromabits\Nucleus\Meditation\Exceptions\InvalidArgumentException;
22: use Traversable;
23:
24: 25: 26: 27: 28: 29:
30: class ComplexFactory extends StaticObject
31: {
32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public static function toLeftFoldable($input)
42: {
43: Arguments::define(Boa::leftFoldable())->check($input);
44:
45: if ($input instanceof LeftFoldableInterface) {
46: return $input;
47: }
48:
49: if (is_array($input) || $input instanceof ArrayObject) {
50: return static::toList($input);
51: }
52:
53: if ($input instanceof Traversable) {
54: return new TraversableLeftFoldable($input);
55: }
56:
57: throw new CoreException('Unable to build LeftFoldable');
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: public static function toFunctor($input)
69: {
70: Arguments::define(Boa::leftFoldable())->check($input);
71:
72: if ($input instanceof FunctorInterface) {
73: return $input;
74: }
75:
76: if (is_array($input) || $input instanceof ArrayObject) {
77: return static::toList($input);
78: }
79:
80: throw new CoreException('Unable to build Functor');
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90:
91: public static function toList($input)
92: {
93: Arguments::define(Boa::lst())->check($input);
94:
95: if ($input instanceof ListInterface) {
96: return $input;
97: }
98:
99: return ArrayList::of($input);
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110:
111: public static function toFoldable($input)
112: {
113: Arguments::define(Boa::foldable())->check($input);
114:
115: if ($input instanceof FoldableInterface) {
116: return $input;
117: }
118:
119: if (is_array($input) || $input instanceof ArrayObject) {
120: return static::toList($input);
121: }
122:
123: throw new CoreException('Unable to build Foldable');
124: }
125:
126: 127: 128: 129: 130: 131: 132: 133: 134:
135: public static function toReadMap($input)
136: {
137: Arguments::define(Boa::readMap())->check($input);
138:
139: if ($input instanceof ReadMapInterface) {
140: return $input;
141: }
142:
143: if (is_array($input)
144: || $input instanceof ArrayObject
145: || $input instanceof ArrayAccess
146: ) {
147: return static::toMap($input);
148: }
149:
150: if ($input instanceof Traversable) {
151: return new TraversableLeftFoldable($input);
152: }
153:
154: throw new CoreException('Unable to build ReadMap');
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165:
166: public static function toMap($input)
167: {
168: Arguments::define(Boa::map())->check($input);
169:
170: if ($input instanceof MapInterface) {
171: return $input;
172: }
173:
174: if (is_array($input) || $input instanceof ArrayObject) {
175: return new ArrayMap($input);
176: }
177:
178: if ($input instanceof ArrayAccess) {
179: return new ArrayAccessMap($input);
180: }
181:
182: throw new CoreException('Unable to build Map');
183: }
184: }
185: