Skip to content

Commit 7843c8e

Browse files
committed
Documentation
1 parent 7cfb0d5 commit 7843c8e

File tree

2 files changed

+6
-190
lines changed

2 files changed

+6
-190
lines changed

README.md

+6-181
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,18 @@
11
[![Version](https://img.shields.io/badge/version-4.0.0-blueviolet.svg)](https://github.com/steevanb/php-collection/tree/4.0.0)
22
[![PHP](https://img.shields.io/badge/php-^8.1-blue.svg)](https://php.net)
33
![Lines](https://img.shields.io/badge/code%20lines-4,637-blue.svg)
4-
![Downloads](https://poser.pugx.org/steevanb/php-collection/downloads)
4+
![Downloads](https://poser.pugx.org/steevanb/php-typed-array/downloads)
55
![GitHub workflow status](https://img.shields.io/github/actions/workflow/status/steevanb/php-collection/ci.yml?branch=master)
66
![Coverage](https://img.shields.io/badge/coverage-96%25-success.svg)
77

88
## php-collection
99

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.
1111

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.
1313

14-
[Changelog](changelog.md)
14+
The goal of this library is to remove array as much as possible to have typed data.
1515

16-
## Installation
16+
See [documentation](https://php-collection.readthedocs.io/).
1717

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).

bin/release/prepare

-9
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ function updateCoverageBadge() {
6262
sed -i "6s/.*/${badge}/" "${rootDir}"/README.md
6363
}
6464

65-
function updateComposerRequire() {
66-
trap onError ERR
67-
68-
echo "Edit composer require in README.md"
69-
local majorMinor=${version%.*}
70-
sed -i "s/composer require steevanb\/php-collection ^[[:digit:]]\+.[[:digit:]]\+/composer require steevanb\/php-collection ^${majorMinor}/" README.md
71-
}
72-
7365
function updateChangelog() {
7466
trap onError ERR
7567

@@ -81,5 +73,4 @@ initLog
8173
updateReleaseBadge
8274
updateCodeLinesBadge
8375
updateCoverageBadge
84-
updateComposerRequire
8576
updateChangelog

0 commit comments

Comments
 (0)