{"id":22446958,"url":"https://github.com/cloudcreativity/utils-value","last_synced_at":"2025-03-27T11:23:16.441Z","repository":{"id":56954132,"uuid":"58796663","full_name":"cloudcreativity/utils-value","owner":"cloudcreativity","description":"Library to handle standard scalar values as objects in PHP","archived":false,"fork":false,"pushed_at":"2022-03-04T18:10:26.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-18T08:34:52.033Z","etag":null,"topics":["php","value-object"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudcreativity.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2016-05-14T08:01:46.000Z","updated_at":"2022-03-04T18:10:17.000Z","dependencies_parsed_at":"2022-08-21T08:50:18.988Z","dependency_job_id":null,"html_url":"https://github.com/cloudcreativity/utils-value","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudcreativity%2Futils-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudcreativity%2Futils-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudcreativity%2Futils-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudcreativity%2Futils-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudcreativity","download_url":"https://codeload.github.com/cloudcreativity/utils-value/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245833250,"owners_count":20679812,"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":["php","value-object"],"created_at":"2024-12-06T04:13:12.752Z","updated_at":"2025-03-27T11:23:16.420Z","avatar_url":"https://github.com/cloudcreativity.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/cloudcreativity/utils-value.svg?branch=master)](https://travis-ci.org/cloudcreativity/utils-value)\n\n# cloudcreativity/utils-value\n\nA small library to handle scalar values that have meaning.\n\n## Values\n\nIt is common to handle scalar values that have particular meaning to your\napplication. For instance, if objects in your applications expect a string\nthat can only be one of several different string values - e.g. `lead`,\n`follow` for describing dance roles - then any object that expects those values\nwould always have to check that the string received matches those two options.\nFor example:\n\n``` php\npublic function setRole($value) {\n  if (!in_array($value, ['lead', 'follow'])) {\n    throw new \\InvalidArgumentException('Expecting a valid dance role.');\n  }\n\n  // ...\n}\n```\n\nThis becomes highly repetitive - plus if you wanted to add `either` as a valid\nvalue, you'd need to update it in every `in_array()` call across the\napplication.\n\nThis is where we'd use the `CloudCreativity\\Utils\\Value\\AbstractValue`\nclass from this package. For example:\n\n``` php\nuse CloudCreativity\\Utils\\Value\\AbstractValue;\n\nclass DanceRole extends AbstractValue\n{\n  \n  protected function accept($value): bool\n  {\n    return in_array($value, ['lead', 'follow'], true);\n  }\n}\n```\n\nThe original setter shown above would then become:\n\n``` php\npublic function setRole(DanceRole $role)\n{\n  // no need to check it as we know it is valid.\n}\n```\n\n## Helper Methods\n\nQuite often we find it is useful for the value object to have helper methods. By implementing\nthese on the value object, they become available to any part of your application that\nreceives an instance of the value.\n\nFor instance, or `DanceRole` object could look like this:\n\n``` php\nclass DanceRole extends AbstractValue\n{\n\n  public function isLead(): bool\n  {\n    return $this-\u003eis('lead');\n  }\n\n  public function isFollow(): bool\n  {\n    return $this-\u003eis('follow');\n  }\n\n  // ... \n}\n```\n\n## Trait\n\nIf you do not want to extend the abstract class, the majority of methods are available via the `ValueTrait`.\n\n## Interface\n\nThis package defines an interface, `ValueInterface`, to ensure consistency across value objects:\n\n- `__toString()` : so that a scalar value can always be written to a string.\n- `toString()` : so that a scalar value can fluently be cast to a string.\n- `jsonSerialize()` : so that a scalar value can always be cast properly when being JSON encoded.\n- `get()` : gets the underlying scalar value, e.g. would return a string if the value object holds a string.\n- `is(...$values)` : compares provided values to the underlying scalar value, returning `true` if the value matches\nany of them.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudcreativity%2Futils-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudcreativity%2Futils-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudcreativity%2Futils-value/lists"}