Skip to content

Commit

Permalink
add nested node conditions system, add only array collection import s…
Browse files Browse the repository at this point in the history
…ystem
  • Loading branch information
nahid committed Apr 27, 2017
1 parent 4efb6ab commit eaa328c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
16 changes: 11 additions & 5 deletions data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
"email": "[email protected]",
"website":"www.example.com"
},
"items":[
{"id":1, "name":"MacBook Pro 13 inch retina", "price": 1500},
{"id":2, "name":"MacBook Pro 15 inch retina", "price": 1700},
{"id":3, "name":"Sony VAIO", "price": 1200},
{"id":4, "name":"Fujitsu", "price": 850}
"users":[
{"id":1, "name":"Johura Akter Sumi", "location": "Barisal"},
{"id":2, "name":"Mehedi Hasan Nahid", "location": "Barisal"},
{"id":3, "name":"Ariful Islam", "location": "Barisal"},
{"id":4, "name":"Suhel Ahmed", "location": "Sylhet"},
{"id":5, "name":"Firoz Serniabat", "location": "Gournodi"},
{"id":6, "name":"Musa Jewel", "location": "Barisal", "visits": [
{"name": "Sylhet", "year": 2011},
{"name": "Cox's Bazar", "year": 2012},
{"name": "Bandarbar", "year": 2014}
]}
]
}
12 changes: 6 additions & 6 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


$result = $json
->node('items')
->where('id', '>', 1)
->fetch(false);

var_dump($result);
->node('users')
->where('id', '=', 6)->get();



echo '<pre>';
var_dump($result);
?>

// var_dump($json->node('products')->where('name', '=', 'Keyboard')->fetch());
5 changes: 5 additions & 0 deletions src/JsonManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ protected function getDataFromFile($file, $type = 'application/json')

protected function getData()
{
if (count($this->_node)==0 || empty($this->_node) || $this->_node == ':') {
return $this->_map;
}

if($this->_node) {
$terminate=false;
$map = $this->_map;
Expand Down Expand Up @@ -185,6 +189,7 @@ protected function fetchOrData()

protected function filterByAndConditions($record, $conditions)
{

$return = false;
foreach($conditions as $rule) {
$func = 'cond' . ucfirst($this->_conds[$rule['condition']]);
Expand Down
65 changes: 54 additions & 11 deletions src/Jsonq.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function __construct($jsonFile=null)
public function node($node=null)
{
if(is_null($node) || $node=='') return false;
if ($node == ':') {
$this->_node = $node;
return $this;
}

$this->_node=explode(':', $node);
return $this;
Expand Down Expand Up @@ -76,19 +80,30 @@ public function orWhere($key=null, $condition=null, $value=null)

public function get($object = true)
{
$calculatedData = $this->processConditions();
//if (count(@$_andConditions)>0 or count(@$_orConditions)>0) {
$calculatedData = $this->processConditions();

$resultingData = [];
$resultingData = [];

foreach ($calculatedData as $data) {
if ($object) {
$resultingData[] = (object) $data;
} else {
$resultingData[] = $data;
foreach ($calculatedData as $data) {
if ($object) {
$resultingData[] = (object) $data;
} else {
$resultingData[] = $data;
}
}
}

return $resultingData;
unset($this->_andConditions);
unset($this->_orConditions);
$this->_node = '';
$this->_andConditions = [];
$this->_orConditions = [];

return $resultingData;
//}

//return $this->getData();


}

Expand All @@ -98,17 +113,45 @@ public function fetch($object = true)
}


public function first()
public function first($object = true)
{
$data = $this->get(false);
if (count($data>0)) {
return json_decode(json_encode(reset($data)));
if ($object) {
return json_decode(json_encode(reset($data)));
}

return json_decode(json_encode(reset($data)), true);

}

return null;

}

public function then($node)
{
$this->_map = $this->first(false);

$this->node($node);
return $this;
}

public function collect($data)
{
$this->_map = $this->objectToArray($data);
return $this;
}

public function objectToArray($obj) {
if(!is_array($obj) && !is_object($obj)) return $obj;

if(is_object($obj)) $obj = get_object_vars($obj);

return array_map([$this,'objectToArray'], $obj);
}



/*
getNodeValue()
Expand Down

0 comments on commit eaa328c

Please sign in to comment.