2017 © Pedro Peláez
 

library data

Common interface for interacting with data in objects and arrays.

image

aklump/data

Common interface for interacting with data in objects and arrays.

  • Wednesday, May 31, 2017
  • by aklump
  • Repository
  • 1 Watchers
  • 0 Stars
  • 69 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 28 Versions
  • 6 % Grown

The README.md

Data


************* ABANDONED *************, (*1)

Suggested replacement: - https://packagist.org/packages/dflydev/dot-access-data, (*2)

// Setter
$setter = (new \Dflydev\DotAccessData\Data($multi_array));
$setter->set('do.re.mi', $value);
$multi_array = $setter->export();

// Getter
print (new \Dflydev\DotAccessData\Data($multi_array))
  ->get('do.re.mi', 'default');

Summary

Provides a common means for getting data from objects or arrays with default option such as Lodash's get method. Other methods for working with array/object data will be added in time., (*3)

Rationale

Given a multidimensional array, in vanilla PHP you will do this:, (*4)

print isset($multi_array['do']['re']) ? $multi_array['do']['re'] : 'default';

Using this class you would do this:, (*5)

$data = new Data;
print $data->get($multi_array, 'do.re', 'default');

Not too impressive... but wait... imagine when you have complex objects, like say a Drupal 7 field value translated into spanish., (*6)

print isset($node->field_description['es']['1']['value']) ? $node->field_description['es']['1']['value'] : '';

// vs...

print $data->get($node, 'field_description.1.value', '');

Or when you need to work in Drupal 8 for a few days., (*7)

print isset($node->field_description) ? $node->get('field_description')->get(1)->value : '';

vs.

print $data->get($node, 'field_description.1.value', '');

This is where a consistent interface approach starts to make sense. By the way, there is a Drupal module that uses a different implementation of this class which can be found here., (*8)

Default value

Every call to ::get can have a default value, which removes the need for if/thens or issets., (*9)

$country = $data->get($record, 'home.address.country', 'U.S.');

Callback

You can pass a callback to process the value such as loading a record by id., (*10)

$url = $data->get($record, 'picture.id', 'http://mysite.com/default.jpg', function($id) {
    return load_url_by_record_id($id);
});

Details

<?php
use AKlump\Data\Data;

$data = new Data;

// Let's create a data subject, from which we want to pull data.
$a = array('b' => array('c' => 'd'));

// First let's pull data that exits.
// Because $a['b']['c'] has a value 'd', it will be returned.  Default is ignored.
$value = $data->get($a, 'b.c', 'e');
$value === 'c';

// Because $a['b']['z'] is not set then the default value comes back, 'e'.
$value = $data->get($a, 'b.z', 'e');
$value === 'e';

// This interface works on objects or arrays, regardless.  Let's convert this to a nested object using json functions.
$a_object = json_decode(json_encode($a));

// Make the same call and you'll get the same answer.
$value = $data->get($a, 'b.c', 'e');
$value === 'c';

Data::set()

Unconditionally sets a value at path., (*11)

Data::ensure()

Ensures that a path is set, does not overwrite if the key/property exists., (*12)

Data::fill()

This is a conditional setter method. It will fill in only if a path is empty (or based on some other test see example 3)., (*13)

Example 1

<?php
// In this case the value is filled in.
$array = array('do' => '');
$data->fill($array, 'do', 're');

// The value is filled in because the current value is empty.
$array === array('do' => 're');

Example 2

<?php
// In this case the value is NOT.
$array = array('do' => null);
$data->fill($array, 'do', 're', 'strict');

// The old value of null remains because 're' is a string and the current value not a string; even though it's empty, it will not be replaced because we've used the 'strict' test.
$array === array('do' => null); 

Example 3

<?php
// In this case the value is replaced based on a callback.
$array = array('do' => 45);
$data->fill($array, 'do', 're', function ($current, $exists) {
    return $exists && is_numeric($current);
});

// The value is replaced because our custom callable tested it as a number and returned true.
$array === array('do' => 're');

Acknowledgments

  • Thank you Aaron Jensen (https://github.com/aaronjensen) for introducing me to this concept.
  • https://lodash.com/docs/4.16.2#get

The Versions

31/05 2017

dev-master

9999999-dev http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

31/05 2017

0.0.28

0.0.28.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

31/05 2017

0.0.27

0.0.27.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

17/05 2017

0.0.26

0.0.26.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

13/01 2017

0.0.25

0.0.25.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

14/12 2016

0.0.24

0.0.24.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

10/12 2016

0.0.23

0.0.23.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

10/12 2016

0.0.22

0.0.22.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

09/12 2016

0.0.21

0.0.21.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

09/12 2016

0.0.20

0.0.20.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.19

0.0.19.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.18

0.0.18.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.17

0.0.17.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.16

0.0.16.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.15

0.0.15.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.14

0.0.14.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/12 2016

0.0.13

0.0.13.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

31/10 2016

0.0.12

0.0.12.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

31/10 2016

0.0.11

0.0.11.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

31/10 2016

0.0.10

0.0.10.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

09/10 2016

0.0.9

0.0.9.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

07/10 2016

0.0.8

0.0.8.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

05/10 2016

0.0.7

0.0.7.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

05/10 2016

0.0.6

0.0.6.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

04/10 2016

0.0.5

0.0.5.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

04/10 2016

0.0.4

0.0.4.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

04/10 2016

0.0.3

0.0.3.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects

03/10 2016

0.0.2

0.0.2.0 http://github.com/aklump/data

Common interface for interacting with data in objects and arrays.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

php array data getters objects