Skip to content

Commit a911a8c

Browse files
zoohzooh
authored andcommitted
Initial commit
0 parents  commit a911a8c

File tree

82 files changed

+5048
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+5048
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea/
2+
/vendor/
3+
/composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Edward Surov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
PHP JSON Pointer
2+
================
3+
This library allows usage of RFC-6901-compliant JSON pointers with PHP
4+
variables.
5+
6+
#License
7+
PHP JSON Pointer is licensed under MIT license.
8+
9+
#Documentation
10+
To get ready to access data with JSON Pointer links you need just 3 simple steps:
11+
12+
1. Create an object of `\ESurov\JSONPointer\Pointer` class by calling it's static `factory()` method.
13+
2. Link data to the pointer object by calling it's `setData()` method.
14+
3. Set up JSON Pointer link text by calling it's `setText()` method.
15+
16+
After doing so, you can call `test()`, `read()` and `write()` methods to access data.
17+
18+
##Example of usage
19+
```php
20+
<?php
21+
22+
use \ESurov\JSONPointer\Pointer;
23+
24+
// Setting up data.
25+
$data = (object) [
26+
'a' => (object) [
27+
'b' => ['c', 'd', 'e'],
28+
],
29+
];
30+
31+
// Setting up pointer.
32+
$link = '/a/b/1';
33+
$pointer = Pointer::factory()
34+
->setData($data)
35+
->setText($link);
36+
37+
// Readind value.
38+
echo $pointer->read(); // d
39+
40+
// Writing value
41+
echo $pointer
42+
->write('f') // Sets $data->a->b[1] to 'f'.
43+
->read(); // f
44+
45+
// Testing value.
46+
$result = $pointer->test(); // Sets $result to TRUE.
47+
$link = '/a/c'; // Link to non-existing property
48+
$result = $pointer
49+
->setText($link)
50+
->test(); // Sets $result to FALSE.
51+
52+
// Treating PHP objects as arrays (not compliant with RFC6901, but
53+
// it's the only way to access non-numeric index in PHP array).
54+
$subData = ['g' => 2, 'h' => 3];
55+
$link = '/a/c/g'; // Link to non-numeric index of array.
56+
$result = $pointer
57+
->write($subData) // Sets $data->a->c to ['g' => 2, 'h' => 3].
58+
->setText($link)
59+
->test(); // Sets $result to FALSE.
60+
$result $pointer
61+
->setOptions(Pointer::OPTION_NON_NUMERIC_ARRAY_INDICES)
62+
->test(); // Sets $result to TRUE.
63+
echo $pointer->read(); // 2
64+
```

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "remorhaz/php-json-pointer",
3+
"description": "JSON Pointer (RFC-6901) PHP implementation",
4+
"authors": [
5+
{
6+
"name": "Edward Surov",
7+
"email": "zoohie@gmail.com"
8+
}
9+
],
10+
"require": {
11+
"php": ">=5.6"
12+
},
13+
"require-dev": {
14+
"phpunit/phpunit": "@stable"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Remorhaz\\JSONPointer\\": "src/"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Remorhaz\\JSONPointer\\Test\\": "tests/"
24+
}
25+
}
26+
}

src/EvaluateException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer;
4+
5+
interface EvaluateException extends Exception
6+
{
7+
}

src/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer;
4+
5+
interface Exception
6+
{
7+
}

src/Locator.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer;
4+
5+
class Locator
6+
{
7+
8+
/**
9+
* Reference list.
10+
*
11+
* @var Locator\Reference[]
12+
*/
13+
protected $referenceList = [];
14+
15+
/**
16+
* @var Locator\Reference|null
17+
*/
18+
protected $lastReference;
19+
20+
21+
/**
22+
* Constructor.
23+
*/
24+
protected function __construct()
25+
{
26+
}
27+
28+
29+
/**
30+
* Creates object instance.
31+
*
32+
* @return static
33+
*/
34+
public static function factory()
35+
{
36+
return new static();
37+
}
38+
39+
40+
/**
41+
* Returns reference list.
42+
*
43+
* @return Locator\Reference[]
44+
*/
45+
public function getReferenceList()
46+
{
47+
return $this->referenceList;
48+
}
49+
50+
51+
/**
52+
* Adds reference to list.
53+
*
54+
* @param Locator\Reference $reference
55+
* @return $this
56+
*/
57+
public function addReference(Locator\Reference $reference)
58+
{
59+
$this->referenceList[] = $reference;
60+
if (null === $this->lastReference) {
61+
$pathPrefix = '';
62+
} else {
63+
$pathPrefix = $this
64+
->lastReference
65+
->setLast(false)
66+
->getPath();
67+
}
68+
69+
$this->lastReference = $reference
70+
->setLast()
71+
->setPath("{$pathPrefix}/{$reference->getText()}");
72+
return $this;
73+
}
74+
}

src/Locator/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Remorhaz\JSONPointer\Locator;
4+
5+
interface Exception extends \Remorhaz\JSONPointer\Exception
6+
{
7+
}

0 commit comments

Comments
 (0)