2017 © Pedro Peláez
 

library get

Get nested array values and object properties.

image

schnittstabil/get

Get nested array values and object properties.

  • Tuesday, July 31, 2018
  • by schnittstabil
  • Repository
  • 1 Watchers
  • 2 Stars
  • 36,605 Installations
  • PHP
  • 11 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 21 Versions
  • 1 % Grown

The README.md

Get Build Status Coverage Status

SensioLabsInsight, (*1)

Get nested array values and object properties., (*2)

Install

$ composer require schnittstabil/get

Usage

require __DIR__.'/vendor/autoload.php';

use Schnittstabil\Get;

echo 'Hello '.Get\value('name', $_REQUEST, 'John Doe');
echo 'Hello '.Get\valueOrFail('PHP_AUTH_USER', $_SERVER, 'User is not authenticated.');

echo 'user:     '.Get\env('DB_USER', 'root');
echo 'password: '.Get\envOrFail('DB_PASS', 'DB_PASS is not set');

API

Schnittstabil\Get\value

/**
 * Return array values and object properties.
 *
 * @param string|int|mixed[] $path    the path
 * @param object|array|null  $target  the target
 * @param mixed              $default default value if $path is not valid
 *
 * @return mixed the value determined by `$path` or otherwise `$default`
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
function value($path, $target, $default = null)
{/**/}
use Schnittstabil\Get;

$array = ['zero', 'one', 'two'];
$array['foo'] = new \stdClass();
$array['foo']->bar = true;
$array['un.usual'] = true;

Get\value(1,              $array);  //=> 'one'
Get\value('1',            $array);  //=> 'one'
Get\value('foo.bar',      $array);  //=> true
Get\value(['foo', 'bar'], $array);  //=> true
Get\value(['un.usual'],   $array);  //=> true

// $default
Get\value('un.usual', $array);      //=> null
Get\value(3,          $array);      //=> null
Get\value(3,          $array, 42);  //=> 42

Schnittstabil\Get\valueOrFail

Same as Schnittstabil\Get\value, but throws an OutOfBoundsException., (*3)

/**
 * Return array values and object properties.
 *
 * @param string|int|mixed[] $path    the path
 * @param object|array|null  $target  the target
 * @param mixed              $message exception message
 *
 * @throws \OutOfBoundsException if the `$path` does not determine a member of `$target`
 *
 * @return mixed the value determined by `$path`
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
function valueOrFail($path, $target, $message = null)
{/**/}
use Schnittstabil\Get;

Get\valueOrFail(3, $array);
//=> throws an OutOfBoundsException

Get\valueOrFail(3, $array, 'Error Message');
//=> throws a new OutOfBoundsException('Error Message')

Schnittstabil\Get\typed

/**
 * Converts a string value to a PHP typed value.
 *
 * @param mixed  $value
 * @return mixed
 */
function typed($value)
{/**/}
use Schnittstabil\Get;

Get\typed('42');
//=> int(42)

Get\typed('true');
Get\typed('yes');
Get\typed('on');
//=> bool(true)

Get\typed('false');
Get\typed('no');
Get\typed('off');
//=> bool(false)

/* escaping */
Get\typed('"true"');
Get\typed("'true'");
//=> string("true")

Schnittstabil\Get\env

/**
 * Gets the value of an environment variable.
 *
 * @param string $varname   the variable name
 * @param mixed  $default   default value if `$varname` is not valid
 * @param mixed  $typed     convert environment value to a PHP typed value
 * @param bool   $localOnly return only values of local environment variables
 *
 * @return mixed the environment value determined by `$varname` or otherwise `$default`
 */
function env($varname, $default = null, $typed = true, $localOnly = false)
{/**/}

Schnittstabil\Get\envOrFail

/**
 * Gets the value of an environment variable.
 *
 * @param string $varname   the variable name
 * @param mixed  $message   exception message
 * @param mixed  $typed     convert environment value to a PHP typed value
 * @param bool   $localOnly return only values of local environment variables
 *
 * @throws OutOfBoundsException if the `$varname` does not determine an environment value
 *
 * @return mixed the environment value determined by `$varname`
 */
function envOrFail($varname, $message = null, $typed = true, $localOnly = false)
{/**/}

License

MIT © Michael Mayer, (*4)

The Versions

31/07 2018

dev-master

9999999-dev https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

array object value null nested get coalescing

01/04 2018

v3.2.0

3.2.0.0 https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

array object value null nested get coalescing

28/05 2017

v3.1.0

3.1.0.0 https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

array object value null nested get coalescing

26/05 2017

v3.0.0

3.0.0.0 https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

array object value null nested get coalescing

26/05 2017

v2.2.0

2.2.0.0 https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

array object value null nested get coalescing

14/02 2017

v2.1.0

2.1.0.0 https://github.com/schnittstabil/get

Get nested array values and object properties.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

array object value null nested get coalescing

24/11 2015
23/11 2015

v1.0.0

1.0.0.0 https://github.com/schnittstabil/array_get

Get nested array values and object properties.

  Sources   Download

MIT

The Development Requires

array object value null nested get coalescing