2017 © Pedro Peláez
 

library php-json-pointer

PHP JSON Pointer (RFC6901) implementation

image

gamringer/php-json-pointer

PHP JSON Pointer (RFC6901) implementation

  • Monday, May 22, 2017
  • by g.amringer
  • Repository
  • 2 Watchers
  • 7 Stars
  • 7,045 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 25 % Grown

The README.md

JSONPointer

License Latest Stable Version Latest Unstable Version Total Downloads, (*1)

SensioLabsInsight, (*2)

Build Status, (*3)

Build Status Code Coverage Scrutinizer Code Quality, (*4)

A RFC6901 compliant JSON Pointer PHP implementation, (*5)

License

JSONPointer is licensed under the MIT license., (*6)

Installation

composer require gamringer/php-json-pointer

Tests

composer install
phpunit

Documentation

Testing a value for existence

<?php

$target = [
  "foo" => ["bar", "baz"],
  "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->has("/foo"));

/* Results:

bool(true)

*/

Retrieving a value that does not exist will return false, (*7)

<?php

$target = [
  "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->has("/foo"));

/* Results:

bool(false)

*/

Retrieving a value

<?php

$target = [
    "foo" => ["bar", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->get("/foo"));

/* Results:

array(2) {
  [0] =>
  string(3) "bar"
  [1] =>
  string(3) "baz"
}

*/

Retrieving a value that does not exist will throw an exception, (*8)

<?php

$target = [
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->get("/foo"));

/* Results:

Throws gamringer\JSONPointer\Exception

*/

Inserting a value

Inserting a value will returns a VoidValue object if used on an indexed array., (*9)

<?php

$target = [
    "foo" => ["bar", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

$value = "waldo";
var_dump($pointer->insert("/foo/1", $value));
var_dump($pointer->get("/foo"));

/* Results:

class gamringer\JSONPointer\VoidValue#6 (2) {
  protected $owner =>
  array(3) {
    ...
  }
  protected $target =>
  string(1) "1"
}
array(3) {
  [0] =>
  string(3) "bar"
  [1] =>
  string(5) "waldo"
  [2] =>
  string(3) "baz"
}

*/

If used on anything else, it will behave in the exact same way as get(), (*10)

<?php

$target = [
    "foo" => ["bar", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

$value = "waldo";
var_dump($pointer->insert("/foo", $value));
var_dump($pointer->get("/foo"));

/* Results:

array(2) {
  [0] =>
  string(3) "bar"
  [1] =>
  string(3) "baz"
}
string(5) "waldo"

*/

Setting a value

Setting a value returns the content previously at that path, (*11)

<?php

$target = [
    "foo" => ["bar", "waldo", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

$value = "corge";
var_dump($pointer->set("/foo", $value));

/* Results:

array(3) {
  [0] =>
  string(3) "bar"
  [1] =>
  string(5) "waldo"
  [2] =>
  string(3) "baz"
}

*/

If the path was attainable, but not set, it will return a VoidValue, (*12)

<?php

$target = [
    "foo" => ["bar", "waldo", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

$value = "garply";
var_dump($pointer->set("/grault", $value));

/* Results:

class gamringer\JSONPointer\VoidValue#6 (2) {
  protected $owner =>
  array(3) {
    ...
  }
  protected $target =>
  string(6) "grault"
}

*/

Remove a value

Removing a value returns the content previously at that path, (*13)

<?php

$target = [
    "foo" => ["bar", "waldo", "baz"],
    "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->remove("/qux"));

/* Results:

string(4) "quux"

*/

Removing a value that does not exist will throw an exception, (*14)

<?php

$target = [
    "foo" => ["bar", "waldo", "baz"],
];

$pointer = new \gamringer\JSONPointer\Pointer($target);

var_dump($pointer->remove("/qux"));

/* Results:

Throws gamringer\JSONPointer\Exception

*/

Operations affect the original object

This affects Remove Operations, (*15)

<?php

$target = [
  "foo" => ["bar", "waldo", "baz"],
  "qux" => "quux"
];

$pointer = new \gamringer\JSONPointer\Pointer($target);
$pointer->remove("/qux");

var_dump($target);

/* Results:

array(1) {
  'foo' =>
  array(3) {
    [0] =>
    string(3) "bar"
    [1] =>
    string(5) "waldo"
    [2] =>
    string(3) "baz"
  }
}

*/

This also affects Add Operations, (*16)

<?php

$target = [
  "foo" => ["bar", "waldo", "baz"],
  "qux" => "quux"
];

$value = "bar";
$pointer = new \gamringer\JSONPointer\Pointer($target);
$pointer->set("/foo", $value);

var_dump($target);

/* Results:

array(2) {
  'foo' =>
  string(3) "bar"
  'qux' =>
  string(4) "quux"
}

*/

The Versions

22/05 2017

dev-master

9999999-dev https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

06/02 2017

3.2

3.2.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

28/08 2015

3.1

3.1.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

08/07 2015

3.0.0

3.0.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

08/07 2015

3.0.0-alpha

3.0.0.0-alpha https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

06/04 2015

2.0.0

2.0.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

01/03 2015

1.0.2

1.0.2.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

28/02 2015

1.0.1

1.0.1.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

27/02 2015

1.0.0

1.0.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

18/02 2015

0.2

0.2.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901

12/11 2014

0.1

0.1.0.0 https://github.com/gamringer/JSONPointer

PHP JSON Pointer (RFC6901) implementation

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Guillaume Amringer

json json pointer rfc6901 6901