public function hydrate($source) { if (is_object($source)) { $source = Objects::propertyValues($source); } foreach (Objects::properties($this) as $property) { if (isset($source[$property])) { $this->{$property} = $source[$property]; } } return $this; }
public function validate(array $properties = null, $throw = true) { $allValid = true; if ($properties === null) { $properties = Objects::properties($this->_payload); } $this->_properties = $properties; foreach ($properties as $property) { $block = DocBlockParser::fromProperty($this->_payload, $property); $nullable = $block->hasTag('nullable'); $optional = $block->hasTag('optional'); $val = $this->_payload->{$property}; if ($val === null && ($nullable || $optional) || $val === '' && $optional) { continue; } foreach ($block->getTags() as $tag => $tags) { foreach ($tags as $opt) { if ($this->_repair) { $this->repairValue($tag, $property, $val, $opt); } try { $this->runValidator($tag, $property, $val, $opt); } catch (\Exception $e) { if ($throw) { throw $e; } else { $allValid = false; if (!isset($this->_errors[$property])) { $this->_errors[$property] = []; } $this->_errors[$property][] = $e->getMessage(); } } } } } return $allValid; }
/** * Return an array with only the public properties and their values. * * If calling get_object_vars withing a class, * will return protected and private properties, * this function fixes this instance * * @param object $object Source object * @param bool $returnKeys Return Property keys * * @return mixed * * @deprecated */ function get_public_properties($object, $returnKeys = false) { return $returnKeys ? \Packaged\Helpers\Objects::properties($object) : \Packaged\Helpers\Objects::propertyValues($object); }
public function testProperties() { $expect = ['name' => null, 'age' => null]; $class = new PropertyClass(); $this->assertNotEquals($expect, $class->objectVars()); $this->assertEquals($expect, $class->publicVars()); $this->assertEquals($expect, get_object_vars($class)); $this->assertEquals($expect, Objects::propertyValues($class)); $this->assertEquals(['name', 'age'], Objects::properties($class)); }
/** * Hydrate the public properties * * @param $data * * @return $this * @throws \Exception */ public function hydrate($data) { Objects::hydrate($this, ValueAs::obj($data), Objects::properties($this), false); return $this; }