Skip to content

Commit

Permalink
Simplified getArrayCopy method
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Delegate/ArraySerializable.php
#	test/unit/Delegate/ArraySerializableTest.php
  • Loading branch information
halfpastfouram authored and Bob Kruithof committed Sep 2, 2018
1 parent ac39cc7 commit bb6b71a
Showing 1 changed file with 16 additions and 56 deletions.
72 changes: 16 additions & 56 deletions src/Delegate/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

/**
* Class ArraySerializable
*
* @package Halfpastfour\PHPChartJS\Model
*/
trait ArraySerializable
{
<<<<<<< HEAD
/**
* Will loop through class properties and try and assign their values to an array of data that will be returned.
*
* @return array
* @throws \ReflectionException
* @throws \Zend_Reflection_Exception
*/
public function getArrayCopy()
{
Expand All @@ -31,76 +30,37 @@ public function getArrayCopy()

// Only process properties that aren't null
if (! is_null($value)) {
$object = (is_object($value) && ! $this->$property instanceof Expr);
// Gather phpdoc from property
$phpDoc = $reflectionClass->getProperty($property)->getDocComment();
$type = $phpDoc->getTag('var')->getDescription();
$object = false;

// 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);
if (is_object($value) && ! $this->$property instanceof Expr) {
$object = true;
}

// Finally simply use the property
if (! method_exists($this, $getter)) {
// Determine whether the getter method is equal to the property name or is prepended by 'is' or 'get'
if (strcmp($type, 'bool') === 0 || strcmp($type, 'boolean') === 0) {
// Prepend 'is' to the getter method
$getter = 'is' . ucfirst($property);
} elseif (method_exists($this, $property) && is_object($value)) {
// The getter method is equal to the property name and the value is an actual object
$getter = $property;
$object = true;
}

// Abort if none of the above methods exit
// Abort if the method does not exist
if (! method_exists($this, $getter)) {
continue;
}

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

return $data;
}
<<<<<<< HEAD
}
=======
/**
* 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 );
}
// Finally simply use the property
if( !method_exists($this, $getter) ) {
$getter = $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
=======
}
>>>>>>> e69d8b3... Add unit test making sure the ArraySerializable trait is working well.

0 comments on commit bb6b71a

Please sign in to comment.