{"id":22496991,"url":"https://github.com/morebec/valueobjects","last_synced_at":"2025-03-27T21:25:29.688Z","repository":{"id":57019776,"uuid":"217546006","full_name":"Morebec/ValueObjects","owner":"Morebec","description":"[DEPRECATED] A PHP Value Object Library in use by Morebec Projects, (see morebec/orkestra instead)","archived":false,"fork":false,"pushed_at":"2020-11-09T04:47:49.000Z","size":3845,"stargazers_count":2,"open_issues_count":9,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T23:45:15.630Z","etag":null,"topics":["enum","php","valueobject"],"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/Morebec.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-10-25T13:59:39.000Z","updated_at":"2021-08-18T23:18:04.000Z","dependencies_parsed_at":"2022-08-22T20:31:17.958Z","dependency_job_id":null,"html_url":"https://github.com/Morebec/ValueObjects","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2FValueObjects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2FValueObjects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2FValueObjects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2FValueObjects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morebec","download_url":"https://codeload.github.com/Morebec/ValueObjects/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245925768,"owners_count":20694958,"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":["enum","php","valueobject"],"created_at":"2024-12-06T20:15:21.393Z","updated_at":"2025-03-27T21:25:29.665Z","avatar_url":"https://github.com/Morebec.png","language":"PHP","readme":"# ValueObjects\nA PHP Value Object Library in use by Morebec Projects\n\n[![Build Status](https://travis-ci.com/Morebec/ValueObjects.svg?branch=master)](https://travis-ci.com/Morebec/ValueObjects)\n[![Coverage Status](https://coveralls.io/repos/github/Morebec/ValueObjects/badge.svg?branch=master)](https://coveralls.io/github/Morebec/ValueObjects?branch=master)\n\nValue objects are small objects representing simple concepts, and whose equality is\nbased on their internal property values rather than a specific identity.\n\nValue objects must honour the following contract:\n\n- They are immutable (no setters)\n- They are Self Validating\n- They represent and describe concepts in a clear way\n\n## Installation\nTo install the library in a project, add these lines to your `composer.json` configuration file:\n\n```json\n{\n    \"repositories\": [\n        {\n            \"url\": \"https://github.com/Morebec/ValueObjects.git\",\n            \"type\": \"git\"\n        }\n    ],\n    \"require\": {\n        \"morebec/value-objects\": \"^1.0\"\n    }\n}\n```\n \n## Usage\nThis library comes with a number of predesigned ValueObject classes, \nthat you can use in your projects.\nThe ValueObject either implement the `ValueObjectInterface` or extend the \n`BasicEnum` class.\n\n\n### Creating your own Value Object using `ValueObjectInterface`\nTo create, one needs to implement the `ValueObjectInterface` and\nimplement the two following methods: \n- `__toString()`\n- `isEqualTo(ValueObjectInterface $valueObject): bool`\n\nHere's a basic example: \n\n```php\nuse Assert\\Assertion;\nuse Morebec\\ValueObjects\\ValueObjectInterface;\n\n/**\n * Age Value Object\n */\nfinal class Age implements ValueObjectInterface\n{\n    /** @var int age */\n    private $age;\n\n    public function __construct(int $age)\n    {\n        Assertion::min($age, 1);\n        $this-\u003eage = $age;\n    }\n\n    public function __toString()\n    {\n        return strval($this-\u003eage);\n    }\n\n    /**\n     * Returns the value of this age object\n     * @return int\n     */\n    public function toInt(): int\n    {\n        return $this-\u003eage;\n    }\n\n    /**\n     * Indicates if this value object is equal to abother value object\n     * @param  ValueObjectInterface $valueObject othervalue object to compare to\n     * @return boolean                           true if equal otherwise false\n     */\n    public function isEqualTo(ValueObjectInterface $vo): bool\n    {\n        return (string)$this === (string)$vo;\n    }\n}\n```\n\nDoing that, our class can be used as follows:\n\n```php\n$age = new Age(24);\n\n// Test Equality\n$maturity = new Age(18);\n$age-\u003eisEqualTo($maturity); // false  \n$age == $maturity; // false\n$age === '18'; // false\n\n// Test Greater than\n$age-\u003etoInt() \u003e= 18; // true\n$age-\u003etoInt() \u003e= $maturity-\u003etoInt();\n\n```\n\n### Creating your own Enum class extending `BasicEnum`\nTo create a new Enum, one needs to extend the `BasicEnum` class.\nAs an example, lets pretend we want to create a CardinalPoint Class.\nSince there are strictly 4 cardinal points, we will create an enum based \nValueObject:\n\n```php\n\u003c?php\n\nuse Morebec\\ValueObjects\\ValueObjectInterface;\n\n/**\n * CardinalPoint\n */\nclass CardinalPoint implements ValueObjectInterface\n{\n    const NORTH = 'NORTH';    \n    const EAST = 'EAST';    \n    const WEST = 'WEST';  \n    const SOUTH = 'SOUTH';\n}\n```\n\nDoing this, will allow us to use our class in the following way:\n\n```php\n// Instatiate a new CardinalPoint instance\n$direction = new CardinalPoint(CardinalPoint::NORTH);\n\n// Since Enums have builtin validation,\n// the following line would throw an InvalidArgumentException:\n$direction = new CardinalPoint('North');\n// However the following would work:\n$direction = new CardinalPoint('NORTH');\n\n// Using in functions or class methods \npublic function changeDirection(CardinalPoint $direction)\n{\n    // Testing equlity with string\n    if(!$direction-\u003eisEqualTo(new CardinalPoint(CardinalPoint::EAST))) {\n        echo 'Not going East!';\n    }\n\n    // Since the constants are strings, it is also possible to compare\n    // using string comparison\n    if($direction == CardinalPoint::NORTH) {\n        echo 'Definitely going North!';\n    }    \n}\n\n```\n\n## Running Tests\nThe tests are based on codeception.\nTo run the tests simply run:\n\n```bash\ncomposer test\n```\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Fvalueobjects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorebec%2Fvalueobjects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Fvalueobjects/lists"}