-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mauro Cassani
committed
Jan 10, 2018
1 parent
87b45c0
commit f607925
Showing
11 changed files
with
248 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.