-
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 8, 2018
1 parent
9ba7782
commit 870b984
Showing
10 changed files
with
360 additions
and
10 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
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 | ||
{ | ||
} |
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
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); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.