{"id":22900441,"url":"https://github.com/kapitanluffy/array-legacy","last_synced_at":"2025-04-01T04:57:24.655Z","repository":{"id":57004413,"uuid":"156350481","full_name":"kapitanluffy/array-legacy","owner":"kapitanluffy","description":"A class for converting arrays into fault-tolerant objects","archived":false,"fork":false,"pushed_at":"2018-11-12T10:12:07.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-07T03:25:37.588Z","etag":null,"topics":["arrays","legacy-code","php"],"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/kapitanluffy.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":"2018-11-06T08:25:00.000Z","updated_at":"2018-11-12T10:12:09.000Z","dependencies_parsed_at":"2022-08-21T12:10:50.899Z","dependency_job_id":null,"html_url":"https://github.com/kapitanluffy/array-legacy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kapitanluffy%2Farray-legacy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kapitanluffy%2Farray-legacy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kapitanluffy%2Farray-legacy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kapitanluffy%2Farray-legacy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kapitanluffy","download_url":"https://codeload.github.com/kapitanluffy/array-legacy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246586048,"owners_count":20801026,"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":["arrays","legacy-code","php"],"created_at":"2024-12-14T01:20:05.744Z","updated_at":"2025-04-01T04:57:24.634Z","avatar_url":"https://github.com/kapitanluffy.png","language":"PHP","readme":"# ArrayLegacy\n\nA class for converting arrays into fault-tolerant objects.\nIts aim is to access arrays as usual but without checking if a key is \"set\".\n\n```php\n// No more checking if the key is set\nif (isset($foo['baz')) {\n    $baz = $foo['baz'];\n}\n\n// No more setting a default value\nif (isset($foo['bar') == false) {\n    $foo['bar'] = 'default value';\n}\n```\n#### What are \"legacy\" arrays?\nThese are arrays that are used like objects. After working with \"legacy\" codes, I have seen many of them use arrays to store data.\nThey (the developers) did not bother creating objects that would \"model\" their data because there's \"not enough time\" or \"too much refactoring\".\n\nWith ArrayLegacy, we can avoid this..\n\u003e **NOTICE** Undefined index: Foo on line number 5\n\n\n#### Usage\n\nConvert an array into an ArrayLegacy\n```php\n$foo = [\n    \"title\" =\u003e \"FooBar\",\n    \"in_stock\" =\u003e 1000,\n    \"price\" =\u003e 100\n];\n\n$foo = ArrayLegacy::make($foo);\n```\n\nUse like an array\n```php\n$foo['title'] = \"FooBaz\";\n// Output: FooBaz\necho $foo['title'];\n```\n\nUse a getter/setter method\n```php\n$foo-\u003esetInStock(999);\n// Output: 999\necho $foo-\u003egetInStock();\n```\n\nUse get/set methods\n```php\n$foo-\u003eset(\"price\", 99);\n// Output: 99\necho $foo-\u003eget(\"price\");\n```\n\nWith `get()` you can also specify a default value if the key does not exist\n```php\n// Output: This is a default description\necho $foo-\u003eget(\"description\", \"This is a default description\");\n```\n\nRemove a key using `unset()`\n```php\nunset($foo[\"title\"]);\n// Output: null\necho $foo[\"title\"];\n```\n\nUse a mutator when accessing/modifying a key\n```php\n// Note that mutators should directly access `attributes` property\nclass Foo extends ArrayLegacy {\n    public function setTitle($value) {\n        $this-\u003eattributes['title'] = strtolower($value);\n    }\n\n    public function getTitle() {\n        return ucwords($this-\u003eattributes['title']);\n    }\n}\n\n$foo = new Foo($foo);\necho $foo-\u003esetTitle(\"This is a test\");\n\n// Output: This Is A Test\necho $foo-\u003egetTitle();\n```\n\nUsing PHP array functions\n\nYou can use [array functions](https://secure.php.net/manual/en/ref.array.php) by calling it directly in the object.\n- Functions with an `array_` prefix can be called without the prefix.\n- Functions returning the array type will return a new `ArrayLegacy` instance.\n- Functions that pass the array by reference will modify the current instance.\n\nUnsupported functions are the following:\n- array_combine\n- array_fill_keys\n- array_fill\n- range\n- list\n- extract\n\n```php\n$foo = new Foo($foo);\n\n// calls array_keys, returns a new ArrayLegacy instance of keys\n$foo-\u003ekeys('bar');\n\n// calls shuffle and applies it to the current instance\n$foo-\u003eshuffle();\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkapitanluffy%2Farray-legacy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkapitanluffy%2Farray-legacy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkapitanluffy%2Farray-legacy/lists"}