{"id":15029826,"url":"https://github.com/mschindler83/array-access","last_synced_at":"2025-04-09T20:40:45.515Z","repository":{"id":57021313,"uuid":"240688597","full_name":"mschindler83/array-access","owner":"mschindler83","description":"Library to ease array access handling","archived":false,"fork":false,"pushed_at":"2021-07-28T10:03:20.000Z","size":37,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T07:03:00.184Z","etag":null,"topics":["array","array-helper","array-manipulations","php74"],"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/mschindler83.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":"2020-02-15T10:40:50.000Z","updated_at":"2023-09-04T06:53:40.000Z","dependencies_parsed_at":"2022-08-23T13:50:55.378Z","dependency_job_id":null,"html_url":"https://github.com/mschindler83/array-access","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschindler83%2Farray-access","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschindler83%2Farray-access/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschindler83%2Farray-access/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschindler83%2Farray-access/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mschindler83","download_url":"https://codeload.github.com/mschindler83/array-access/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248109626,"owners_count":21049347,"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":["array","array-helper","array-manipulations","php74"],"created_at":"2024-09-24T20:11:43.031Z","updated_at":"2025-04-09T20:40:45.485Z","avatar_url":"https://github.com/mschindler83.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Array Access\n[![Build Status](https://img.shields.io/travis/mschindler83/array-access/master.svg)](https://travis-ci.org/mschindler83/array-access)\n[![Latest Stable Version](https://img.shields.io/packagist/v/mschindler83/array-access.svg)](https://packagist.org/packages/mschindler83/array-access)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/mschindler83/array-access.svg)](https://scrutinizer-ci.com/g/mschindler83/array-access/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/mschindler83/array-access/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mschindler83/array-access/?branch=master)\n[![Code Intelligence Status](https://scrutinizer-ci.com/g/mschindler83/array-access/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)\n[![Monthly Downloads](https://img.shields.io/packagist/dm/mschindler83/array-access.svg)](https://packagist.org/packages/mschindler83/array-access)\n\n\nLibrary to ease array access handling.\nRequires PHP \u003e= 7.4\n\n## Install\n`composer require mschindler83/array-access`\n\n## Features\n\n - Savely access typed values from a given array\n - Optional JSON schema validation\n - Support for datetime parsing\n - Define your own validation callback when retrieving values\n - Create a new array in form of \"dot annotations\"\n - Easily write values to a specific path of the array\n\n## Usage Examples\n\n### Creating access objects\n#### Create access object from an array and access values\n\n```\n$array = [\n    'key1' =\u003e [\n        'key2' =\u003e [\n            'key3' =\u003e 'the-value'\n        ],\n    ],\n];\n\n$access = ArrayAccess::create($array);\n\ntry {\n    // Get the string value at the given path\n    $value = $access-\u003estring('key1', 'key2', 'key3');\n    \n    // This will fail with an exception because we try to get an integer at the given path\n    $invalidValue = $access-\u003eint('key1', 'key2', 'key3');\n} catch (ArrayAccessFailed $e) {\n    // handle errors\n    echo $e-\u003egetMessage();\n}\n```\n\n#### Create an array from \"dot annotation\"\n```\n$access = ArrayAccess::newFromDotAnnotation(\n    SimpleDotAnnotation::create('key1.key2.2.key3', 'the-value-1'),\n    SimpleDotAnnotation::create('key1.key2.2.key4', 'the-value-2')\n);\n\n$plainArray = $access-\u003edata();\n```\n\nPlain array will contain:\n\n```\nArray\n(\n  [key1] =\u003e Array\n    (\n      [key2] =\u003e Array\n        (\n          [2] =\u003e Array\n            (\n              [key3] =\u003e the-value-1\n              [key4] =\u003e the-value-2\n            )\n        )\n    )\n)\n```\n### Array access with JSON schema validation\n```\n$data = [\n    'key1' =\u003e 'value1',\n    'key2' =\u003e true,\n];\n\n$access = ArrayAccess::createWithJsonSchemaValidation($data, \\file_get_contents('json-schema.json'));\n\n```\n\nJSON schema: \u003cjson-schema.json\u003e\n```\n{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"key1\": {\n      \"type\": \"string\",\n      \"minLength\": 3,\n      \"maxLength\": 64,\n      \"pattern\": \"^[a-zA-Z0-9\\\\-]+(\\\\s[a-zA-Z0-9\\\\-]+)*$\"\n    },\n    \"key2\": {\n      \"type\": \"boolean\"\n    }\n  },\n  \"required\": [\"key1\", \"key2\"],\n  \"additionalProperties\": false\n}\n```\n\nIf validation fails, an `ArrayAccessValidationFailed` exception will be thrown.\nYou can get an `ArrayAccess` object of validation errors by calling the method `errorMapping()` on the exception.\n\n### Access values\n```\n$array = [\n    'root' =\u003e [\n        'string-value' =\u003e 'the-value',\n        'int-value' =\u003e 10,\n        'float-value' =\u003e 9.99,\n        'bool-value' =\u003e true,\n        'array-value' =\u003e [1, 2, 3],\n        'datetime-value' =\u003e '2020-01-01 12:00:00',\n        'object-value' =\u003e new \\stdClass(),\n        'custom' =\u003e 'Choice 1',\n    ],\n];\n\n// Create the access object\n$access = ArrayAccess::create($array);\n\n// This will return the string \"the-value\"\n$access-\u003estring('root', 'string-value');\n\n// This will return the integer \"10\"\n$access-\u003eint('root', 'int-value');\n\n// This will return the float \"9.99\"\n$access-\u003efloat('root', 'float-value');\n\n// This will return the bool \"true\"\n$access-\u003ebool('root', 'bool-value');\n\n// This will return the array \"[1, 2, 3]\"\n$access-\u003earray('root', 'array-value');\n\n// This will return a new ArrayAccess object\n$access-\u003earrayAccess('root', 'array-value');\n\n// This will return a \\DateTimeImmutable object\n$access-\u003edateTimeImmutable('Y-m-d H:i:s', 'root', 'datetime-value');\n\n// This will return a \\DateTime object\n$access-\u003edateTime('Y-m-d H:i:s', 'root', 'datetime-value');\n\n// This will return the \\stdClass object\n$access-\u003eobjectOfType(\\stdClass::class, 'root', 'object-value');\n\n// This will return a mixed, depending on the array content, but only if the custom validation passes\n// In this case it will return the string \"Choice 1\"\n$access-\u003ecallback(\n    function ($value) {\n        return in_array($value, ['Choice 1', 'Choice 2', 'Choice 3']);\n    },\n    'root', 'custom'\n);\n```\n\n### Write to a path\n\n```\n$access = ArrayAccess::create([]);\n$access-\u003ewriteAtPath('the-value', 'at', 'some', 'path');\n$theValue = $access-\u003estring('at', 'some', 'path');\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmschindler83%2Farray-access","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmschindler83%2Farray-access","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmschindler83%2Farray-access/lists"}