Skip to content

Commit 9db8573

Browse files
committed
init
0 parents  commit 9db8573

File tree

8 files changed

+448
-0
lines changed

8 files changed

+448
-0
lines changed

.gitignore

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

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 SoberNT
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 all
13+
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 THE
21+
SOFTWARE.

README.MD

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
**Install**
2+
3+
install this package via composer:
4+
5+
`composer require sobernt/json-object`
6+
7+
**Usage**
8+
9+
init object with minimal params:
10+
11+
$obj = new sobernt\JsonObject\JsonObject("{
12+
\"testkey\":\"testval\",
13+
\"testarray\":[
14+
\"testsimplearrayval1\",
15+
\"testsimplearrayval2\"
16+
],
17+
\"testcompositearray\":[
18+
\"testcompositearrayval1\",
19+
{
20+
\"testcompositearray2key\": \"testcompositearray2value\"
21+
}
22+
],
23+
\"testobject\":{
24+
\"testobjectkey\": \"testobjectval\",
25+
\"testobjectintkey\": \"1\"
26+
}
27+
}");
28+
29+
full constructor:
30+
31+
`__construct($json,$max_depth=255)`
32+
33+
set `$max_depth = 500` for reconfigure object max recursion level = 500
34+
35+
throws `JsonException` if json can't be parse
36+
37+
you can use this as default object
38+
$obj->testobject
39+
40+
this example return array:
41+
42+
$obj->testarray
43+
44+
this example returns object:
45+
46+
$obj->testcompositearray[1]
47+
48+
throws `InvalidArgumentException` if you call object not founded in json
49+
50+
you can set our prop [string or JsonObject]:
51+
52+
$obj->prop="testprop"
53+
54+
this example return value from tree:
55+
56+
$obj->filter("testcompositearray[1].testcompositearray2key")
57+
58+
you can set your formatter for primitive objects:
59+
60+
$this->setFormatter(function(string $data,string $name=null)
61+
{
62+
..
63+
return $data;
64+
}
65+
)
66+
67+
formatter necessarily has 2 import params:data and name.
68+
it's throw `InvalidArgumentException` if input is not callable.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "sobernt/json-object",
3+
"type": "project",
4+
"version": "1.0.0",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "SoberNT",
9+
"email": "[email protected]",
10+
"role": "Developer"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.2",
15+
"ext-json": "*"
16+
},
17+
"config": {
18+
"optimize-autoloader": true,
19+
"preferred-install": "dist",
20+
"sort-packages": true
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"sobernt\\JsonObject\\": "src/"
25+
}
26+
}
27+
}

example.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use sobernt\JsonObject\Exceptions\InvalidArgumentException;
4+
use sobernt\JsonObject\JsonObject;
5+
6+
require("vendor/autoload.php");
7+
8+
$obj = new sobernt\JsonObject\JsonObject("{
9+
\"testkey\":\"testval\",
10+
\"testarray\":[
11+
\"testsimplearrayval1\",
12+
\"testsimplearrayval2\"
13+
],
14+
\"testcompositearray\":[
15+
\"testcompositearrayval1\",
16+
{
17+
\"testcompositearray2key\": \"testcompositearray2value\"
18+
}
19+
],
20+
\"testobject\":{
21+
\"testobjectkey\": \"testobjectval\",
22+
\"testobjectintkey\": \"1\"
23+
}
24+
}");
25+
var_dump($obj);
26+
echo("\ntestkey:");
27+
var_dump($obj->testkey);
28+
echo("\ntestarray:");
29+
var_dump($obj->testarray);
30+
echo("\ntestcompositearray:");
31+
var_dump($obj->testcompositearray);
32+
echo("\ntestcompositearray[1]:");
33+
var_dump($obj->testcompositearray[1]);
34+
echo("\ntestobject:");
35+
var_dump($obj->testobject);
36+
echo("\ntestobject->testobjectkey:");
37+
var_dump($obj->testobject->testobjectkey);
38+
echo("\n->testobject->testobjectintkey:");
39+
var_dump($obj->testobject->testobjectintkey);
40+
echo("\nobj:");
41+
var_dump($obj);
42+
$obj->prop="testprop";
43+
echo("\njson_encode(obj):");
44+
var_dump(json_encode($obj));
45+
echo("\nfilter testcompositearray[1].testcompositearray2key:");
46+
var_dump($obj->filter("testcompositearray[1].testcompositearray2key"));
47+
48+
49+
try{
50+
var_dump($obj->tst);
51+
}catch (InvalidArgumentException $e){
52+
var_dump($e);
53+
}
54+
55+
try{
56+
$obj = new JsonObject("testval");
57+
}catch (\sobernt\JsonObject\Exceptions\JsonException $e){
58+
var_dump($e);
59+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace sobernt\JsonObject\Exceptions;
3+
use Exception;
4+
use Throwable;
5+
6+
class InvalidArgumentException extends Exception
7+
{
8+
public function __construct($message = "", $code = 0, Throwable $previous = null)
9+
{
10+
if($code==0) $code=404;
11+
parent::__construct($message, $code, $previous);
12+
}
13+
}

src/Exceptions/JsonException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
4+
namespace sobernt\JsonObject\Exceptions;
5+
6+
7+
use Exception;
8+
use Throwable;
9+
10+
class JsonException extends Exception
11+
{
12+
public function __construct($message = "", $code = 0, Throwable $previous = null)
13+
{
14+
if($code==0) $code=400;
15+
parent::__construct($message, $code, $previous);
16+
}
17+
}

0 commit comments

Comments
 (0)