|
1 | 1 | [](https://github.com/steevanb/php-collection/tree/4.0.0)
|
2 | 2 | [](https://php.net)
|
3 | 3 | 
|
4 |
| - |
| 4 | + |
5 | 5 | 
|
6 | 6 | 
|
7 | 7 |
|
8 | 8 | ## php-collection
|
9 | 9 |
|
10 |
| -Bored of not knowing value type in array? You are at the right spot! |
| 10 | +php-collection is a PHP library to create collections. |
11 | 11 |
|
12 |
| -With `php-collection`, you can type your array values. How? Cause now you will use object instead of array, who can control their values. |
| 12 | +A collection is a list of typed data with methods to work on it. |
13 | 13 |
|
14 |
| -[Changelog](changelog.md) |
| 14 | +The goal of this library is to remove array as much as possible to have typed data. |
15 | 15 |
|
16 |
| -## Installation |
| 16 | +See [documentation](https://php-collection.readthedocs.io/). |
17 | 17 |
|
18 |
| -``` |
19 |
| -composer require steevanb/php-collection ^4.0 |
20 |
| -``` |
21 |
| - |
22 |
| -## Typed array available |
23 |
| - |
24 |
| - * `FloatArray`: can store `float` |
25 |
| - * `FloatNullableArray`: can store `float` or `null` |
26 |
| - * `IntArray`: can store `int` |
27 |
| - * `IntNullableArray`: can store `int` or `null` |
28 |
| - * `StringArray`: can store `string` |
29 |
| - * `StringNullableArray`: can store `string` or `null` |
30 |
| - * `AbstractObjectArray`: can store `object` |
31 |
| - * `AbstractObjectNullableArray`: can store `object` or `null` |
32 |
| - * `AbstractEnumArray`: can store instances of `\UnitEnum` (PHP 8.1 enums) |
33 |
| - |
34 |
| -## Usage |
35 |
| - |
36 |
| -Simple usage: |
37 |
| -```php |
38 |
| -$intArray = new IntArray([1, 2]); |
39 |
| -$intArray['key'] = 3; |
40 |
| -foreach ($intArray as $key => $int) { |
41 |
| - // do your stuff, you are SURE $int is integer! |
42 |
| -} |
43 |
| -``` |
44 |
| - |
45 |
| -Usefull usage: |
46 |
| -```php |
47 |
| -function returnInts(): IntArray |
48 |
| -{ |
49 |
| - return new IntArray([1, 2, 3]); |
50 |
| -} |
51 |
| - |
52 |
| -foreach (returnInts() as $key => $int) { |
53 |
| - // do your stuff, you are SURE $int is an integer! |
54 |
| -} |
55 |
| -``` |
56 |
| - |
57 |
| -This will throw an `\Exception`, cause `'foo'` is not allowed: |
58 |
| -```php |
59 |
| -$intArray = new IntArray([1, 2, 'foo']); |
60 |
| -``` |
61 |
| - |
62 |
| -### Filter values to be uniques |
63 |
| - |
64 |
| -If you want to be sure a value is unique inside your Collection, you have to configure `valueAlreadyExistMode`: |
65 |
| - |
66 |
| -```php |
67 |
| -// Default behavior, code here is just for the example |
68 |
| -// IntArray will contain [1, 2, 2] |
69 |
| -new IntArray([1, 2, 2], ValueAlreadyExistsModeEnum::ADD); |
70 |
| - |
71 |
| -// IntArray will contain [1, 2] |
72 |
| -new IntArray([1, 2, 2], ValueAlreadyExistsModeEnum::DO_NOT_ADD); |
73 |
| - |
74 |
| -// ValueAlreadyExistSException will be throwned |
75 |
| -(new IntArray([1, 2, 2], ValueAlreadyExistsModeEnum::EXCEPTION)); |
76 |
| -``` |
77 |
| - |
78 |
| -### Read only |
79 |
| - |
80 |
| -Sometimes when you add values in your Collection, once you have finished you don't want this object to be modified. |
81 |
| - |
82 |
| -You can have this behavior with read only: |
83 |
| - |
84 |
| -```php |
85 |
| -$foo = new IntArray([1, 2]); |
86 |
| -// By default, you can add values after object creation |
87 |
| -$foo[] = 3; |
88 |
| -// Now set read only |
89 |
| -$foo->setReadOnly(); // you don't need to define first parameter to true: it's default value |
90 |
| -// ReadOnlyException will be throwned |
91 |
| -$foo[] = 4; |
92 |
| - |
93 |
| -// You can disable readonly |
94 |
| -$foo->setReadOnly(false); |
95 |
| -$foo[] = 3; |
96 |
| -``` |
97 |
| - |
98 |
| -### ObjectArray |
99 |
| - |
100 |
| -If you need to store objects in array, you have to create a classe who extends |
101 |
| -`Steevanb\PhpCollection\ObjectArray\AbstractObjectArray` or `AbstractObjectNullableArray`. |
102 |
| - |
103 |
| -```php |
104 |
| -class DateTimeArray extends AbstractObjectArray |
105 |
| -{ |
106 |
| - public function __construct(iterable $values = []) |
107 |
| - { |
108 |
| - parent::__construct(\DateTime::class, $values); |
109 |
| - } |
110 |
| - |
111 |
| - /** This method is not mandatory, but you can create it to type $values */ |
112 |
| - public function merge(self $values): static |
113 |
| - { |
114 |
| - parent::doMerge($values); |
115 |
| - |
116 |
| - return $this; |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * This method is not mandatory, but you can create it to type return when you access an item |
121 |
| - * $data = new DateTimeArray([new \DateTime(), new \DateTime()]); |
122 |
| - * // Autocompletion should work with the override of current() |
123 |
| - * $first = $data[0]; |
124 |
| - */ |
125 |
| - public function current(): ?\DateTime |
126 |
| - { |
127 |
| - return parent::current(); |
128 |
| - } |
129 |
| - |
130 |
| - /** This method is not mandatory, but you can create it to type the return */ |
131 |
| - /** @return array<\DateTime> */ |
132 |
| - public function toArray(): array |
133 |
| - { |
134 |
| - return parent::toArray(); |
135 |
| - } |
136 |
| -} |
137 |
| -``` |
138 |
| - |
139 |
| -### EnumArray |
140 |
| - |
141 |
| -If you need to store enums in array, |
142 |
| -you can create a class who extends `Steevanb\PhpCollection\EnumArray\AbstractEnumArray`. |
143 |
| - |
144 |
| -```php |
145 |
| -class FooEnumArray extends AbstractEnumArray |
146 |
| -{ |
147 |
| - public function __construct(iterable $values = []) |
148 |
| - { |
149 |
| - parent::__construct(FooEnum::clas, $values); |
150 |
| - } |
151 |
| - |
152 |
| - /** This method is not mandatory, but you can create it to type $values */ |
153 |
| - public function merge(self $values): static |
154 |
| - { |
155 |
| - parent::doMerge($values); |
156 |
| - |
157 |
| - return $this; |
158 |
| - } |
159 |
| - |
160 |
| - /** |
161 |
| - * This method is not mandatory, but you can create it to type return when you access an item |
162 |
| - * $data = new FooEnumArray([FooEnum::VALUE1, FooEnum::VALUE2]); |
163 |
| - * // Autocompletion should work with the override of current() |
164 |
| - * $first = $data[0]; |
165 |
| - */ |
166 |
| - public function current(): ?FooEnum |
167 |
| - { |
168 |
| - return parent::current(); |
169 |
| - } |
170 |
| - |
171 |
| - /** This method is not mandatory, but you can create it to type the return */ |
172 |
| - /** @return array<FooEnum> */ |
173 |
| - public function toArray(): array |
174 |
| - { |
175 |
| - return parent::toArray(); |
176 |
| - } |
177 |
| -} |
178 |
| -``` |
179 |
| - |
180 |
| -## Methods to modify keys and values |
181 |
| - |
182 |
| -All methods below will directly apply modifications, |
183 |
| -it will not return a new Collection with modifications applied like some PHP functions do. |
184 |
| - |
185 |
| -| Method | PHP associated function | Description | |
186 |
| -| --- | --- | --- | |
187 |
| -| `clear()` | _none_ | Clear all data and reset next key to `0`. Next data added with `$array[]` will have key `0`. | |
188 |
| -| `changeKeyCase()` | [array_change_key_case()](https://www.php.net/manual/fr/function.array-change-key-case.php) | Changes the case of all keys | |
189 |
| - |
190 |
| -## Bridges |
191 |
| - |
192 |
| -You can easily integrate `php-collection` in your projects with these bridges: |
193 |
| -* [Symfony](documentation/BridgeSymfony.md) |
| 18 | +See [changelog](changelog.md). |
0 commit comments