Skip to content

Commit

Permalink
Improvements to get public properties, and remove bad properties on h…
Browse files Browse the repository at this point in the history
…ydrate
  • Loading branch information
bajb committed Apr 15, 2015
1 parent 85040f3 commit dec7a92
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions inc/GlobalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,15 @@ function idp($object, $property, $default = null)
* will return protected and private properties,
* this function fixes this instance
*
* @param object $object Source object
* @param object $object Source object
* @param bool $returnKeys Return Property keys
*
* @return mixed
*/
function get_public_properties($object)
function get_public_properties($object, $returnKeys = false)
{
return get_object_vars($object);
$properties = get_object_vars($object);
return $returnKeys ? array_keys($properties) : $properties;
}
}

Expand Down Expand Up @@ -708,6 +710,7 @@ function hydrate($destination, $source, array $properties, $copyNull = true)
throw new Exception("hydrate() must be given objects");
}

$properties = array_filter($properties);
foreach($properties as $property)
{
$newVal = idp($source, $property);
Expand Down
4 changes: 4 additions & 0 deletions tests/GlobalFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public function testProperties()
$this->assertEquals($expect, $class->publicVars());
$this->assertEquals($expect, get_object_vars($class));
$this->assertEquals($expect, get_public_properties($class));
$this->assertEquals(['name', 'age'], get_public_properties($class, true));
}

public function testStringFrom()
Expand Down Expand Up @@ -380,6 +381,9 @@ public function testHydrate()
$source->age = 19;
$source->nullify = null;

hydrate($dest, $source, [null]);
$this->assertEquals('Please', $dest->nullify);

hydrate($dest, $source, ['nullify'], false);
$this->assertEquals('Please', $dest->nullify);

Expand Down

0 comments on commit dec7a92

Please sign in to comment.