{"id":20002804,"url":"https://github.com/babenkoivan/fluent-array","last_synced_at":"2025-05-04T15:34:32.036Z","repository":{"id":62490996,"uuid":"134868884","full_name":"babenkoivan/fluent-array","owner":"babenkoivan","description":"The fluent array library provides you with a convenient chainable interface. If you like object-oriented syntax or you just want to have more readable array declaration, the fluent array is at your service.","archived":false,"fork":false,"pushed_at":"2018-12-08T11:53:22.000Z","size":42,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-08T08:05:22.959Z","etag":null,"topics":["array","array-helper","array-object","associative-array","chainable","fluent-interface"],"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/babenkoivan.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-05-25T14:46:12.000Z","updated_at":"2020-04-03T10:04:08.000Z","dependencies_parsed_at":"2022-11-02T11:15:53.094Z","dependency_job_id":null,"html_url":"https://github.com/babenkoivan/fluent-array","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Ffluent-array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Ffluent-array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Ffluent-array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babenkoivan%2Ffluent-array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/babenkoivan","download_url":"https://codeload.github.com/babenkoivan/fluent-array/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252357139,"owners_count":21735086,"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-object","associative-array","chainable","fluent-interface"],"created_at":"2024-11-13T05:23:08.546Z","updated_at":"2025-05-04T15:34:31.789Z","avatar_url":"https://github.com/babenkoivan.png","language":"PHP","funding_links":["https://www.paypal.me/ivanbabenko"],"categories":[],"sub_categories":[],"readme":"# FluentArray\n\n[![Packagist](https://img.shields.io/packagist/v/babenkoivan/fluent-array.svg)](https://packagist.org/packages/babenkoivan/fluent-array)\n[![Packagist](https://img.shields.io/packagist/dt/babenkoivan/fluent-array.svg)](https://packagist.org/packages/babenkoivan/fluent-array)\n[![Build Status](https://travis-ci.com/babenkoivan/fluent-array.svg?branch=master)](https://travis-ci.com/babenkoivan/fluent-array)\n[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/babenkoivan/fluent-array)\n[![Donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.me/ivanbabenko)\n\n* [Introduction](#introduction)\n* [Installation](#installation)\n* [Configuration](#configuration) \n* [Macros](#macros)\n* [Fixed methods](#fixed-methods) \n* [Dynamic methods](#dynamic-methods) \n* [Implemented interfaces](#implemented-interfaces) \n* [Code Formatting](#code-formatting)\n\n## Introduction\n\nThe fluent array library provides you with a convenient chainable interface. \nIf you like object-oriented syntax or you just want to have more readable array declaration,\nthe fluent array is at your service.  \n\n#### Basic usage\n\n```php\n$order = (new FluentArray())\n    -\u003euser()\n        -\u003eid(1)\n        -\u003ename('John')\n    -\u003eend()\n    -\u003ecoupon('SALE10')\n    -\u003estatus('delivered')\n    -\u003eproducts()\n        -\u003epush()\n            -\u003eid(1)\n            -\u003ename('iPhone X')\n            -\u003eprice(1200)\n        -\u003eend()\n        -\u003epush()\n            -\u003eid(2)\n            -\u003ename('Beats By Dre Studio3')\n            -\u003eprice(360)\n        -\u003eend()\n    -\u003eend();\n```\n\nIf we convert the fluent array to an associative array, by calling `$order-\u003etoArray()`,\nwe will get the following output:\n\n```php\n[\n    'user' =\u003e [\n        'id' =\u003e 1,\n        'name' =\u003e 'John'\n    ],\n    'coupon' =\u003e 'SALE10',\n    'status' =\u003e 'delivered',\n    'products' =\u003e [\n        [\n            'id' =\u003e 1,\n            'name' =\u003e 'iPhone X',\n            'price' =\u003e 1200\n        ],\n        [\n            'id' =\u003e 2,\n            'name' =\u003e 'Beats By Dre Studio3'\n            'price' =\u003e 360\n        ]\n    ]\n]\n```\n\n#### Storage array\n\nEvery time you call [set](#set) or [get]($get), or any other method, that modifies or retrieves the state, \nyou update the internal storage of fluent array.\n\n```php\n$fluentArray = new FluentArray();\n\n// we set the key `one` and the corresponding value `1` in the storage \n$fluentArray-\u003eset('one', 1);\n    \n// we get the value, that corresponds the key `one` from the storage\n$fluentArray-\u003eget('one');\n```\n\n## Installation\n\nUse composer to install the library:\n\n```bash\ncomposer require babenkoivan/fluent-array\n```\n\n## Configuration\n\nThe configuration allows you to change a fluent array [behavior](#naming-strategies) and add [new functionality](#macros).\n\n#### Local scope\n\nTo configure a specific fluent array instance use local scope.\n\n```php\n$config = (clone FluentArray::globalConfig())\n    -\u003enamingStrategy(new CamelCaseStrategy());\n\n$fluentArray = (new FluentArray($config))\n    -\u003eone(1)\n    -\u003etwo(2);\n\n// alternatively you can set configuration, using the `config` method\n$fluentArray = (new FluentArray())\n    -\u003econfig($config)\n    -\u003eone(1)\n    -\u003etwo(2);\n\n$fluentArray-\u003eall();\n// ['One' =\u003e 1, 'Two' =\u003e 2]\n```\n \n#### Global scope\n\nTo configure all fluent arrays use global scope.\n\n```php\n$globalConfig = FluentArray::globalConfig();\n\n$globalConfig-\u003enamingStrategy(new CamelCaseStrategy());\n\n$fluentArray = (new FluentArray())\n    -\u003eone(1)\n    -\u003etwo(2);\n    \n$fluentArray-\u003eall();\n// ['One' =\u003e 1, 'Two' =\u003e 2]    \n```\n\n#### Macros\n\nYou can use macros to extend a fluent array functionality. \nIt can be done via configuration in [global](#global-scope) or [local scope](#local-scope).\n\n```php\n$globalConfig = FluentArray::globalConfig();\n\n$globalConfig\n    -\u003emacros()\n        -\u003eformat(function (string $key, int $decimals = 0) {\n            $value = $this-\u003eget($key);\n        \n            if (is_numeric($value)) {\n                return number_format($value, $decimals);\n            } else {\n                return $value;\n            }\n        })\n    -\u003eend();\n    \n$fluentArray = (new FluentArray())\n    -\u003eset('one', 10.567)\n    -\u003eset('two', 2.89);\n    \n$fluentArray-\u003eformat('one', 2);\n// 10.57\n\n$fluentArray-\u003eformat('two', 1);\n// 2.9    \n```\n\n#### Naming strategies\n\nNaming strategies describe key transformation in [dynamic methods](#dynamic-methods).\n\nFor example, we want all our keys to be underscored in [the storage array](#storage-array). \n\n```php\n$config = (clone FluentArray::globalConfig())\n    -\u003enamingStrategy(new UnderscoreStrategy());\n\n$fluentArray = (new FluentArray($config))\n    -\u003efirstValue(1)\n    -\u003esecondValue(2);\n\n$fluentArray-\u003eall();\n// ['first_value' =\u003e 1, 'second_value' =\u003e 2]\n```\n\nNow we want them to be camel-cased.\n\n```php\n$config = (clone FluentArray::globalConfig())\n    -\u003enamingStrategy(new CamelCaseStrategy());\n\n$fluentArray = (new FluentArray($config))\n    -\u003efirstValue(1)\n    -\u003esecondValue(2);\n\n$fluentArray-\u003eall();\n// ['first_value' =\u003e 1, 'second_value' =\u003e 2]\n```\n\nThe supported naming strategies are:\n\nStrategy | Example\n---------|--------\nCamelCaseStrategy | `MyValue`\nNullStrategy | `myValue`\nUnderscoreStrategy | `my_value`\n\nThe default naming strategy is `UnderscoreStrategy`. \n\n## Fixed methods\n\n* [all](#all)\n* [clean](#clean)\n* [config](#config)\n* [count](#count)\n* [each](#each)\n* [filter](#filter)\n* [first](#first)\n* [fromArray](#fromarray)\n* [fromJson](#fromjson)\n* [get](#get)\n* [globalConfig](#globalconfig)\n* [has](#has)\n* [keys](#keys)\n* [krsort](#krsort)\n* [ksort](#ksort)\n* [last](#last)\n* [map](#map)\n* [pluck](#pluck)\n* [pushWhen](#pushwhen)\n* [push](#push)\n* [rsort](#rsort)\n* [setWhen](#setwhen)\n* [set](#set)\n* [sort](#sort)\n* [toArray](#toarray)\n* [toJson](#tojson)\n* [unset](#unset)\n* [usort](#usort)\n* [values](#values)\n* [when](#when)\n\n#### all\n\nThe `all` method returns [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eall();    \n// ['one' =\u003e 1, 'two' =\u003e 2]\n```\n\n#### clean\n\nThe `clean` method removes all keys and values from [the storage array](#storage-array). \n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n\n$fluentArray-\u003eall();    \n// ['one' =\u003e 1, 'two' =\u003e 2]\n\n$fluentArray-\u003eclean()-\u003eall();    \n// []    \n```\n\n#### config\n\nThe `config` method allows you to set or retrieve [local configuration](#local-scope).\n\n```php\n$config = (new FluentArray())\n    -\u003eset('naming_strategy', new NullStrategy());\n    \n$fluentArray = (new FluentArray())\n    -\u003econfig($config);\n    \n$fluentArray-\u003econfig()-\u003eget('naming_strategy');\n// instance of NullStrategy       \n```\n\n#### count\n\nThe `count` method returns the amount of values in [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2)\n    -\u003eset('three', 3);\n\n$fluentArray-\u003ecount();\n// 3\n```\n\n#### each\n\nThe `each` method iterates over the values in [the storage array](#storage-array).\n\n```php\n$odds = [];\n\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2)\n    -\u003eset('three', 3)\n    -\u003eset('four', 4);\n\n$fluentArray-\u003eeach(function ($value, $key) use (\u0026$odds) {\n    if ($value % 2 !== 0) {\n        $odds[] = $value;\n    }\n});\n\n$odds;\n// [1, 3]\n```\n\nTo stop the iteration return `false` from the callback.\n\n```php\n$counter = 0;\n\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2)\n    -\u003eset('three', 3);\n\n$fluentArray-\u003eeach(function ($value, $key) use (\u0026$counter) {\n    if ($value \u003e 1) {\n        return false;\n    }\n    \n    $counter++;\n});\n\n$counter;\n// 1\n```\n\n#### filter\n\nThe `filter` method filters [the storage array](#storage-array) using the given callback.\nReturn `false` from the callback to remove a value.\n\n```php\n$sourceFluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$filteredFluentArray = $sourceFluentArray-\u003efilter(function ($value, $key) {\n    return $value \u003e 1;\n});\n\n$filteredFluentArray-\u003eall();\n// ['two' =\u003e 2]    \n```  \n\nIf callback is not specified, all values, that can be converted to `false` will be removed.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('zero', 0)\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003efilter()-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]    \n```\n\n#### first\n\nThe `first` method retrieves the first value from [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003efirst();\n// 1        \n```\n\n#### fromArray\n\nThe `fromArray` method converts an array to a fluent array.\n\n```php\n$array = ['one' =\u003e 1, 'two' =\u003e 2];\n\n$fluentArray = FluentArray::fromArray($array);\n\n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]\n```\n\n#### fromJson\n\nThe `fromJson` method converts JSON to a fluent array.\n\n```php\n$json = '{\"one\":1,\"two\":2}';\n\n$fluentArray = FluentArray::fromJson($json);\n\n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]\n```\n\n#### get\n\nThe `get` method retrieves the value from [the storage array](#storage-array), \nthat corresponds the given key.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eget('two');\n// 2    \n```\n\n#### globalConfig\n\nThe `globalConfig` method allows you to set or retrieve [global configuration](#global-scope).\n\n```php\n$globalConfig = (new FluentArray())\n    -\u003eset('naming_strategy', new NullStrategy());\n   \nFluentArray::globalConfig($globalConfig);\n\nFluentArray::globalConfig()-\u003eget('naming_strategy');\n// instance of NullStrategy       \n``` \n\n#### has\n\nThe `has` method checks if the given key exists in [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003ehas('one');\n// true\n\n$fluentArray-\u003ehas('three');\n// false    \n```\n\n#### keys\n\nThe `keys` method retrieves all the keys from [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003ekeys();\n// ['one', 'two'] \n```\n\n#### krsort\n\nThe `krsort` method sorts [the storage array](#storage-array) by keys in descending order.\nYou can specify sort flags as a first argument.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('b', 1)\n    -\u003eset('a', 2)\n    -\u003eset('c', 3);\n    \n$fluentArray-\u003ekrsort(SORT_STRING)-\u003eall();\n// ['c' =\u003e 3, 'b' =\u003e 1, 'a' =\u003e 2] \n```\n\n#### ksort\n\nThe `ksort` method sorts [the storage array](#storage-array) by keys in ascending order.\nYou can specify sort flags as a first parameter.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('b', 1)\n    -\u003eset('a', 2)\n    -\u003eset('c', 3);\n    \n$fluentArray-\u003eksort(SORT_STRING)-\u003eall();\n// ['a' =\u003e 2, 'b' =\u003e 1, 'c' =\u003e 3] \n```\n\n#### last\n\nThe `last` method retrieves the last value from [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003elast();\n// 2        \n```\n\n#### map\n\nThe `map` method applies the given callback to all values in [the storage array](#storage-array) \nand returns a new fluent array. \n\n```php\n$sourceFluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n\n$resultFluentArray = $sourceFluentArray-\u003emap(function ($value, $key) {\n    return $value * 10;\n});\n\n$resultFluentArray-\u003eall();\n// ['one' =\u003e 10, 'two' =\u003e 20]\n```\n\n#### pluck\n\nThe `pluck` method extracts values with the given key, from child fluent arrays to a new fluent array.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', (new FluentArray())-\u003eset('id', 1))\n    -\u003eset('two', (new FluentArray())-\u003eset('id', 2));\n    \n$fluentArray-\u003epluck('id')-\u003eall();\n// [1, 2]   \n```\n\n#### push\n\nThe `push` method appends the given value to [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003epush(1)\n    -\u003epush(2);\n    \n$fluentArray-\u003eall();\n// [1, 2]    \n```\n\nAnother way of using the `push` method:\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003epush()\n        -\u003eone(1)\n        -\u003etwo(2)\n    -\u003eend()\n    -\u003epush()\n        -\u003ethree(3)\n    -\u003eend();\n    \n$fluentArray-\u003etoArray();\n// [['one' =\u003e 1, 'two' =\u003e 2], ['three' =\u003e 3]]    \n```\n\n#### pushWhen\n\nThe `pushWhen` method appends the given value to [the storage array](#storage-array), \nif the first argument is equivalent to `true`.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003epushWhen(true, 1)\n    -\u003epushWhen(false, 2)\n    -\u003epushWhen(function () { return true; }, 3);\n    \n$fluentArray-\u003eall();\n// [1, 3]    \n```\n\nAnother way of using the `pushWhen` method:\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003epushWhen(true)\n        -\u003eone(1)\n    -\u003eend(false)\n    -\u003epushWhen(false)\n        -\u003etwo(2)\n    -\u003eend()\n    -\u003epushWhen(function () { return true; })\n        -\u003ethree(3)\n    -\u003eend();\n    \n$fluentArray-\u003etoArray();\n// [['one' =\u003e 1], ['three' =\u003e 3]]    \n```\n\n#### rsort\n\nThe `rsort` method sorts [the storage array](#storage-array) in descending order.\nYou can specify sort flags as a first parameter.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('three', 3)\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003ersort(SORT_NUMERIC)-\u003eall();\n// ['three' =\u003e 3, 'two' =\u003e 2, 'one' =\u003e 1]    \n```\n\n#### set\n\nThe `set` method sets the given key and value in [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]    \n```\n\n#### setWhen\n\nThe `setWhen` method sets the given key and value in [the storage array](#storage-array),\nif the first argument is equivalent to `true`.  \n\n```php\n$fluentArray = (new FluentArray())\n    -\u003esetWhen(true, 'one', 1)\n    -\u003esetWhen(false, 'two', 2)\n    -\u003esetWhen(function () { return true; }, 'three', 3);\n    \n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'three' =\u003e 3]    \n```\n\n#### sort\n\nThe `sort` method sorts [the storage array](#storage-array) in ascending order.\nYou can specify sort flags as a first parameter.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('three', 3)\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003esort(SORT_NUMERIC)-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2, 'three' =\u003e 3]    \n```\n\n#### toArray\n\nThe `toArray` method converts a fluent array to an array.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n\n$fluentArray-\u003etoArray();\n// ['one' =\u003e 1, 'two' =\u003e 2]\n```\n\n#### toJson\n\nThe `toJson` method converts a fluent array to JSON.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n\n$fluentArray-\u003etoJson();\n// \"{\"one\":1,\"two\":2}\"\n```\n\n#### unset\n\nThe `unset` method removes [the storage array](#storage-array) value by the given key.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eunset('one')-\u003eall();\n// ['two' =\u003e 2]    \n```\n\n#### usort\n\nThe `usort` method sorts [the storage array](#storage-array) using the given comparison function.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('three', 3)\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eusort(function ($a, $b) {\n    return $a \u003c=\u003e $b;\n});    \n    \n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2, 'three' =\u003e 3]    \n```\n\n#### values\n\nThe `values` method retrieves all the values from [the storage array](#storage-array).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$fluentArray-\u003eall();\n// [1, 2]    \n```\n\n#### when\n\nThe `when` method executes the given callback, if the first argument is equivalent to `true`.  \n\n```php\n$fluentArray = new FluentArray();\n\n$fluentArray-\u003ewhen(true, function () use ($fluentArray) {\n    $fluentArray-\u003eset('one', 1);\n});\n\n$fluentArray-\u003ewhen(false, function () use ($fluentArray) {\n    $fluentArray-\u003eset('two', 2);\n});\n\n$fluentArray-\u003ewhen(\n    function () {\n        return true;\n    }, \n    function () use ($fluentArray) {\n        $fluentArray-\u003eset('three', 3);\n    }\n);\n\n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'three' =\u003e 3]\n```\n\nYou can specify a default callback, that will be executed,\nif the first argument is equivalent to `false`.\n\n```php\n$fluentArray = new FluentArray();\n\n$fluentArray-\u003ewhen(\n    false, \n    function () use ($fluentArray) {\n        $fluentArray-\u003eset('one', 1);\n    },\n    function () use ($fluentArray) {\n        $fluentArray-\u003eset('two', 2);\n    }\n);\n\n$fluentArray-\u003eall();\n// ['two' =\u003e 2]\n```\n\n## Dynamic methods\n\n* [Dynamic setter](#dynamic-setter)\n* [Dynamic getter](#dynamic-getter)\n* [Dynamic has](#dynamic-has)\n* [Dynamic pluck](#dynamic-pluck)\n* [Dynamic unset](#dynamic-unset)\n\n#### Dynamic setter\n\nYou can also set a key-value pair in [the storage array](#storage-array) using a dynamic setter.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone(1)\n    -\u003etwo(2);\n    \n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]    \n```\n\nIf you want to set the key, that is reserved for a method name, you can escape it.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003e{'\\set'}(1)\n    -\u003e{'\\get'}(2);\n    \n$fluentArray-\u003eall();\n// ['set' =\u003e 1, 'get' =\u003e 2]    \n```\n\nAdd `When` to set the given value if the first argument is equivalent to `true`.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eoneWhen(true, 1)\n    -\u003etwoWhen(false, 2)\n    -\u003ethreeWhen(function () { return true; }, 3);\n    \n$fluentArray-\u003eall();\n// ['one' =\u003e 1, 'three' =\u003e 3]    \n```\n\nYou can also chain creation of child fluent arrays.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone()\n        -\u003etwo(3)\n    -\u003eend()\n    -\u003ethree()\n        -\u003efour(4)\n        -\u003efive(5)\n    -\u003eend();\n    \n$fluentArray-\u003etoArray();\n// ['one' =\u003e ['two' =\u003e 2], 'three' =\u003e ['four' =\u003e 4, 'five' =\u003e 5]]    \n``` \n\n#### Dynamic getter\n\nTo retrieve a value from [the storage array](#storage-array) you can use a dynamic getter.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone(1)\n    -\u003etwo(2);\n    \n$fluentArray-\u003etwo();\n// 2    \n```\n\n#### Dynamic has\n\nTo check if a key exists in [the storage array](#storage-array) you can use a dynamic `has` method.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone(1)\n    -\u003etwo(2);\n    \n$fluentArray-\u003ehasOne();\n// true\n\n$fluentArray-\u003ehasThree();\n// false    \n```\n\n#### Dynamic pluck\n\nTo extract values from child fluent arrays you can use a dynamic `pluck` method.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone()\n        -\u003eid(1)\n    -\u003eend()\n    -\u003etwo()\n        -\u003eid(2)\n    -\u003eend();\n    \n$fluentArray-\u003epluckId()-\u003eall();\n// [1, 2]   \n```\n\n#### Dynamic unset\n\nTo remove a value from [the storage array](#storage-array) you can use a dynamic `unset` method.\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone(1)\n    -\u003etwo(2);\n    \n$fluentArray-\u003eunsetOne()-\u003eall();\n// ['two' =\u003e 2]    \n```\n\n## Implemented interfaces\n\n* [Countable](#countable)\n* [Serializable](#serializable)\n* [ArrayAccess](#arrayaccess)\n* [IteratorAggregate](#iteratoraggregate)\n\n#### Countable \n\nThe `Countable` interface provides the `count` method support.\n[See more here](http://php.net/manual/en/class.countable.php).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \ncount($fluentArray);\n// 2\n```\n\n#### Serializable \n\nThe `Serializable` interface provides serialization support.\n[See more here](http://php.net/manual/en/class.serializable.php).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \n$serialized = serialize($fluentArray);\n$unserialized = unserialize($serialized);\n\n$unserialized-\u003eall();\n// ['one' =\u003e 1, 'two' =\u003e 2]    \n```\n\n#### ArrayAccess \n\nThe `ArrayAccess` interface provides array access.\n[See more here](http://php.net/manual/en/class.arrayaccess.php).\n\n```php\n$fluentArray = new FluentArray();\n\n$fluentArray['one'] = 1;\n$fluentArray['two'] = 2;\n\n$fluentArray['two'];\n// 2\n```\n\n#### IteratorAggregate\n\nThe `IteratorAggregate` interface enables iteration over [the storage array](#storage-array).\n[See more here](http://php.net/manual/en/class.iteratoraggregate.php).\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eset('one', 1)\n    -\u003eset('two', 2);\n    \nforeach ($fluentArray as $key =\u003e $value) {\n    $fluentArray-\u003eset($key, $value * 10);\n}    \n\n$fluentArray-\u003eall();\n// ['one' =\u003e 10, 'two' =\u003e 20]\n```\n\n## Code formatting\n\nIf you use `PhpStorm` and code auto formatting, you will likely face the issue, that the following code:\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone()\n        -\u003eid(1)\n    -\u003eend()\n    -\u003etwo()\n        -\u003eid(2)\n    -\u003eend();\n```  \n\nWill be transformed by `PhpStorm` to:\n\n```php\n$fluentArray = (new FluentArray())\n    -\u003eone()\n    -\u003eid(1)\n    -\u003eend()\n    -\u003etwo()\n    -\u003eid(2)\n    -\u003eend();\n``` \n\nNow the code is less readable, but luckily we can configure `PhpStorm` to disable auto formatting the specified peace of code.\nTo do so, open `PhpStorm` preferences, go to the `Editor \u003e Code Style` section and select option `Enable formatter markers in comments`.\n\nNow you can turn the formatter off for the specific part of your code:\n\n```php\n// @formatter:off\n$fluentArray = (new FluentArray())\n    -\u003eone()\n        -\u003eid(1)\n    -\u003eend()\n    -\u003etwo()\n        -\u003eid(2)\n    -\u003eend();\n// @formatter:on\n```  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabenkoivan%2Ffluent-array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbabenkoivan%2Ffluent-array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabenkoivan%2Ffluent-array/lists"}