Skip to content

Commit

Permalink
ConsistencyChecker helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Jan 8, 2018
1 parent 9ba7782 commit 870b984
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/Exceptions/NotConsistentDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Exceptions;

class NotConsistentDataException extends \Exception
{
}
5 changes: 3 additions & 2 deletions src/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use ArrayQuery\Constants;
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;
use ArrayQuery\Helpers\ArrayConverter;

abstract class AbstractFilter
{
Expand All @@ -36,7 +37,7 @@ private static function convertObjectToArray($arrayElement)
{
$convertedArray = [];

foreach ((array)$arrayElement as $key => $element) {
foreach (ArrayConverter::convertToPlainArray($arrayElement) as $key => $element) {
$key = explode("\\", $key);
$key = end($key);
$key = explode("\000", $key);
Expand All @@ -58,7 +59,7 @@ private static function getValueFromKeysArray($keysArray, $arrayElement)
if (count($keysArray)>1) {
$key = array_shift($keysArray);

return self::getValueFromKeysArray($keysArray, (array) $arrayElement[$key]);
return self::getValueFromKeysArray($keysArray, ArrayConverter::convertToPlainArray($arrayElement[$key]));
}

if (!isset($arrayElement[$keysArray[0]])) {
Expand Down
4 changes: 3 additions & 1 deletion src/Filters/Criterion/InArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ArrayQuery\Filters\Criterion;

use ArrayQuery\Helpers\ArrayConverter;

class InArrayFilter implements FilterInterface
{
/**
Expand All @@ -19,6 +21,6 @@ class InArrayFilter implements FilterInterface
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
return in_array($value, (array) $valueToCompare);
return in_array($value, ArrayConverter::convertToPlainArray($valueToCompare));
}
}
4 changes: 3 additions & 1 deletion src/Filters/Criterion/InArrayInversedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ArrayQuery\Filters\Criterion;

use ArrayQuery\Helpers\ArrayConverter;

class InArrayInversedFilter implements FilterInterface
{
/**
Expand All @@ -19,6 +21,6 @@ class InArrayInversedFilter implements FilterInterface
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
return in_array($valueToCompare, (array) $value);
return in_array($valueToCompare, ArrayConverter::convertToPlainArray($value));
}
}
32 changes: 32 additions & 0 deletions src/Helpers/ArrayConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Helpers;

class ArrayConverter
{
/**
* @param $array
* @return mixed
*/
public static function convertToObjectArray($array)
{
return json_decode(json_encode($array));
}

/**
* @param $array
* @return mixed
*/
public static function convertToPlainArray($array)
{
return json_decode(json_encode($array), true);
}
}
56 changes: 56 additions & 0 deletions src/Helpers/ConsistencyChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Helpers;

class ConsistencyChecker
{
/**
* @param $element
* @param $array
* @return bool
*/
public static function isElementValid($element, $array)
{
foreach ($array as $key => $item) {
if (is_object($item)) {
$array[$key] = ArrayConverter::convertToPlainArray($item);
}
}

if (is_object($element)) {
$element = ArrayConverter::convertToPlainArray($element);
}

$FirstItemKeyMap = array_keys(current($array));
$ItemKeyMap = array_keys($element);

if (count(array_diff($FirstItemKeyMap, $ItemKeyMap)) > 0 || count(array_diff($ItemKeyMap, $FirstItemKeyMap)) > 0) {
return false;
}

return true;
}

/**
* @param $array
* @return bool
*/
public static function isValid($array)
{
foreach ($array as $element) {
if (false === self::isElementValid($element, $array)) {
return false;
}
}

return true;
}
}
19 changes: 13 additions & 6 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use ArrayQuery\Exceptions\EmptyArrayException;
use ArrayQuery\Exceptions\InvalidArrayException;
use ArrayQuery\Exceptions\NotConsistentDataException;
use ArrayQuery\Exceptions\NotValidCriterionOperatorException;
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;
use ArrayQuery\Exceptions\NotValidLimitsOfArrayException;
Expand All @@ -20,6 +21,8 @@
use ArrayQuery\Filters\JoinFilter;
use ArrayQuery\Filters\SortingFilter;
use ArrayQuery\Filters\LimitFilter;
use ArrayQuery\Helpers\ArrayConverter;
use ArrayQuery\Helpers\ConsistencyChecker;

class QueryBuilder
{
Expand Down Expand Up @@ -67,16 +70,20 @@ public static function create(array $array)
}

/**
* @param $array
*
* @param array $array
* @throws EmptyArrayException
* @throws NotConsistentDataException
*/
private function setArray(array $array)
{
if (empty($array)) {
throw new EmptyArrayException('Empty array provided.');
}

if (false === ConsistencyChecker::isValid($array)) {
throw new NotConsistentDataException('Array provided has no consistent data.');
}

$this->array = $array;
}

Expand Down Expand Up @@ -293,13 +300,13 @@ private function applyCriteriaFilter(array $array)
}
);

$results = array_map(function($result) use ($criterion) {
$results = array_map(function ($result) use ($criterion) {
$key = explode(Constants::ALIAS_DELIMITER, $criterion['key']);
if(count($key) > 1){
if (count($key) > 1) {
$oldkey = explode(Constants::ARRAY_SEPARATOR, $key[0]);
$newkey = $key[1];

$result = (array)($result);
$result = ArrayConverter::convertToPlainArray($result);
$result[$newkey] = $result[$oldkey[0]];
unset($result[$oldkey[0]]);
}
Expand All @@ -316,7 +323,7 @@ private function applyCriteriaFilter(array $array)
*/
private function castElementToArray($element)
{
return (array) $element;
return ArrayConverter::convertToPlainArray($element);
}

/**
Expand Down
79 changes: 79 additions & 0 deletions tests/ArrayConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Tests;

use ArrayQuery\Helpers\ArrayConverter;
use PHPUnit\Framework\TestCase;

class ArrayConverterTest extends TestCase
{
/**
* @test
*/
public function it_converts_a_plain_array_in_an_object_array()
{
$array = [
[
'id' => 1,
'title' => 'Leanne Graham',
'email' => '[email protected]',
'rate' => 5,
'company' => [
'name' => 'Romaguera-Jacobson',
'catchPhrase' => 'Face to face bifurcated interface',
'bs' => 'e-enable strategic applications'
]
],
[
'id' => 2,
'title' => 'Ervin Howell',
'email' => '[email protected]',
'rate' => 3,
'company' => [
'name' => 'Robel-Corkery',
'catchPhrase' => 'Multi-tiered zero tolerance productivity',
'bs' => 'transition cutting-edge web services'
]
],
];

$objectArray = ArrayConverter::convertToObjectArray($array);
foreach ($objectArray as $item) {
$this->assertInstanceOf(\stdClass::class, $item);
}
}

/**
* @test
*/
public function it_converts_an_object_array_in_a_plain_array()
{
$a = new \stdClass();
$a->id = 1;
$a->title = 'Leanne Graham';
$a->email = '[email protected]';

$b = new \stdClass();
$b->id = 2;
$b->title = 'Ervin Howell';
$b->email = '[email protected]';

$array = [
$a,
$b
];

$plainArray = ArrayConverter::convertToPlainArray($array);
foreach ($plainArray as $item) {
$this->assertTrue(is_array($item));
}
}
}
Loading

0 comments on commit 870b984

Please sign in to comment.