|
1 |
| -# immutable-object |
| 1 | +# Immutable Object |
| 2 | + |
| 3 | +[](LICENSE) |
| 4 | + |
| 5 | +* [Overview](#overview) |
| 6 | +* [Installation](#installation) |
| 7 | +* [How to use](#how-to-use) |
| 8 | +* [License](#license) |
| 9 | +* [Contributing](#contributing) |
| 10 | + |
| 11 | +<div id='overview'></div> |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +The **Immutable Object** library ensures that objects implementing it remain immutable after initialization. Once |
| 16 | +created, the state of the object cannot be modified. Any attempt to change properties or collection elements will throw |
| 17 | +an exception. |
| 18 | + |
| 19 | +<div id='installation'></div> |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require tiny-blocks/immutable-object |
| 25 | +``` |
| 26 | + |
| 27 | +<div id='how-to-use'></div> |
| 28 | + |
| 29 | +## How to use |
| 30 | + |
| 31 | +The library provides the `Immutable` interface and the `Immutability` trait to guarantee immutability. These components |
| 32 | +prevent properties and collections from being modified or unset. |
| 33 | + |
| 34 | +### Concrete implementation |
| 35 | + |
| 36 | +By implementing the `Immutable` interface and using the `Immutability` trait, you can ensure that object properties and |
| 37 | +elements in collections are immutable. |
| 38 | + |
| 39 | +```php |
| 40 | +<?php |
| 41 | + |
| 42 | +namespace Example; |
| 43 | + |
| 44 | +use TinyBlocks\Immutable\Immutable; |
| 45 | +use TinyBlocks\Immutable\Immutability; |
| 46 | + |
| 47 | +final class Order implements Immutable |
| 48 | +{ |
| 49 | + use Immutability; |
| 50 | + |
| 51 | + public function __construct(public int $id, public Products $products) |
| 52 | + { |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +#### Handling property immutability |
| 58 | + |
| 59 | +The Immutability trait also prevents modifications to properties of an object. Trying to modify a property after the |
| 60 | +object is initialized will throw an exception. |
| 61 | + |
| 62 | +```php |
| 63 | +$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']); |
| 64 | + |
| 65 | +$order->id = 2; # Throws an exception |
| 66 | +unset($order->id); # Throws an exception |
| 67 | +``` |
| 68 | + |
| 69 | +#### Handling collection immutability |
| 70 | + |
| 71 | +The `Immutability` trait also prevents the modification of collection elements (e.g., arrays). Trying to modify or |
| 72 | +remove an element will throw an exception. |
| 73 | + |
| 74 | +```php |
| 75 | +$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']); |
| 76 | + |
| 77 | +$order->items[0] = 'item3'; # Throws an exception |
| 78 | +unset($order->items[0]); # Throws an exception |
| 79 | +``` |
| 80 | + |
| 81 | +<div id='license'></div> |
| 82 | + |
| 83 | +## License |
| 84 | + |
| 85 | +Immutable Object is licensed under [MIT](LICENSE). |
| 86 | + |
| 87 | +<div id='contributing'></div> |
| 88 | + |
| 89 | +## Contributing |
| 90 | + |
| 91 | +Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to |
| 92 | +contribute to the project. |
0 commit comments