1: <?php
2:
3: namespace Chromabits\Nucleus\Data;
4:
5: use ArrayObject;
6: use Chromabits\Nucleus\Data\Interfaces\IterableInterface;
7: use Chromabits\Nucleus\Data\Interfaces\KeyFoldableInterface;
8: use Chromabits\Nucleus\Data\Interfaces\LeftKeyFoldableInterface;
9: use Chromabits\Nucleus\Data\Interfaces\ListInterface;
10: use Chromabits\Nucleus\Data\Interfaces\ListableInterface;
11: use Chromabits\Nucleus\Data\Interfaces\MapInterface;
12: use Chromabits\Nucleus\Data\Interfaces\MappableInterface;
13: use Chromabits\Nucleus\Data\Interfaces\SemigroupInterface;
14: use Chromabits\Nucleus\Data\Traits\ArrayBackingTrait;
15: use Chromabits\Nucleus\Exceptions\CoreException;
16: use Chromabits\Nucleus\Foundation\Interfaces\ArrayableInterface;
17: use Chromabits\Nucleus\Meditation\Boa;
18: use Chromabits\Nucleus\Meditation\Constraints\AbstractTypeConstraint;
19: use Chromabits\Nucleus\Meditation\Exceptions\MismatchedArgumentTypesException;
20:
21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class ArrayList extends IndexedCollection implements
32: ListInterface,
33: MapInterface,
34: ListableInterface,
35: MappableInterface,
36: KeyFoldableInterface,
37: LeftKeyFoldableInterface
38: {
39: use ArrayBackingTrait;
40:
41: 42: 43:
44: protected $value;
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: 66:
67: public function __construct(array $value = [])
68: {
69: parent::__construct();
70:
71: if ($value instanceof ArrayObject) {
72: $value = $value->getArrayCopy();
73: }
74:
75: $this->value = array_values($value);
76: $this->size = count($value);
77: }
78:
79: 80: 81:
82: public function getValueType()
83: {
84:
85: return Boa::any();
86: }
87:
88: 89: 90:
91: public function toArray()
92: {
93: return array_values($this->value);
94: }
95:
96: 97: 98:
99: public function reverse()
100: {
101: return new static(array_reverse($this->value));
102: }
103:
104: 105: 106: 107: 108:
109: public function filter(callable $callable)
110: {
111: $result = [];
112:
113: foreach ($this->value as $key => $value) {
114: if ($callable($value, $key, $this)) {
115: $result[] = $value;
116: }
117: }
118:
119: return static::of($result);
120: }
121:
122: 123: 124:
125: public function toList()
126: {
127: return $this;
128: }
129:
130: 131: 132:
133: public function toMap()
134: {
135: return new ArrayMap($this->value);
136: }
137:
138: 139: 140:
141: protected function assertNotEmpty()
142: {
143: if ($this->size < 1) {
144: throw new CoreException('List is empty');
145: }
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function append(SemigroupInterface $other)
158: {
159: if ($other instanceof static) {
160: return new static(array_merge($this->value, $other->value));
161: }
162:
163: $this->throwMismatchedDataTypeException($other);
164: }
165: }
166: