2017 © Pedro Peláez
 

library params

Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.

image

graste/params

Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.

  • Thursday, April 30, 2015
  • by graste
  • Repository
  • 2 Watchers
  • 11 Stars
  • 5,797 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 13 Versions
  • 1 % Grown

The README.md

Params

Latest-Stable-Version License Latest Unstable Version Build Status [Stories in Ready][15] Total Composer Downloads, (*1)

Purpose

Array wrapper object that eases the retrieval of values. It provides a get method that can return a default value if the given key is missing. In addition to the usage as a normal array and the various get, set, has etc. methods Parameters defines a getValues($expression) method that allows the retrieval of values by providing more or less complex expressions. You can easily retrieve values from nested arrays or get several values from different nesting levels with one call. There are traits for convenient creation of configurable classes., (*2)

Requirements and installation

  • PHP ^7.0

Install the library via Composer:, (*3)

./composer.phar require graste/params [optional version], (*4)

Adding it manually as a vendor library requirement to the composer.json file of your project works as well:, (*5)

{
    "require": {
        "graste/params": "^3.0.0"
    }
}

Alternatively, you can download a release archive from the releases., (*6)

Documentation and usage

$data = array(
    'str' => 'some string',
    'first_level' => 'first level',
    'nested' => array(
        'str' => 'some nested string',
        '2nd level' => 'second level',
    ),
    'more' => array(
        'str' => 'other nested string',
    )
);

// base object of the aliased versions:
$arrayobj = new \Params\ConfigurableArrayObject($array);

// create mutable parameters instance:
$params = new \Params\Parameters($data);
// or initialize an immutable instance that prevents further modifications after construction
$params = new \Params\Immutable\ImmutableParameters($data);

// do you like options instead of parameters in your classes?
$params = new \Params\Options($data);
$params = new \Params\Immutable\ImmutableOptions($data);

// do you like settings instead of parameters in your classes?
$params = new \Params\Settings($data);
$params = new \Params\Immutable\ImmutableSettings($data);

// use it as a recursive object:

$params->has("foo")                           // returns true now
$params->get("str")                           // gives "some string"
$params->get("nested")                        // gives array stored under "nested" key
$params->get("nested")->get('str')            // gives "some nested string"
$params->get("non-existant", "default value") // gives "default value" as given key is non existant
$params->get("nested")->set("foo", "bar")     // sets key "foo" to value "bar" on the "nested" array
$params->getKeys()                            // returns all first level keys
$params->toArray()                            // returns internal array

// or use the mutable methods
$params->set("foo", "bar")                    // sets key "foo" to value "bar"
$params->add(array|ArrayAccess)               // add array or other ArrayAccess implementing object to current instance
$params->clear()                              // empty internal array
$params->map(function($key, $value) { … })    // modifies each value to the value returned by the callback

// retrieve values using expressions

$params->getValues("foo")                               // gives "bar"
$params->getValues("nested.str")                        // gives "some nested string"
$params->getValues('*.str')                             // gives array("some nested string", "other nested string")
$params->getValues('[str, nested.str]')                 // gives array("some string", "some nested string")
$params->getValues('nested."2nd level" || first_level') // gives "second level" as that key exists; other expression not evaluated
$params->getValues('first_level || nested."2nd level"') // gives "first level" as that key exists; other expression not evaluated

// use it as an array:

$params["str"]    // gives "some string"
$params["nested"] // gives the array under the "nested" key
$params[1]        // gives "first level"

// use it as an object with properties

$params->foo = 'bar'          // sets key 'foo' to value 'bar'
$params->filter->bool = 'yes' // sets $params['filter']['bool'] to value 'yes'

The expression syntax used is provided by Michael Dowling's JMESPath., (*7)

Traits

There are [Traits][16] may be used for own configurable classes:, (*8)

  • ImmutableParametersTrait wraps parameters
  • ParametersTrait wraps parameters
  • ImmutableOptionsTrait wraps options
  • OptionsTrait wraps options
  • ImmutableSettingsTrait wraps settings
  • SettingsTrait wraps settings

For fluent API support the methods add, set, remove and clear return the class instance they're mixed into., (*9)

ElasticSearch queries

The syntax sugar Parameters provides is not only nice to define configurable classes, but also eases the creation and modification of ElasticSearch queries:, (*10)

$params->filter->bool->must[1]->term->live = false;
$params->get('filter')->set('bool', …);
$params->filter->bool->must[] = array(…);

Contribution

Please contribute by forking and sending a pull request. More information can be found in the CONTRIBUTING.md file., (*11)

To develop on this repository clone your fork and use the Makefile targets:, (*12)

  • make install installs composer and all necessary vendor libraries
  • make tests runs the phpunit tests
  • make code-sniffer-cli runs the code sniffer on console

The code tries to adhere to the following PHP-FIG standards: PSR-4, PSR-1 and PSR-2. The current license is MIT., (*13)

The authors and contributors are mentioned in the github contributors graph of this repository. The license file must be left intact for attribution and sharing of this work., (*14)

Changelog

See CHANGELOG.md for more information about changes., (*15)

The Versions

30/04 2015

dev-master

9999999-dev https://github.com/graste/params

Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.

  Sources   Download

MIT

The Requires

 

The Development Requires

settings library search array options parameters

08/03 2015

v3.0.0

3.0.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.

  Sources   Download

MIT

The Requires

 

The Development Requires

settings library search array options parameters

28/01 2015

2.1.1

2.1.1.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values. Has parameters, options and settings traits for inclusion in other libs.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

settings library search array options parameters

07/10 2014

v2.1.0

2.1.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values. Has parameters, options and settings traits for inclusion in other libs.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

settings library search array options parameters

06/10 2014

v2.0.1

2.0.1.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

03/07 2014

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

03/07 2014

v2.0.0

2.0.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

02/07 2014

v1.2.0

1.2.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

01/07 2014

v1.1.0

1.1.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

01/07 2014

v1.0.1

1.0.1.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

01/07 2014

v1.0.0

1.0.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

29/06 2014

v0.2.0

0.2.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters

28/06 2014

v0.1.0

0.1.0.0 https://github.com/graste/params

Array wrapper that eases the retrieval of values.

  Sources   Download

CC-BY-SA-3.0

The Requires

 

The Development Requires

library search array options parameters