{"id":27091697,"url":"https://github.com/dgame/php-object","last_synced_at":"2025-04-06T07:53:28.233Z","repository":{"id":56967364,"uuid":"87475805","full_name":"Dgame/php-object","owner":"Dgame","description":null,"archived":false,"fork":false,"pushed_at":"2019-07-31T20:30:36.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T17:55:39.299Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Dgame.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-06T21:16:51.000Z","updated_at":"2019-07-31T20:30:38.000Z","dependencies_parsed_at":"2022-08-21T11:20:29.477Z","dependency_job_id":null,"html_url":"https://github.com/Dgame/php-object","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dgame","download_url":"https://codeload.github.com/Dgame/php-object/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451629,"owners_count":20940946,"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":[],"created_at":"2025-04-06T07:53:27.786Z","updated_at":"2025-04-06T07:53:28.227Z","avatar_url":"https://github.com/Dgame.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-object\r\n\r\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Dgame/php-object/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Dgame/php-object/?branch=master)\r\n[![codecov](https://codecov.io/gh/Dgame/php-object/branch/master/graph/badge.svg)](https://codecov.io/gh/Dgame/php-object)\r\n[![Build Status](https://scrutinizer-ci.com/g/Dgame/php-object/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Dgame/php-object/build-status/master)\r\n[![StyleCI](https://styleci.io/repos/87475805/shield?branch=master)](https://styleci.io/repos/87475805)\r\n[![Build Status](https://travis-ci.org/Dgame/php-object.svg?branch=master)](https://travis-ci.org/Dgame/php-object)\r\n\r\n----\r\nAn intelligent facade around your object to protect it from any harm.\r\n----\r\n\r\n### hasMethod\r\n```php\r\n$facade = new ObjectFacade(new Exception());\r\n$this-\u003eassertTrue($facade-\u003ehasMethod('getMessage'));\r\n$this-\u003eassertFalse($facade-\u003ehasMethod('foo'));\r\n```\r\n### hasProperty\r\n```php\r\n$facade = new ObjectFacade(new Exception());\r\nforeach (['message', 'code', 'file', 'line'] as $property) {\r\n    $this-\u003eassertTrue($facade-\u003ehasProperty($property));\r\n}\r\n$this-\u003eassertFalse($facade-\u003ehasProperty('foo'));\r\n```\r\n\r\n### getPropertyByName\r\n```php\r\n$facade = new ObjectFacade(new Exception());\r\nforeach (['message', 'code', 'file', 'line'] as $name) {\r\n    $property = $facade-\u003egetPropertyByName($name);\r\n\r\n    $this-\u003eassertNotNull($property);\r\n    $this-\u003eassertEquals($name, $property-\u003egetName());\r\n    $this-\u003eassertNotEquals(0, ReflectionProperty::IS_PROTECTED \u0026 $property-\u003egetModifiers());\r\n}\r\n```\r\n\r\n### getMethodByName\r\n```php\r\n$facade = new ObjectFacade(new Exception());\r\n$method = $facade-\u003egetMethodByName('getMessage');\r\n\r\n$this-\u003eassertNotNull($method);\r\n$this-\u003eassertEquals('getMessage', $method-\u003egetName());\r\n$this-\u003eassertNotEquals(0, (ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) \u0026 $method-\u003egetModifiers());\r\n```\r\n\r\n### getGetterMethod\r\n```php\r\n$facade = new ObjectFacade(new Exception());\r\n$method = $facade-\u003egetGetterMethod('message');\r\n\r\n$this-\u003eassertNotNull($method);\r\n$this-\u003eassertEquals('getMessage', $method-\u003egetName());\r\n```\r\n\r\n### getSetterMethod\r\n```php\r\n$facade = new ObjectFacade(\r\n    new class() {\r\n        public function setFoo()\r\n        {\r\n        }\r\n    }\r\n);\r\n\r\n$method = $facade-\u003egetSetterMethod('foo');\r\n\r\n$this-\u003eassertNotNull($method);\r\n$this-\u003eassertEquals('setFoo', $method-\u003egetName());\r\n```\r\n\r\n### getValueByMethod\r\n```php\r\n$exception = new Exception('Test');\r\n$facade    = new ObjectFacade($exception);\r\n\r\n$this-\u003eassertEquals($exception, $facade-\u003egetObject());\r\n$this-\u003eassertEquals('Test', $facade-\u003egetValueByMethod('message'));\r\n$this-\u003eassertEquals($exception-\u003egetMessage(), $facade-\u003egetValueByMethod('message'));\r\n$this-\u003eassertEquals($exception-\u003egetFile(), $facade-\u003egetValueByMethod('file'));\r\n$this-\u003eassertEquals($exception-\u003egetLine(), $facade-\u003egetValueByMethod('line'));\r\n$this-\u003eassertNull($facade-\u003egetValueByMethod('unknown'));\r\n```\r\n\r\n### getValueByProperty\r\n```php\r\n$facade = new ObjectFacade(\r\n    new class() {\r\n        public $foo = 42;\r\n        public $bar = Exception::class;\r\n    }\r\n);\r\n\r\n$this-\u003eassertEquals(42, $facade-\u003egetValueByProperty('foo'));\r\n$this-\u003eassertEquals(Exception::class, $facade-\u003egetValueByProperty('bar'));\r\n$this-\u003eassertNull($facade-\u003egetValueByProperty('unknown'));\r\n\r\n$facade = new ObjectFacade(new Exception());\r\n\r\n$this-\u003eassertNull($facade-\u003egetValueByProperty('line')); // not a public property\r\n$this-\u003eassertNull($facade-\u003egetValueByProperty('file')); // not a public property\r\n```\r\n\r\n### setValueByMethod\r\n```php\r\n$facade = new ObjectFacade(\r\n  new class() {\r\n      private $foo = 42;\r\n      private $bar;\r\n\r\n      public function setFoo(int $foo)\r\n      {\r\n          $this-\u003efoo = $foo;\r\n      }\r\n\r\n      public function setFooBar()\r\n      {\r\n          $this-\u003efoo = 1;\r\n          $this-\u003ebar = 2;\r\n      }\r\n\r\n      public function getFoo(): int\r\n      {\r\n          return $this-\u003efoo;\r\n      }\r\n\r\n      public function setBar(int $bar = null)\r\n      {\r\n          $this-\u003ebar = $bar;\r\n      }\r\n\r\n      public function getBar()\r\n      {\r\n          return $this-\u003ebar;\r\n      }\r\n  }\r\n);\r\n\r\n$this-\u003eassertEquals(42, $facade-\u003egetValueByMethod('foo'));\r\n$facade-\u003esetValueByMethod('foo', 23);\r\n$this-\u003eassertEquals(23, $facade-\u003egetValueByMethod('foo'));\r\n$facade-\u003esetValueByMethod('foo', null); // \"setFoo\" does not accept null =\u003e keep the old value\r\n$this-\u003eassertEquals(23, $facade-\u003egetValueByMethod('foo'));\r\n$facade-\u003esetValueByMethod('foo', 'abc'); // \"setFoo\" does not accept a string =\u003e keep the old value\r\n$this-\u003eassertEquals(23, $facade-\u003egetValueByMethod('foo'));\r\n\r\n$this-\u003eassertNull($facade-\u003egetValueByMethod('bar'));\r\n$facade-\u003esetValueByMethod('bar', 1337);\r\n$this-\u003eassertEquals(1337, $facade-\u003egetValueByMethod('bar'));\r\n$facade-\u003esetValueByMethod('bar', null);\r\n$this-\u003eassertNull($facade-\u003egetValueByMethod('bar'));\r\n\r\n$facade-\u003esetValueByMethod('foobar', uniqid());\r\n$this-\u003eassertEquals(1, $facade-\u003egetValueByMethod('foo'));\r\n$this-\u003eassertEquals(2, $facade-\u003egetValueByMethod('bar'));\r\n```\r\n\r\n### setValueByProperty\r\n```php\r\n$facade = new ObjectFacade(\r\n    new class() {\r\n        public $foo = 42;\r\n    }\r\n);\r\n\r\n$this-\u003eassertEquals(42, $facade-\u003egetValueByProperty('foo'));\r\n$this-\u003eassertEquals($facade-\u003egetObject()-\u003efoo, $facade-\u003egetValueByProperty('foo'));\r\n$facade-\u003esetValueByProperty('foo', 23);\r\n$this-\u003eassertEquals(23, $facade-\u003egetValueByProperty('foo'));\r\n$this-\u003eassertEquals($facade-\u003egetObject()-\u003efoo, $facade-\u003egetValueByProperty('foo'));\r\n$facade-\u003esetValueByProperty('foo', null);\r\n$this-\u003eassertNull($facade-\u003egetValueByProperty('foo'));\r\n$this-\u003eassertEquals($facade-\u003egetObject()-\u003efoo, $facade-\u003egetValueByProperty('foo'));\r\n$facade-\u003esetValueByProperty('foo', 1337);\r\n$this-\u003eassertEquals(1337, $facade-\u003egetValueByProperty('foo'));\r\n$this-\u003eassertEquals($facade-\u003egetObject()-\u003efoo, $facade-\u003egetValueByProperty('foo'));\r\n```\r\n\r\n### setValue / getValue\r\n```php\r\n$facade = new ObjectFacade(\r\n    new class() {\r\n        public $foo = 42;\r\n    }\r\n);\r\n\r\n$facade-\u003esetValue('foo', 3537);\r\n$this-\u003eassertEquals(3537, $facade-\u003egetValue('foo'));\r\n```\r\n\r\n```php\r\nnew class() {\r\n    private $foo = 42;\r\n\r\n    public function setFoo(int $foo)\r\n    {\r\n        $this-\u003efoo = $foo;\r\n    }\r\n}\r\n\r\n$facade-\u003esetValue('foo', 3537);\r\n$this-\u003eassertEquals(3537, $facade-\u003egetValue('foo'));\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgame%2Fphp-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-object/lists"}