https://github.com/jasny/immutable
Helper method for immutable objects
https://github.com/jasny/immutable
immutable-objects php7 trait
Last synced: 11 months ago
JSON representation
Helper method for immutable objects
- Host: GitHub
- URL: https://github.com/jasny/immutable
- Owner: jasny
- License: mit
- Created: 2019-03-15T14:40:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-14T10:18:53.000Z (about 4 years ago)
- Last Synced: 2025-04-01T23:17:38.939Z (11 months ago)
- Topics: immutable-objects, php7, trait
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Jasny immutable
===
[](https://travis-ci.org/jasny/immutable)
[](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)
[](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)
[](https://packagist.org/packages/jasny/immutable)
[](https://packagist.org/packages/jasny/immutable)
The library provides a helper methods for immutable objects.
Installation
---
composer require jasny/immutable
Usage
---
### NoDynamicProperties
The `Jasny\Immutable\NoDynamicProperties` defines the `__set` method, which will throw a `LogicException` when
attempting to set a non-existing property.
```php
class ImmutableFoo
{
use Jasny\Immutable\NoDynamicProperties;
}
```
### With
Immutable objects may have `with...` methods, that create an altered version of the object, while the immutable object
itself remains unchanged. An example are classes work as described in [PSR-7](https://www.php-fig.org/psr/psr-7/).
The `with...` methods in these objects typically follow the same method.
```php
class ImmutableFoo
{
public function withSomething(string $newValue)
{
if ($this->something === $newValue) {
return $this;
}
$clone = clone $this;
$clone->something = $newValue;
return $clone;
}
}
```
The `Jasny\Immutable\With` trait implements a protected `withProperty()` and `withoutProperty()` method for setting and
unsetting properties on a clone of the object.
```php
class ImmutableFoo
{
use Jasny\Immutable\With;
protected $something;
public function withSomething(string $value): self
{
return $this->withProperty('something', $value);
}
public function withoutSomething(): self
{
return $this->withoutProperty('something');
}
}
```
The trait contains the `withPropertyKey()` and `withoutPropertyKey()` methods for setting and unsetting an item of an
associative array.
```php
class ImmutableFoo
{
use Jasny\Immutable\With;
protected array $colors = [];
public function withColor(string $color, int $level): self
{
return $this->withPropertyKey('colors', $color, $level);
}
public function withoutColor(string $color): self
{
return $this->withoutPropertyKey('colors', $color);
}
}
```
The `withPropertyItem()` and `withoutPropertyItem()` methods work on a sequential array to add and remove an item.
```php
class ImmutableFoo
{
use Jasny\Immutable\With;
protected array $services = [];
public function withAddedService($service): self
{
return $this->withPropertyItem('services', $service, true /* unique */);
}
public function withoutService($service): self
{
return $this->withoutPropertyItem('services', $service);
}
}
```
If the third argument of `withPropertyItem()` is set to `true`, the item isn't added if it's already in the array.
If the item is in the array multiple times, `withoutPropertyItem()` will remove them all. Strict comparison is used to
find items.