1: <?php
2:
3: namespace Chromabits\Nucleus\Data;
4:
5: use ArrayObject;
6: use Chromabits\Nucleus\Control\Interfaces\ApplicativeInterface;
7: use Chromabits\Nucleus\Control\Maybe;
8: use Chromabits\Nucleus\Data\Interfaces\FoldableInterface;
9: use Chromabits\Nucleus\Data\Interfaces\IterableInterface;
10: use Chromabits\Nucleus\Data\Interfaces\KeyFoldableInterface;
11: use Chromabits\Nucleus\Data\Interfaces\LeftFoldableInterface;
12: use Chromabits\Nucleus\Data\Interfaces\LeftKeyFoldableInterface;
13: use Chromabits\Nucleus\Data\Interfaces\ListInterface;
14: use Chromabits\Nucleus\Data\Interfaces\MapInterface;
15: use Chromabits\Nucleus\Data\Interfaces\MappableInterface;
16: use Chromabits\Nucleus\Data\Interfaces\MonoidInterface;
17: use Chromabits\Nucleus\Data\Interfaces\SemigroupInterface;
18: use Chromabits\Nucleus\Data\Traits\ArrayBackingTrait;
19: use Chromabits\Nucleus\Exceptions\CoreException;
20: use Chromabits\Nucleus\Foundation\Interfaces\ArrayableInterface;
21: use Chromabits\Nucleus\Meditation\Boa;
22: use Chromabits\Nucleus\Meditation\Constraints\AbstractTypeConstraint;
23: use Chromabits\Nucleus\Meditation\Exceptions\MismatchedArgumentTypesException;
24:
25: 26: 27: 28: 29: 30: 31: 32:
33: class ArrayMap extends KeyedCollection implements
34: MapInterface,
35: MonoidInterface,
36: FoldableInterface,
37: LeftFoldableInterface,
38: ApplicativeInterface,
39: MappableInterface,
40: ListInterface,
41: KeyFoldableInterface,
42: LeftKeyFoldableInterface
43: {
44: use ArrayBackingTrait;
45:
46: 47: 48: 49: 50:
51: public static function of($input)
52: {
53: if ($input instanceof static) {
54: return $input;
55: } elseif ($input instanceof ArrayableInterface) {
56: return new static($input->toArray());
57: }
58:
59: return new static($input);
60: }
61:
62: 63: 64:
65: protected $value;
66:
67: 68: 69: 70: 71:
72: public function __construct($value = [])
73: {
74: parent::__construct();
75:
76: if ($value instanceof ArrayObject) {
77: $value = $value->getArrayCopy();
78: }
79:
80: $this->value = $value;
81: $this->size = count($value);
82: }
83:
84: 85: 86:
87: public function getKeyType()
88: {
89:
90: return Boa::any();
91: }
92:
93: 94: 95:
96: public function getValueType()
97: {
98:
99: return Boa::any();
100: }
101:
102: 103: 104: 105: 106:
107: public function lookup($key)
108: {
109: if ($this->member($key) === false) {
110: return Maybe::nothing();
111: }
112:
113: $copy = array_merge($this->value);
114:
115: return Maybe::just($copy[$key]);
116: }
117:
118: 119: 120: 121: 122:
123: public function filter(callable $callable)
124: {
125: $result = [];
126:
127: foreach ($this->value as $key => $value) {
128: if ($callable($value, $key, $this)) {
129: $result[$key] = $value;
130: }
131: }
132:
133: return static::of($result);
134: }
135:
136: 137: 138:
139: public function toArray()
140: {
141: return $this->value;
142: }
143:
144: 145: 146:
147: public function reverse()
148: {
149: return new static(array_reverse($this->value, true));
150: }
151:
152: 153: 154:
155: public function toList()
156: {
157: return $this->values();
158: }
159:
160: 161: 162:
163: public function toMap()
164: {
165: return $this;
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176:
177: public function append(SemigroupInterface $other)
178: {
179: if ($other instanceof static) {
180: return new static(array_merge($this->value, $other->value));
181: }
182:
183: $this->throwMismatchedDataTypeException($other);
184: }
185: }
186: