{"id":15029185,"url":"https://github.com/jasny/immutable","last_synced_at":"2025-04-09T20:32:34.852Z","repository":{"id":56996773,"uuid":"175837562","full_name":"jasny/immutable","owner":"jasny","description":"Helper method for immutable objects","archived":false,"fork":false,"pushed_at":"2022-02-14T10:18:53.000Z","size":19,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T23:17:38.939Z","etag":null,"topics":["immutable-objects","php7","trait"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasny.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-15T14:40:19.000Z","updated_at":"2023-08-01T21:38:34.000Z","dependencies_parsed_at":"2022-08-21T14:10:43.447Z","dependency_job_id":null,"html_url":"https://github.com/jasny/immutable","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fimmutable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fimmutable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fimmutable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasny%2Fimmutable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasny","download_url":"https://codeload.github.com/jasny/immutable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248107606,"owners_count":21048966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["immutable-objects","php7","trait"],"created_at":"2024-09-24T20:09:55.148Z","updated_at":"2025-04-09T20:32:34.831Z","avatar_url":"https://github.com/jasny.png","language":"PHP","readme":"Jasny immutable\n===\n\n[![Build Status](https://travis-ci.org/jasny/immutable.svg?branch=master)](https://travis-ci.org/jasny/immutable)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jasny/immutable/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/jasny/immutable/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)\n[![Packagist Stable Version](https://img.shields.io/packagist/v/jasny/immutable.svg)](https://packagist.org/packages/jasny/immutable)\n[![Packagist License](https://img.shields.io/packagist/l/jasny/immutable.svg)](https://packagist.org/packages/jasny/immutable)\n\nThe library provides a helper methods for immutable objects.\n\nInstallation\n---\n\n    composer require jasny/immutable\n\nUsage\n---\n\n### NoDynamicProperties\n\nThe `Jasny\\Immutable\\NoDynamicProperties` defines the `__set` method, which will throw a `LogicException` when\nattempting to set a non-existing property.\n\n```php\nclass ImmutableFoo\n{\n    use Jasny\\Immutable\\NoDynamicProperties;\n}\n```\n\n### With\n\nImmutable objects may have `with...` methods, that create an altered version of the object, while the immutable object\nitself remains unchanged. An example are classes work as described in [PSR-7](https://www.php-fig.org/psr/psr-7/).\n\nThe `with...` methods in these objects typically follow the same method.\n\n```php\nclass ImmutableFoo\n{\n    public function withSomething(string $newValue)\n    {\n        if ($this-\u003esomething === $newValue) {\n            return $this;\n        }\n\n        $clone = clone $this;\n        $clone-\u003esomething = $newValue;\n\n        return $clone;\n    }\n}\n```\n\nThe `Jasny\\Immutable\\With` trait implements a protected `withProperty()` and `withoutProperty()` method for setting and\nunsetting properties on a clone of the object.\n\n```php\nclass ImmutableFoo\n{\n    use Jasny\\Immutable\\With;\n    \n    protected $something;\n\n    public function withSomething(string $value): self\n    {\n        return $this-\u003ewithProperty('something', $value);\n    }\n\n    public function withoutSomething(): self\n    {\n        return $this-\u003ewithoutProperty('something');\n    }\n}\n```\n\nThe trait contains the `withPropertyKey()` and `withoutPropertyKey()` methods for setting and unsetting an item of an\nassociative array.\n\n```php\nclass ImmutableFoo\n{\n    use Jasny\\Immutable\\With;\n\n    protected array $colors = [];\n\n    public function withColor(string $color, int $level): self\n    {\n        return $this-\u003ewithPropertyKey('colors', $color, $level);\n    }\n\n    public function withoutColor(string $color): self\n    {\n        return $this-\u003ewithoutPropertyKey('colors', $color);\n    }\n}\n```\n\nThe `withPropertyItem()` and `withoutPropertyItem()` methods work on a sequential array to add and remove an item.\n\n```php\nclass ImmutableFoo\n{\n    use Jasny\\Immutable\\With;\n\n    protected array $services = [];\n\n    public function withAddedService($service): self\n    {\n        return $this-\u003ewithPropertyItem('services', $service, true /* unique */);\n    }\n\n    public function withoutService($service): self\n    {\n        return $this-\u003ewithoutPropertyItem('services', $service);\n    }\n}\n```\n\nIf the third argument of `withPropertyItem()` is set to `true`, the item isn't added if it's already in the array.\n\nIf the item is in the array multiple times, `withoutPropertyItem()` will remove them all. Strict comparison is used to\nfind items.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasny%2Fimmutable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasny%2Fimmutable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasny%2Fimmutable/lists"}