Skip to content

Commit 174c5df

Browse files
committed
Merge branch 'master' of github.com:nahid/jsonq
2 parents c1de055 + c409bc3 commit 174c5df

File tree

5 files changed

+275
-41
lines changed

5 files changed

+275
-41
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ You don't need to call `fetch()` method after this. Because this method will fet
193193
Let's say you want to get the value of _'cities'_ property of your Json Data. You can do it like this:
194194

195195
```php
196-
$q = new jsonq('data.json');
196+
$q = new Jsonq('data.json');
197197
echo $q->find('vendor.name');
198198
```
199199

200200
If you want to traverse to more deep in hierarchy, you can do it like:
201201

202202
```php
203-
$q = new jsonq('data.json');
203+
$q = new Jsonq('data.json');
204204
echo $q->find('vendor.name');
205205
```
206206

@@ -219,14 +219,14 @@ Difference between this method and `find()` is that, `find()` method will return
219219
Let's say you want to start query over the values of _'vendor.name'_ property of your JSON Data. You can do it like this:
220220

221221
```php
222-
$q = new jsonq('data.json');
222+
$q = new Jsonq('data.json');
223223
echo $q->from('vendor.name')->get();
224224
```
225225

226226
If you want to traverse to more deep in hierarchy, you can do it like:
227227

228228
```php
229-
$q = new jsonq('data.json');
229+
$q = new Jsonq('data.json');
230230
echo $q->from('users.5.visits')->get();
231231
```
232232

@@ -273,14 +273,14 @@ This is an alias method of `from()` and will behave exactly like that. See examp
273273
Let's say you want to find the _'users'_ who has _`id`_ of `1`. You can do it like this:
274274

275275
```php
276-
$q = new jsonq('data.json');
276+
$q = new Jsonq('data.json');
277277
$res = $q->from('users')->where('id', '=', 1)->get();
278278
```
279279

280280
You can add multiple _where_ conditions. It'll give the result by AND-ing between these multiple where conditions.
281281

282282
```php
283-
$q = new jsonq('data.json');
283+
$q = new Jsonq('data.json');
284284
$res = $q->from('users')
285285
->where('id', '=', 1)
286286
->where('location', '=', 'barisal')
@@ -296,7 +296,7 @@ Parameters of `orWhere()` are the same as `where()`. The only difference between
296296
For example, if you want to find the users with _id_ of `1` or `2`, you can do it like this:
297297

298298
```php
299-
$q = new jsonq('data.json');
299+
$q = new Jsonq('data.json');
300300
$res = $q->from('users')
301301
->where('id', '=', 1)
302302
->orWhere('id', '=', 2)
@@ -361,7 +361,7 @@ This method will behave like `where(key, 'contains', val)` method call.
361361
Let's say you want to find the sum of the _'price'_ of the _'products'_. You can do it like this:
362362

363363
```php
364-
$q = new jsonq('data.json');
364+
$q = new Jsonq('data.json');
365365
$res = $q->from('products')
366366
->where('cat', '=', 1)
367367
->sum('price');
@@ -379,7 +379,7 @@ It will return the number of elements in the collection.
379379
Let's say you want to find how many elements are in the _'products'_ property. You can do it like:
380380

381381
```php
382-
$q = new jsonq('data.json');
382+
$q = new Jsonq('data.json');
383383
$res = $q->from('products')
384384
->where('cat', '=', 1)
385385
->count();
@@ -400,7 +400,7 @@ This is an alias method of `count()`.
400400
Let's say you want to find the maximum of the _'price'_ of the _'products'_. You can do it like this:
401401

402402
```php
403-
$q = new jsonq('data.json');
403+
$q = new Jsonq('data.json');
404404
$res = $q->from('products')
405405
->where('cat', '=', 1)
406406
->max('price);
@@ -418,7 +418,7 @@ See detail example [here](examples/max.php)
418418
Let's say you want to find the minimum of the _'price'_ of the _'products'_. You can do it like this:
419419

420420
```php
421-
$q = new jsonq('data.json');
421+
$q = new Jsonq('data.json');
422422
$res = $q->from('products')
423423
->where('cat', '=', 1)
424424
->min('price');
@@ -436,7 +436,7 @@ See detail example [here](examples/min.php)
436436
Let's say you want to find the average of the _'price'_ of the _'products'_. You can do it like this:
437437

438438
```php
439-
$q = new jsonq('data.json');
439+
$q = new Jsonq('data.json');
440440
$res = $q->from('products')
441441
->where('cat', '=', 1)
442442
->avg('price');
@@ -467,7 +467,7 @@ It will return the last element of the collection.
467467
**example:**
468468

469469
```php
470-
$q = new jsonq('data.json');
470+
$q = new Jsonq('data.json');
471471
$res = $q->from('products')
472472
->where('cat', '=', 1)
473473
->last();
@@ -484,7 +484,7 @@ It will return the nth element of the collection. If the given index is a **posi
484484
**example:**
485485

486486
```php
487-
$q = new jsonq('data.json');
487+
$q = new Jsonq('data.json');
488488
$res = $q->from('products')
489489
->where('cat', '=', 1)
490490
->nth(2);
@@ -501,7 +501,7 @@ It will return **true** if the element is not **empty** or not **null** or not a
501501
Let's say you want to find how many elements are in the _'products'_ property. You can do it like:
502502

503503
```php
504-
$q = new jsonq('data.json');
504+
$q = new Jsonq('data.json');
505505
$res = $q->from('products')
506506
->where('cat', '=', 1)
507507
->exists();
@@ -518,7 +518,7 @@ See detail example [here](examples/exists.php).
518518
Let's say you want to group the _'users'_ data based on the _'location'_ property. You can do it like:
519519

520520
```php
521-
$q = new jsonq('data.json');
521+
$q = new Jsonq('data.json');
522522
$res = $q->from('users')
523523
->groupBy('location')
524524
->get();
@@ -537,8 +537,8 @@ See detail example [here](examples/group-by.php).
537537
Let's say you want to sort the _'arr'_ data. You can do it like:
538538

539539
```php
540-
$q = new jsonq();
541-
$res = $q->collect([7, 5, 9, 1, 3)
540+
$q = new Jsonq();
541+
$res = $q->collect([7, 5, 9, 1, 3])
542542
->sort();
543543
```
544544

@@ -556,7 +556,7 @@ See detail example [here](examples/sort.php).
556556
Let's say you want to sort the _'price'_ data of _'products'_. You can do it like:
557557

558558
```php
559-
$q = new jsonq('data.json');
559+
$q = new Jsonq('data.json');
560560
$res = $q->from('products')
561561
->where('cat', '=', 1)
562562
->sortBy('price', 'desc');

src/JsonQueriable.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nahid\JsonQ\Exceptions\ConditionNotAllowedException;
66
use Nahid\JsonQ\Exceptions\FileNotFoundException;
7+
use Nahid\JsonQ\Exceptions\InvalidJsonException;
78

89
trait JsonQueriable
910
{
@@ -76,15 +77,15 @@ trait JsonQueriable
7677
/**
7778
* import data from file
7879
*
79-
* @param $json_file string
80+
* @param string $file
8081
* @return bool
8182
* @throws FileNotFoundException
8283
*/
83-
public function import($json_file = null)
84+
public function import($file = null)
8485
{
85-
if (!is_null($json_file)) {
86-
if (file_exists($json_file)) {
87-
$this->_map = $this->getDataFromFile($json_file);
86+
if (!is_null($file)) {
87+
if (is_string($file) && file_exists($file)) {
88+
$this->_map = $this->getDataFromFile($file);
8889
$this->_baseContents = $this->_map;
8990
return true;
9091
}
@@ -121,9 +122,9 @@ protected function prepare()
121122
}
122123

123124
/**
124-
* parse object to array
125+
* Parse object to array
125126
*
126-
* @param $obj object
127+
* @param object $obj
127128
* @return array|mixed
128129
*/
129130
protected function objectToArray($obj)
@@ -144,9 +145,9 @@ protected function objectToArray($obj)
144145
}
145146

146147
/**
147-
* check given value is multidimensional array
148+
* Check given value is multidimensional array
148149
*
149-
* @param $arr array
150+
* @param array $arr
150151
* @return bool
151152
*/
152153
protected function isMultiArray($arr)
@@ -161,16 +162,26 @@ protected function isMultiArray($arr)
161162
}
162163

163164
/**
164-
* check given value is valid JSON
165-
* @param $value string
166-
* @param $return_map bool
167-
* @return bool|array|string
165+
* Check given value is valid JSON
166+
*
167+
* @param string $value
168+
* @param bool $isReturnMap
169+
*
170+
* @return bool|array
168171
*/
169-
public function isJson($value, $return_map = false)
172+
public function isJson($value, $isReturnMap = false)
170173
{
174+
if (is_array($value) || is_object($value)) {
175+
return false;
176+
}
177+
171178
$data = json_decode($value, true);
172179

173-
return (json_last_error() == JSON_ERROR_NONE) ? ($return_map ? $data : true) : json_last_error_msg();
180+
if (json_last_error() !== JSON_ERROR_NONE) {
181+
return false;
182+
}
183+
184+
return $isReturnMap ? $data : true;
174185
}
175186

176187
/**
@@ -212,10 +223,14 @@ protected function getDataFromFile($file, $type = 'application/json')
212223
];
213224

214225
$context = stream_context_create($opts);
215-
216226
$data = file_get_contents($file, 0, $context);
217-
218-
return $this->isJson($data, true);
227+
$json = $this->isJson($data, true);
228+
229+
if (!$json) {
230+
throw new InvalidJsonException();
231+
}
232+
233+
return $json;
219234
}
220235

221236
throw new FileNotFoundException();

src/Jsonq.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,10 @@ public function then($node)
523523
*/
524524
public function json($data)
525525
{
526-
if (is_string($data)) {
527-
if ($json = $this->isJson($data, true)) {
528-
return $this->collect($json);
529-
}
526+
$json = $this->isJson($data, true);
527+
528+
if ($json) {
529+
return $this->collect($json);
530530
}
531531

532532
return $this;

tests/AbstractTestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Nahid\JsonQ\Tests;
4+
5+
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* Make private and protected function callable
9+
*
10+
* @param mixed $object
11+
* @param string $function
12+
* @return \ReflectionMethod
13+
*/
14+
protected function makeCallable($object, $function)
15+
{
16+
$method = new \ReflectionMethod($object, $function);
17+
$method->setAccessible(true);
18+
19+
return $method;
20+
}
21+
}

0 commit comments

Comments
 (0)