Skip to content

Commit 5a3fe78

Browse files
committed
make code developer friendly, and refactor
1 parent b47511e commit 5a3fe78

File tree

4 files changed

+145
-57
lines changed

4 files changed

+145
-57
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
/.gitattributes export-ignore
44
/.gitignore export-ignore
5-
/README.md export-ignore
5+
/README.md export-ignore
6+
/example export-ignore

example/index.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,22 @@
1010
$result = '';
1111
$json=new Jsonq($rootDir . 'data.json');
1212
$json1 = $json->copy();
13-
$result = $json->from('users')
14-
->prepare()
15-
->groupBy('locations');
16-
17-
13+
$result = $json->from('products');
1814
echo '<pre>';
19-
dump($json->get());
15+
dump($json->sum('price'));
16+
dump($json->copy()->collect([1,2,6])->avg());
2017

2118

2219
/* ----------- avg method example ------------ */
23-
20+
/*
2421
$json=new Jsonq($rootDir . 'data.json');
25-
$avg = $json->from('products')->prepare()->avg('price');
22+
$avg = $json->from('products')->avg('price');
2623
2724
echo '<pre>';
2825
dump($avg);
2926
3027
$json=new Jsonq();
31-
$avg = $json->collect([1, 3, 2])->prepare()->avg();
28+
$avg = $json->collect([1, 3, 2])->avg();
3229
3330
echo '<pre>';
34-
dump($avg);
31+
dump($avg);*/

src/JsonQueriable.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ trait JsonQueriable
3333
*/
3434
protected $_conditions = [];
3535

36+
37+
protected $_isProcessed = false;
38+
3639
/**
3740
* map all conditions with methods
3841
* @var array
@@ -78,6 +81,56 @@ public function import($json_file = null)
7881
throw new FileNotFoundException();
7982
}
8083

84+
/**
85+
* Prepare data from desire conditions
86+
*
87+
* @return $this
88+
* @throws ConditionNotAllowedException
89+
*/
90+
protected function prepare()
91+
{
92+
if ($this->_isProcessed) {
93+
return $this;
94+
}
95+
if (count($this->_conditions) > 0) {
96+
$calculatedData = $this->processConditions();
97+
$this->_map = $this->objectToArray($calculatedData);
98+
99+
$this->_conditions = [];
100+
$this->_node = '';
101+
$this->_isProcessed = true;
102+
return $this;
103+
}
104+
105+
$this->_isProcessed = true;
106+
$this->_map = $this->objectToArray($this->getData());
107+
return $this;
108+
}
109+
110+
/**
111+
* parse object to array
112+
*
113+
* @param $obj object
114+
* @return array|mixed
115+
*/
116+
protected function objectToArray($obj)
117+
{
118+
if (!is_array($obj) && !is_object($obj)) {
119+
return $obj;
120+
}
121+
122+
if (is_array($obj)) {
123+
return $obj;
124+
}
125+
126+
if (is_object($obj)) {
127+
$obj = get_object_vars($obj);
128+
}
129+
130+
return array_map([$this, 'objectToArray'], $obj);
131+
}
132+
133+
81134

82135
/**
83136
* check given value is multidimensional array
@@ -214,6 +267,11 @@ protected function processConditions()
214267
*/
215268
public function where($key, $condition = null, $value = null)
216269
{
270+
if (!is_null($condition) && is_null($value)) {
271+
$value = $condition;
272+
$condition = '=';
273+
}
274+
217275
if (count($this->_conditions) < 1) {
218276
array_push($this->_conditions, []);
219277
}
@@ -230,6 +288,11 @@ public function where($key, $condition = null, $value = null)
230288
*/
231289
public function orWhere($key = null, $condition = null, $value = null)
232290
{
291+
if (!is_null($condition) && is_null($value)) {
292+
$value = $condition;
293+
$condition = '=';
294+
}
295+
233296
array_push($this->_conditions, []);
234297

235298
return $this->makeWhere($key, $condition, $value);

0 commit comments

Comments
 (0)