Skip to content

Commit

Permalink
Fix on ConsistencyChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Jan 10, 2018
1 parent 87b45c0 commit f607925
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 77 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ $qb = QueryBuilder::create($array);
$qb
->addCriterion('name as n', 'Ervin Howell')
->addCriterion('username as user', 'Antonette')
->addCriterion('address.street as add', 'Victor Plains');
->addCriterion('address.street as street', 'Victor Plains');

foreach ($qb->getResults() as $element){
// ...
// now you have
// $element['n']
// $element['user']
// $element['add']['street']
// $element['street']
}
```

Expand Down
25 changes: 3 additions & 22 deletions src/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

abstract class AbstractFilter
{
Expand All @@ -25,29 +25,10 @@ protected static function getArrayElementValueFromKey($key, $arrayElement)
{
return self::getValueFromKeysArray(
explode(Constants::ARRAY_SEPARATOR, $key),
(is_object($arrayElement)) ? self::convertObjectToArray($arrayElement) : $arrayElement
(is_object($arrayElement)) ? ArrayHelper::convertObjectToArray($arrayElement) : $arrayElement
);
}

/**
* @param $arrayElement
* @return array
*/
private static function convertObjectToArray($arrayElement)
{
$convertedArray = [];

foreach (ArrayConverter::convertToPlainArray($arrayElement) as $key => $element) {
$key = explode("\\", $key);
$key = end($key);
$key = explode("\000", $key);

$convertedArray[end($key)] = $element;
}

return $convertedArray;
}

/**
* @param $keysArray
* @param $arrayElement
Expand All @@ -59,7 +40,7 @@ private static function getValueFromKeysArray($keysArray, $arrayElement)
if (count($keysArray)>1) {
$key = array_shift($keysArray);

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

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

namespace ArrayQuery\Filters\Criterion;

use ArrayQuery\Helpers\ArrayConverter;
use ArrayQuery\Helpers\ArrayHelper;

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

namespace ArrayQuery\Filters\Criterion;

use ArrayQuery\Helpers\ArrayConverter;
use ArrayQuery\Helpers\ArrayHelper;

class InArrayInversedFilter implements FilterInterface
{
Expand All @@ -21,6 +21,6 @@ class InArrayInversedFilter implements FilterInterface
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
return in_array($valueToCompare, ArrayConverter::convertToPlainArray($value));
return in_array($valueToCompare, ArrayHelper::convertToPlainArray($value));
}
}
32 changes: 0 additions & 32 deletions src/Helpers/ArrayConverter.php

This file was deleted.

111 changes: 111 additions & 0 deletions src/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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 ArrayHelper
{
/**
* @param $firstArray
* @param $secondArray
* @return bool
*/
public static function checkIfTwoArraysAreConsistent($firstArray, $secondArray)
{
if (count(array_diff($firstArray, $secondArray)) > 0 || count(array_diff($secondArray, $firstArray)) > 0) {
return false;
}

return true;
}

/**
* @param $itemToCompare
* @param $element
* @return bool
*/
public static function compareElementToItemKeyMap($itemToCompare, $element)
{
foreach ($itemToCompare as $key => $item){
if(is_array($item) && ArrayHelper::isAnAssociativeArray($item)){
if (false === ArrayHelper::checkIfTwoArraysAreConsistent(array_keys($item), array_keys($element[$key]))) {
return false;
}

return self::compareElementToItemKeyMap($item, $element[$key]);
}
}
}

/**
* @param $arrayElement
* @return array
*/
public static function convertObjectToArray($arrayElement)
{
$convertedArray = [];

foreach (self::convertToPlainArray($arrayElement) as $key => $element) {
$key = explode("\\", $key);
$key = end($key);
$key = explode("\000", $key);

$convertedArray[end($key)] = $element;
}

return $convertedArray;
}

/**
* @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);
}

/**
* @param $key
* @param $array
* @return mixed
*/
public static function getValueFromNestedArray(array $key, array $array)
{
array_shift($key);
$value = $array[$key[0]];

if(is_array($value)){
return self::getValueFromNestedArray($key, $value);
}

return $value;
}

/**
* @param array $array
* @return bool
*/
public static function isAnAssociativeArray(array $array)
{
if ([] === $array) return false;

return array_keys($array) !== range(0, count($array) - 1);
}
}
13 changes: 8 additions & 5 deletions src/Helpers/ConsistencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ public static function isElementValid($element, $array)
{
foreach ($array as $key => $item) {
if (is_object($item)) {
$array[$key] = ArrayConverter::convertToPlainArray($item);
$array[$key] = ArrayHelper::convertToPlainArray($item);
}
}

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

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

if (count(array_diff($FirstItemKeyMap, $ItemKeyMap)) > 0 || count(array_diff($ItemKeyMap, $FirstItemKeyMap)) > 0) {
if (false === ArrayHelper::checkIfTwoArraysAreConsistent(array_keys($firstItem), array_keys($element))) {
return false;
}

if(false === ArrayHelper::compareElementToItemKeyMap($firstItem, $element)) {
return false;
}

Expand Down
12 changes: 7 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use ArrayQuery\Filters\JoinFilter;
use ArrayQuery\Filters\SortingFilter;
use ArrayQuery\Filters\LimitFilter;
use ArrayQuery\Helpers\ArrayConverter;
use ArrayQuery\Helpers\ArrayHelper;
use ArrayQuery\Helpers\ConsistencyChecker;

class QueryBuilder
Expand Down Expand Up @@ -239,7 +239,7 @@ public function getResults()
{
$results = $this->applySortingFilter($this->applyLimitFilter($this->applyCriteriaFilter($this->applyJoinFilter())));

return array_map([ArrayConverter::class, 'convertToPlainArray'], $results);
return array_map([ArrayHelper::class, 'convertToPlainArray'], $results);
}

/**
Expand Down Expand Up @@ -334,10 +334,12 @@ private function applyCriteriaFilter(array $array)
$oldkey = explode(Constants::ARRAY_SEPARATOR, $key[0]);
$newkey = $key[1];

$result = ArrayConverter::convertToPlainArray($result);
$result[$newkey] = $result[$oldkey[0]];
unset($result[$oldkey[0]]);
$result = ArrayHelper::convertToPlainArray($result);
$oldResult = $result[$oldkey[0]];

$result[$newkey] = (count($oldkey) > 1) ? ArrayHelper::getValueFromNestedArray($oldkey, $oldResult) : $oldResult;
}

return $result;
}, $results);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ArrayConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ArrayQuery\Tests;

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

class ArrayConverterTest extends TestCase
Expand Down Expand Up @@ -45,7 +45,7 @@ public function it_converts_a_plain_array_in_an_object_array()
],
];

$objectArray = ArrayConverter::convertToObjectArray($array);
$objectArray = ArrayHelper::convertToObjectArray($array);
foreach ($objectArray as $item) {
$this->assertInstanceOf(\stdClass::class, $item);
}
Expand All @@ -71,7 +71,7 @@ public function it_converts_an_object_array_in_a_plain_array()
$b
];

$plainArray = ArrayConverter::convertToPlainArray($array);
$plainArray = ArrayHelper::convertToPlainArray($array);
foreach ($plainArray as $item) {
$this->assertTrue(is_array($item));
}
Expand Down
45 changes: 45 additions & 0 deletions tests/ConsistencyCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ public function it_returns_false_if_an_not_consistent_array_is_provided()
$this->assertFalse(ConsistencyChecker::isValid($array));
}

/**
* @test
*/
public function it_returns_false_if_an_not_consistent_array_with_nested_keys_is_provided()
{
$array = [
[
'id' => 1,
'name' => 'Mauro Cassani',
'skills' => [
'web' => [
'php' => 5,
'web_design' => 5,
'css' => 4,
]
],
],
[
'id' => 2,
'name' => 'John Doe',
'skills' => [
'web' => [
'php' => 3,
'web_design' => 1,
'css' => 5,
]
],
],
[
'id' => 3,
'name' => 'Maria Callas',
'skills' => [
'web' => [
'php' => 1,
'web_design' => 3,
'css' => 3,
'extra' => 55,
]
],
],
];

$this->assertFalse(ConsistencyChecker::isValid($array));
}

/**
* @test
*/
Expand Down
Loading

0 comments on commit f607925

Please sign in to comment.