{"id":16225754,"url":"https://github.com/macfja/value-provider","last_synced_at":"2026-06-18T20:32:07.421Z","repository":{"id":62521356,"uuid":"23284222","full_name":"MacFJA/value-provider","owner":"MacFJA","description":"Get/Set object value with its getter/setter/property/metadata","archived":false,"fork":false,"pushed_at":"2016-02-19T20:48:49.000Z","size":25,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T02:49:33.191Z","etag":null,"topics":["doctrine","getters","mutators","properties","setters"],"latest_commit_sha":null,"homepage":null,"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/MacFJA.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":"2014-08-24T15:17:34.000Z","updated_at":"2017-11-16T15:51:25.000Z","dependencies_parsed_at":"2022-11-02T13:33:07.218Z","dependency_job_id":null,"html_url":"https://github.com/MacFJA/value-provider","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2Fvalue-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2Fvalue-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2Fvalue-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MacFJA%2Fvalue-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MacFJA","download_url":"https://codeload.github.com/MacFJA/value-provider/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773743,"owners_count":20993633,"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":["doctrine","getters","mutators","properties","setters"],"created_at":"2024-10-10T12:46:12.984Z","updated_at":"2026-06-18T20:32:07.356Z","avatar_url":"https://github.com/MacFJA.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ValueProvider #\n\n**ValueProvider** allow you to access to your object's property value without knowing the way the object have defined to do so.\nProperty, getter/setter? Let's `GuessProvider` take care of this.\n\n## Features ##\n\nCan access to object's properties values:\n\n- With direct property access (like `$myObject-\u003emyProperty`)\n    - Works with magic `__get` and `__set` (see below for examples and restriction)\n    - Works with `protected` and `private` properties (see below for examples and restriction)\n- With mutator (getter/setter) (like `$myObject-\u003egetMyProperty()` and `$myObject-\u003esetMyProperty('new value')`)\n    - Works with magic `__call` (see below for examples and restriction)\n- With **doctrine** **`Metadata`** (see below for examples)\n\n## Installation ##\n\n### Composer ###\n\n```\ncomposer require macfja/value-provider\n```\n\n## Examples ##\n\n### Property Access ###\n\nClass Person\n\n```php\nclass Person {\n    public $firstName = '';\n    public $lastName = '';\n}\n```\n\nSomewhere in your code\n\n```php\n$jdoe = new Person();\n$provider = new PropertyProvider();\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\n//Output : \"Hello John Doe\"\n```\n\n#### Notice ####\n\nThe properties have to be public to be accessed.\n(For protected and private properties, see ReflectorProvider)\n\n### Magic `__get` and `__set` properties access ###\n\nClass Person\n\n```php\nclass Person {\n    private $_firstName = 'John';\n    private $_lastName = 'Doe';\n\n    function __get($name) {\n        if (in_array($name, array('firstName', 'lastName')) {\n            $propertyName = '_' . $name;\n            return $this-\u003e$propertyName;\n        }\n\n        throw new \\BadFunctionCallException;\n    }\n\n    function __set($name, $value) {\n        if (in_array($name, array('firstName', 'lastName')) {\n            $propertyName = '_' . $name;\n            $this-\u003e$propertyName = $value;\n            return;\n        }\n\n        throw new \\BadFunctionCallException;\n    }\n\n    function __isset($name) {\n        return in_array($name, array('firstName', 'lastName');\n    }\n}\n```\n\nSomewhere in your code\n\n```php\n$jdoe = new Person();\n$provider = new PropertyProvider();\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\n//Output : \"Hello John Doe\"\n```\n\n#### Notice ####\n\nThe class rely on the `__isset` function to know if the property can be used with `__get` and `__set`\n\n### Mutator Access (getter/setter) ###\n\nClass Person\n\n```php\nclass Person {\n    private $_firstName = '';\n    private $_lastName = '';\n    private $_known = true;\n\n    public function getFirstName() {\n        return $this-\u003e_firstName;\n    }\n    public function setFirstName($value) {\n        $this-\u003e_firstName = $value;\n    }\n    public function getLastName() {\n        return $this-\u003e_lastName;\n    }\n    public function setLastName($value) {\n        $this-\u003e_firstName = $value;\n    }\n    public function isKnown() {\n        return $this-\u003e_known;\n    }\n    public function setKnown($flag) {\n        $this-\u003e_known = $flag;\n    }\n}\n```\n\nSomewhere in your code\n\n```php\n$jdoe = new Person();\n$provider = new MutatorProvider();\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n$provider-\u003esetValue($jdoe, 'known', false);\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\necho ', you are ' . ($provider-\u003egetValue($jdoe, 'known') ? '' : 'un') . 'known';\n//Output : \"Hello John Doe, you are unknown\"\n```\n\n#### Notice ####\n\nThe mutator have to be public to be accessed.\nGetter are search in this order\n\n- getMyProperty\n- isMyProperty\n\n### Magic `__call` mutator access ###\n\nClass Person\n\n```php\nclass Person {\n    private $_firstName = 'John';\n    private $_lastName = 'Doe';\n    private $_known = true;\n\n    function __call($name, $arguments) {\n        switch ($name) {\n            case 'getFirstName':\n                return $this-\u003e_firstName;\n            case 'getLastName':\n                return $this-\u003e_lastName;\n            case 'isKnown':\n                return $this-\u003e_known;\n            case 'setFirstName':\n                $this-\u003e_firstName = $argument[0];\n                return;\n            case 'setLastName':\n                $this-\u003e_lastName = $argument[0];\n                return;\n            case 'setKnown':\n                $this-\u003e_known = $argument[0];\n                return;\n        }\n\n        throw new \\BadFunctionCallException;\n    }\n}\n```\n\nSomewhere in your code\n\n```php\n$jdoe = new Person();\n$provider = new MutatorProvider();\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n$provider-\u003esetValue($jdoe, 'known', false);\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\necho ', you are ' . ($provider-\u003egetValue($jdoe, 'known') ? '' : 'un') . 'known';\n//Output : \"Hello John Doe, you are unknown\"\n```\n\n#### Notice ####\n\nThe class rely on the implementation of the `__call` function:\nthe function MUST throw either `\\BadFunctionCallException` or `\\InvalidArgumentException` if the getter/setter doesn't exist\n\n### Doctrine Metadata ###\n\nSomewhere in your code\n\n```php\n/** @type EntityManager $entityManager */\n$class = 'MyClass';\n$id = 1234;\n\n$provider = new MetadataProvider();\nMetadataProvider::setEntityManager($entityManager);\n\njdoe = $entityManager-\u003efind($class, $id);\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n$provider-\u003esetValue($jdoe, 'known', false);\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\necho ', you are ' . ($provider-\u003egetValue($jdoe, 'known') ? '' : 'un') . 'known';\n//Output : \"Hello John Doe, you are unknown\"\n```\n\n#### Notice ####\n\nYou have to set the Doctrine `EntityManager` to the `MetadataProvider`\n\n### ReflectorProvider ###\n\nClass Person\n\n```php\nclass Person {\n    public $firstName = 'John';\n    protected $lastName = 'Doe';\n    private $known = true;\n}\n```\n\nSomewhere in your code\n\n```php\n$jdoe = new Person();\n$provider = new ReflectorProvider();\n\n// ...\n\n$provider-\u003esetValue($jdoe, 'firstName', 'John');\n$provider-\u003esetValue($jdoe, 'lastName', 'Doe');\n$provider-\u003esetValue($jdoe, 'known', false);\n\n// ...\n\necho 'Hello ' . $provider-\u003egetValue($jdoe, 'firstName') . ' ' . $provider-\u003egetValue($jdoe, 'lastName');\necho ', you are ' . ($provider-\u003egetValue($jdoe, 'known') ? '' : 'un') . 'known';\n//Output : \"Hello John Doe, you are unknown\"\n```\n\n#### Notice ####\n\nThe class can access to private/protected property if you are running PHP 5.3 or newer\n\n### GuessProvider ###\n\nThis class try to access to the value with mutator or property or reflection.\nIt first try the mutator, if not success then try the property, and finish by trying with reflector.\n\n### ChainProvider ###\n\nThis class allow you to try through multiple Provider.\nFor instance, the `GuessProvider` is based on the `ChainProvider`.\n\n## Information ##\n\nThis library respect PSR-1, PSR-2, PSR-4. It have PHPUnit tests for each provider.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacfja%2Fvalue-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmacfja%2Fvalue-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacfja%2Fvalue-provider/lists"}