Skip to content

Commit

Permalink
Simplified getArrayCopy method
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Guigand authored and Bob Kruithof committed Sep 2, 2018
1 parent 76c50e8 commit 26141f9
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Delegate/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
trait ArraySerializable
{
<<<<<<< HEAD
/**
* Will loop through class properties and try and assign their values to an array of data that will be returned.
*
Expand Down Expand Up @@ -62,3 +63,41 @@ public function getArrayCopy()
return $data;
}
}
=======
/**
* Will loop through class properties and try and assign their values to an array of data that will be returned.
*
* @return array
*/
public function getArrayCopy()
{
$data = [];
$properties = get_object_vars( $this );
$reflectionClass = new \Zend_Reflection_Class( $this );
foreach( $properties as $property => $value ) {
// Skip property if it is not accessible
if( !$reflectionClass->hasProperty( $property ) ) continue;

// Only process properties that aren't null
if( !is_null( $value ) ) {
$object = ( is_object( $value ) && !$this->$property instanceof Expr );

// Prepend 'get' to the getter method.
$getter = 'get' . ucfirst( $property );
if ( !method_exists($this, $getter) ) {
// If 'getSomething' doesn't exist, try to use 'isSomething'
$getter = 'is' . ucfirst( $property );
}

// Abort if none of the above methods exit
if( !method_exists( $this, $getter ) ) continue;

// Assign the contents of the property to the data array
$data[ $property ] = $object ? $this->$getter()->getArrayCopy() : $this->$getter();
}
}

return $data;
}
}
>>>>>>> 6d743af... Simplified getArrayCopy method
131 changes: 131 additions & 0 deletions test/unit/Delegate/ArraySerializableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
class ArraySerializableTest extends \PHPUnit_Framework_TestCase
{
<<<<<<< HEAD
/**
* @var A
*/
Expand Down Expand Up @@ -39,6 +40,46 @@ public function testSuperclass()
$result = $this->classA->getArrayCopy();
ksort($result);
self::assertSame($expected, $result);
=======
/**
* @var A
*/
private $classA;

/**
* @var B
*/
private $classB;

/**
*
*/
public function setUp()
{
$this->classA = new A( 1, 2 );
$this->classB = new B( 3, 4, 5, 6 );
}

/**
*
*/
public function testSuperclass()
{
$expected = [ 'a' => 1, 'b' => 2 ];
ksort( $expected );
$result = $this->classA->getArrayCopy();
ksort( $result );
self::assertSame( $expected, $result );
}

public function testBoolean()
{
$expected = [ 'a' => true, 'b' => 2 ];
$class = new A(true, 2);
ksort( $expected );
$result = $class->getArrayCopy();
ksort( $result );
>>>>>>> 6d743af... Simplified getArrayCopy method
}
}

Expand All @@ -48,6 +89,7 @@ public function testSuperclass()
*/
class A
{
<<<<<<< HEAD
use ArraySerializable;

/**
Expand Down Expand Up @@ -135,6 +177,95 @@ public function getY()
{
return $this->y;
}
=======
use ArraySerializable;

/**
* @var bool|int
*/
protected $a;

/**
* @var int
*/
protected $b;

/**
*
* should not show (private)
* @var int
*/
private $x;

/**
* should not show (private)
* @var int
*/
private $y;

/**
* A constructor.
*
* @param $a int
* @param $b int
*/
public function __construct( $a, $b )
{
$this->a = $a;
$this->b = $b;
}

/**
* @return int
*/
public function isA()
{
return $this->a;
}

/**
* @param int $a
*/
public function setA( $a )
{
$this->a = $a;
}

/**
* @return int
*/
public function getB()
{
return $this->b;
}

/**
* @param int $b
*/
public function setB( $b )
{
$this->b = $b;
}

/**
* @return int
*/
public function getX()
{
return $this->x;
}

/**
* this method should never be called by jsonSerialize because it is not
* a boolean.
*
* @return int
*/
public function getY()
{
return $this->y;
}
>>>>>>> 6d743af... Simplified getArrayCopy method
}

/**
Expand Down

0 comments on commit 26141f9

Please sign in to comment.