{"id":21688458,"url":"https://github.com/spaark/composite-utils","last_synced_at":"2025-03-20T12:27:24.586Z","repository":{"id":57056261,"uuid":"81650125","full_name":"spaark/composite-utils","owner":"spaark","description":"A set of utilities which make interacting with Composite Objects easier","archived":false,"fork":false,"pushed_at":"2017-12-20T21:31:57.000Z","size":385,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-25T22:03:18.355Z","etag":null,"topics":["composite-utils","open-source","opensource","php","php-library","php-utility","php7","utility-library"],"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/spaark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-11T11:59:35.000Z","updated_at":"2022-02-12T12:47:38.000Z","dependencies_parsed_at":"2022-08-24T07:30:25.301Z","dependency_job_id":null,"html_url":"https://github.com/spaark/composite-utils","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spaark%2Fcomposite-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spaark%2Fcomposite-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spaark%2Fcomposite-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spaark%2Fcomposite-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spaark","download_url":"https://codeload.github.com/spaark/composite-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244610669,"owners_count":20481033,"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":["composite-utils","open-source","opensource","php","php-library","php-utility","php7","utility-library"],"created_at":"2024-11-25T17:15:10.236Z","updated_at":"2025-03-20T12:27:24.556Z","avatar_url":"https://github.com/spaark.png","language":"PHP","readme":"# Composite Utils\n\n[![Build Status][travis-img]][travis]\n[![Scrutinizer Code Quality][scrutinizer-img]][scrutinizer]\n[![Code Coverage][code-coverage-img]][scrutinizer]\n\nA set of utilities which make interacting with Composite Objects easier\n\n## The problem\n\nComposite objects are normally little more than a collection of\nproperties with meaningful names. While this is definitely better than\njust using an array, this normally requires a long set of getters and\nsetters; these often do nothing but move the value around:\n\n```php\npublic function setProperty($value)\n{\n    $this-\u003eproperty = $value;\n}\n\npublic function getProperty()\n{\n    return $this-\u003eproperty;\n}\n```\n\nBloat, bloat, bloat!\n\nThat's nine lines of code to reimplement what is just\n`$object-\u003eproperty = $value`. At best, these lines might enforce some\nform of datatype value:\n\n```php\npublic function setProperty(RequiredPropertyType $value)\n{\n    $this-\u003eproperty = $value;\n}\n```\n\nThis problem also exists in constructors, how often have you seen\nsomething like this:\n\n```php\npublic function __construct($value1, $value2, $value3, $value4, $value5)\n{\n    // Set values\n    $this-\u003evalue1 = $value1;\n    $this-\u003evalue2 = $value2;\n    $this-\u003evalue3 = $value3;\n    $this-\u003evalue4 = $value4;\n    $this-\u003evalue5 = $value5;\n    \n    // Init Values\n    $this-\u003evalue6 = new Collection();\n    $this-\u003evalue7 = new HashMap();\n}\n```\n\n## The solution\n\nThis library introduces a set of tools to remove the need to composite\ngetters, setters and bloated constructors without giving up your control\nover property access and data type enforcement.\n\n```php\nclass MyAwesomeComposite\n{\n    use AutoConstructTrait;\n    use PropertyAccessTrait;\n\n    /**\n     * @var int\n     */\n    protected $property = 42;\n\n    /**\n     * @var string\n     * @readable\n     * @writable\n     */\n    protected $stringProperty = 'some value';\n\n    /**\n     * @var DataType\n     * @readable\n     * @construct required\n     */\n     protected $iNeedThis;\n\n    /**\n     * @var ArrayObject\n     * @readable\n     * @construct new\n     */\n    protected $collection;\n}\n```\n\nThat's it. Nothing more required! All the properties other than\n`$property` can be accessed when required, and `$collection` will be\nautomatically constructed for you. Although you can't see it, this\nentity also has a constructor present which requires... you guessed\nit... The `$iNeedThis` property, and it needs to be a `DataType`.\n`$stringProperty` is also writable, if anything other than a string is\npassed to it, it will be automatically cast to one if possible:\n\n```php\n\n$class = new MyAwesomeComposite(); // Fails, $iNeedThis is required\n\n$iNeedThis = new OtherDataType();\n$class = new MyAwesomeComposite(); // Fails, $iNeedThis should be a DataType\n\n$iNeedThis = new DataType();\n$class = new MyAwesomeComposite($iNeedThis); // All good!\n\nvar_dump($class-\u003eproperty); // Fails, not readable\nvar_dump($class-\u003estringProperty); // string(10) \"some value\"\n\n$class-\u003estringProperty = 00000000123;\nvar_dump($class-\u003estringProperty); // string(3) \"123\"\n\n$class-\u003eiNeedThis = new DataType(); // Fails, not writable\n\nvar_dump($class-\u003ecollection); // ArrayObject { }\n\nvar_dump($class-\u003eiNeedThis === $iNeedThis); // true\n```\n\n[travis]: https://travis-ci.org/spaark/composite-utils\n[travis-img]: https://travis-ci.org/spaark/composite-utils.svg?branch=master\n[scrutinizer]: https://scrutinizer-ci.com/g/spaark/composite-utils/\n[scrutinizer-img]: https://scrutinizer-ci.com/g/spaark/composite-utils/badges/quality-score.png?b=master\n[code-coverage-img]: https://scrutinizer-ci.com/g/spaark/composite-utils/badges/coverage.png?b=master\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspaark%2Fcomposite-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspaark%2Fcomposite-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspaark%2Fcomposite-utils/lists"}