Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nahid/jsonq
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed May 28, 2018
2 parents c1de055 + c409bc3 commit 174c5df
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 41 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ You don't need to call `fetch()` method after this. Because this method will fet
Let's say you want to get the value of _'cities'_ property of your Json Data. You can do it like this:

```php
$q = new jsonq('data.json');
$q = new Jsonq('data.json');
echo $q->find('vendor.name');
```

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

```php
$q = new jsonq('data.json');
$q = new Jsonq('data.json');
echo $q->find('vendor.name');
```

Expand All @@ -219,14 +219,14 @@ Difference between this method and `find()` is that, `find()` method will return
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:

```php
$q = new jsonq('data.json');
$q = new Jsonq('data.json');
echo $q->from('vendor.name')->get();
```

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

```php
$q = new jsonq('data.json');
$q = new Jsonq('data.json');
echo $q->from('users.5.visits')->get();
```

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

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

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

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

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

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

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

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

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

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

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

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

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

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

```php
$q = new jsonq();
$res = $q->collect([7, 5, 9, 1, 3)
$q = new Jsonq();
$res = $q->collect([7, 5, 9, 1, 3])
->sort();
```

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

```php
$q = new jsonq('data.json');
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->sortBy('price', 'desc');
Expand Down
51 changes: 33 additions & 18 deletions src/JsonQueriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nahid\JsonQ\Exceptions\ConditionNotAllowedException;
use Nahid\JsonQ\Exceptions\FileNotFoundException;
use Nahid\JsonQ\Exceptions\InvalidJsonException;

trait JsonQueriable
{
Expand Down Expand Up @@ -76,15 +77,15 @@ trait JsonQueriable
/**
* import data from file
*
* @param $json_file string
* @param string $file
* @return bool
* @throws FileNotFoundException
*/
public function import($json_file = null)
public function import($file = null)
{
if (!is_null($json_file)) {
if (file_exists($json_file)) {
$this->_map = $this->getDataFromFile($json_file);
if (!is_null($file)) {
if (is_string($file) && file_exists($file)) {
$this->_map = $this->getDataFromFile($file);
$this->_baseContents = $this->_map;
return true;
}
Expand Down Expand Up @@ -121,9 +122,9 @@ protected function prepare()
}

/**
* parse object to array
* Parse object to array
*
* @param $obj object
* @param object $obj
* @return array|mixed
*/
protected function objectToArray($obj)
Expand All @@ -144,9 +145,9 @@ protected function objectToArray($obj)
}

/**
* check given value is multidimensional array
* Check given value is multidimensional array
*
* @param $arr array
* @param array $arr
* @return bool
*/
protected function isMultiArray($arr)
Expand All @@ -161,16 +162,26 @@ protected function isMultiArray($arr)
}

/**
* check given value is valid JSON
* @param $value string
* @param $return_map bool
* @return bool|array|string
* Check given value is valid JSON
*
* @param string $value
* @param bool $isReturnMap
*
* @return bool|array
*/
public function isJson($value, $return_map = false)
public function isJson($value, $isReturnMap = false)
{
if (is_array($value) || is_object($value)) {
return false;
}

$data = json_decode($value, true);

return (json_last_error() == JSON_ERROR_NONE) ? ($return_map ? $data : true) : json_last_error_msg();
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}

return $isReturnMap ? $data : true;
}

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

$context = stream_context_create($opts);

$data = file_get_contents($file, 0, $context);

return $this->isJson($data, true);
$json = $this->isJson($data, true);

if (!$json) {
throw new InvalidJsonException();
}

return $json;
}

throw new FileNotFoundException();
Expand Down
8 changes: 4 additions & 4 deletions src/Jsonq.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,10 @@ public function then($node)
*/
public function json($data)
{
if (is_string($data)) {
if ($json = $this->isJson($data, true)) {
return $this->collect($json);
}
$json = $this->isJson($data, true);

if ($json) {
return $this->collect($json);
}

return $this;
Expand Down
21 changes: 21 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Nahid\JsonQ\Tests;

abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{
/**
* Make private and protected function callable
*
* @param mixed $object
* @param string $function
* @return \ReflectionMethod
*/
protected function makeCallable($object, $function)
{
$method = new \ReflectionMethod($object, $function);
$method->setAccessible(true);

return $method;
}
}
Loading

0 comments on commit 174c5df

Please sign in to comment.