{"id":17024659,"url":"https://github.com/michael-rubel/laravel-value-objects","last_synced_at":"2025-04-04T18:09:07.828Z","repository":{"id":59680471,"uuid":"538449048","full_name":"michael-rubel/laravel-value-objects","owner":"michael-rubel","description":"A bunch of general-purpose value objects you can use in your Laravel application.","archived":false,"fork":false,"pushed_at":"2024-03-15T21:16:22.000Z","size":622,"stargazers_count":189,"open_issues_count":0,"forks_count":14,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-15T07:26:23.349Z","etag":null,"topics":["laravel","looking-for-contributors","php","value-object","value-objects"],"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/michael-rubel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://paypal.com/donate/?hosted_button_id=KHLEL8PFS4AXJ"]}},"created_at":"2022-09-19T10:32:13.000Z","updated_at":"2024-10-13T14:38:11.000Z","dependencies_parsed_at":"2023-10-02T08:02:06.528Z","dependency_job_id":"ae8e9fac-6bb2-436a-ad5b-8f15bcc13fe3","html_url":"https://github.com/michael-rubel/laravel-value-objects","commit_stats":{"total_commits":361,"total_committers":6,"mean_commits":"60.166666666666664","dds":0.07479224376731297,"last_synced_commit":"5487c4f85332b395d85e861397d219453250136e"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":"michael-rubel/laravel-package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-rubel%2Flaravel-value-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-rubel%2Flaravel-value-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-rubel%2Flaravel-value-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michael-rubel%2Flaravel-value-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michael-rubel","download_url":"https://codeload.github.com/michael-rubel/laravel-value-objects/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226215,"owners_count":20904465,"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":["laravel","looking-for-contributors","php","value-object","value-objects"],"created_at":"2024-10-14T07:26:30.228Z","updated_at":"2025-04-04T18:09:07.808Z","avatar_url":"https://github.com/michael-rubel.png","language":"PHP","readme":"![Value Objects for Laravel](https://user-images.githubusercontent.com/37669560/200172635-6b2ca8d8-fb2b-4037-a697-b8f6e4c8c615.png)\n\n# Laravel Value Objects\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/michael-rubel/laravel-value-objects.svg?style=flat-square\u0026logo=packagist)](https://packagist.org/packages/michael-rubel/laravel-value-objects)\n[![Tests](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-value-objects/run-tests.yml?branch=main\u0026style=flat-square\u0026label=tests\u0026logo=github)](https://github.com/michael-rubel/laravel-value-objects/actions)\n[![Code Quality](https://img.shields.io/scrutinizer/quality/g/michael-rubel/laravel-value-objects.svg?style=flat-square\u0026logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-value-objects/?branch=main)\n[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/michael-rubel/laravel-value-objects.svg?style=flat-square\u0026logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-value-objects/?branch=main)\n[![Infection](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-value-objects/infection.yml?branch=main\u0026style=flat-square\u0026label=infection\u0026logo=php)](https://github.com/michael-rubel/laravel-value-objects/actions)\n[![Larastan](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-value-objects/phpstan.yml?branch=main\u0026style=flat-square\u0026label=larastan\u0026logo=laravel)](https://github.com/michael-rubel/laravel-value-objects/actions)\n\nA bunch of general-purpose value objects you can use in your Laravel application.\n\n---\n\nThe package requires `PHP 8.1` or higher and `Laravel 10` or higher.\n\n## #StandWithUkraine\n[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)\n\n## Installation\nInstall the package using composer:\n```bash\ncomposer require michael-rubel/laravel-value-objects\n```\n\n## Built-in value objects\n\n- [`Boolean`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Boolean.php)\n- [`ClassString`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/ClassString.php)\n- [`Email`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Email.php)\n- [`FullName`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/FullName.php)\n- [`Name`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Name.php)\n- [`Number`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Number.php)\n- [`Phone`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Phone.php)\n- [`TaxNumber`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/TaxNumber.php)\n- [`Text`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Text.php)\n- [`Url`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Url.php)\n- [`Uuid`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Uuid.php)\n\n### Artisan command\nYou can generate custom value objects with Artisan command:\n```shell\nphp artisan make:value-object YourNameValueObject\n```\n\n## Usage\n\n### Boolean\n```php\n$bool = new Boolean('1');\n$bool = Boolean::make('1');\n$bool = Boolean::from('1');\n\n$bool-\u003evalue();   // true\n(string) $bool;   // 'true'\n$bool-\u003etoArray(); // ['true']\n```\n\n---\n\n### Number\n```php\n$number = new Number('10.20999', scale: 2);\n$number = Number::make('10.20999', scale: 2);\n$number = Number::from('10.20999', scale: 2);\n\n$number-\u003evalue();   // '10.20'\n(string) $number;   // '10.20'\n$number-\u003etoArray(); // ['10.20']\n\n// Starting from version `3.5.0` also\n// accepts locale-formatted numbers:\n$number = new Number('1.230,00');\n$number-\u003evalue(); // '1230.00'\n$number = new Number('1,230.00');\n$number-\u003evalue(); // '1230.00'\n$number = new Number('1 230,00');\n$number-\u003evalue(); // '1230.00'\n$number = new Number('1 230.00');\n$number-\u003evalue(); // '1230.00'\n```\n\n---\n\n### Text\n```php\n$text = new Text('Lorem Ipsum is simply dummy text.');\n$text = Text::make('Lorem Ipsum is simply dummy text.');\n$text = Text::from('Lorem Ipsum is simply dummy text.');\n\n$text-\u003evalue();   // 'Lorem Ipsum is simply dummy text.'\n(string) $text;   // 'Lorem Ipsum is simply dummy text.'\n$text-\u003etoArray(); // ['Lorem Ipsum is simply dummy text.']\n```\n\n---\n\n### ClassString\n```php\n$classString = new ClassString('\\Exception');\n$classString = ClassString::make('\\Exception');\n$classString = ClassString::from('\\Exception');\n\n$classString-\u003evalue();   // '\\Exception'\n(string) $classString;   // '\\Exception'\n$classString-\u003etoArray(); // ['\\Exception']\n\n$classString-\u003eclassExists();     // true\n$classString-\u003einterfaceExists(); // false\n$classString-\u003einstantiate();     // Exception { ... }\n$classString-\u003einstantiateWith(['message' =\u003e 'My message.']); // Exception { #message: \"test\" ... }\n```\n\n---\n\n### Email\n```php\n$email = new Email('michael@laravel.software');\n$email = Email::make('michael@laravel.software');\n$email = Email::from('michael@laravel.software');\n\n$email-\u003evalue();   // 'michael@laravel.software'\n(string) $email;   // 'michael@laravel.software'\n$email-\u003etoArray(); // ['email' =\u003e 'michael@laravel.software', 'username' =\u003e 'michael', 'domain' =\u003e 'laravel.software']\n```\n\n---\n\n### FullName\n```php\n$name = new FullName(' Taylor   Otwell ');\n$name = FullName::make(' Taylor   Otwell ');\n$name = FullName::from(' Taylor   Otwell ');\n\n$name-\u003evalue();   // 'Taylor Otwell'\n(string) $name;   // 'Taylor Otwell'\n\n$name-\u003efullName();  // 'Taylor Otwell'\n$name-\u003efirstName(); // 'Taylor'\n$name-\u003elastName();  // 'Otwell'\n\n$name = 'Richard Le Poidevin';\n\n$fullName = new FullName($name, limit: 2);\n\n$fullName-\u003etoArray();\n\n// array:3 [\n//  \"fullName\" =\u003e \"Richard Le Poidevin\"\n//  \"firstName\" =\u003e \"Richard\"\n//  \"lastName\" =\u003e \"Le Poidevin\"\n// ]\n```\n\n---\n\n### Name\n```php\n$name = new Name(' Company name! ');\n$name = Name::make(' Company name! ');\n$name = Name::from(' Company name! ');\n\n$name-\u003evalue();   // 'Company name!'\n(string) $name;   // 'Company name!'\n$name-\u003etoArray(); // ['Company name!']\n```\n\n---\n\n### Phone\n```php\n$phone = new Phone(' +38 000 000 00 00 ');\n$phone = Phone::make(' +38 000 000 00 00 ');\n$phone = Phone::from(' +38 000 000 00 00 ');\n\n$phone-\u003evalue();   // '+38 000 000 00 00'\n(string) $phone;   // '+38 000 000 00 00'\n$phone-\u003etoArray(); // ['+38 000 000 00 00']\n\n$phone-\u003esanitized(); // '+380000000000'\n```\n\n---\n\n### TaxNumber\n```php\n$taxNumber = new TaxNumber('0123456789', 'PL');\n$taxNumber = TaxNumber::make('0123456789', 'PL');\n$taxNumber = TaxNumber::from('0123456789', 'PL');\n\n$taxNumber-\u003evalue();   // 'PL0123456789'\n(string) $taxNumber;   // 'PL0123456789'\n$taxNumber-\u003etoArray(); // ['fullTaxNumber' =\u003e 'PL0123456789', 'taxNumber' =\u003e '0123456789', 'prefix' =\u003e 'PL']\n\n$taxNumber-\u003efullTaxNumber(); // 'PL0123456789'\n$taxNumber-\u003etaxNumber();     // '0123456789'\n$taxNumber-\u003eprefix();        // 'PL'\n```\n\n---\n\n### Url\n```php\n$uuid = new Url('my-blog-page');\n$uuid = Url::make('my-blog-page');\n$uuid = Url::from('my-blog-page');\n\n$uuid-\u003evalue();   // 'https://example.com/my-blog-page'\n(string) $uuid;   // 'https://example.com/my-blog-page'\n$uuid-\u003etoArray(); // ['https://example.com/my-blog-page']\n```\n\n---\n\n### Uuid\n```php\n$uuid = new Uuid('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');\n$uuid = Uuid::make('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');\n$uuid = Uuid::from('8547d10c-7a37-492a-8d33-be0e5ae6119b', 'Optional name');\n\n$uuid-\u003evalue();   // '8547d10c-7a37-492a-8d33-be0e5ae6119b'\n(string) $uuid;   // '8547d10c-7a37-492a-8d33-be0e5ae6119b'\n$uuid-\u003etoArray(); // ['name' =\u003e 'Optional name', 'value' =\u003e '8547d10c-7a37-492a-8d33-be0e5ae6119b']\n\n$uuid-\u003euuid(); // '8547d10c-7a37-492a-8d33-be0e5ae6119b'\n$uuid-\u003ename(); // 'Optional name'\n```\n\n## Handle failed validation\n\nIf you want to avoid try/catching your value object when the validation fails, you can use `makeOrNull` method:\n\n```php\n$bool = Boolean::makeOrNull('bad input'); // null\n\n$bool?-\u003evalue(); // null\n```\n\n## Extending functionality\nAll value objects are [Macroable](https://laravel.com/api/9.x/Illuminate/Support/Traits/Macroable.html).\nThis way you can add new methods dynamically. If you need to extend existing methods, you can create a value object locally with `make:value-object` command and use inheritance.\n\nFor example:\n```php\nValueObject::macro('str', function () {\n    return str($this-\u003evalue());\n});\n\n$name = new Text('Lorem ipsum');\n\n$name-\u003estr()-\u003eis('Lorem ipsum'); // true\n```\n\n## Conditionable\nValue objects utilize a [Conditionable](https://laravel.com/api/9.x/Illuminate/Support/Traits/Conditionable.html) trait.\nYou can use `when` and `unless` methods.\n\n```php\nTaxNumber::from('PL0123456789')-\u003ewhen(function ($number) {\n    return $number-\u003eprefix() !== null;\n})-\u003eprefix();\n```\n\n## Contributing\nIf you see any way we can improve the package, or maybe you want to make your own custom value object as built-in, PRs are welcome.\n\n## Testing\n```bash\ncomposer test\n```\n\n## License\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://paypal.com/donate/?hosted_button_id=KHLEL8PFS4AXJ"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichael-rubel%2Flaravel-value-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichael-rubel%2Flaravel-value-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichael-rubel%2Flaravel-value-objects/lists"}