/** * @covers TheSportsDb\Entity\EntityPropertyUtil::getRawValue */ public function testGetRawValue() { // Simple value. $this->assertEquals('test', EntityPropertyUtil::getRawValue('test')); // Complex value. $mock = $this->getMockBuilder('\\stdClass')->setMethods(array('raw'))->getMock(); $mock->expects($this->once())->method('raw')->willReturn('rawVal'); $this->assertEquals('rawVal', EntityPropertyUtil::getRawValue($mock)); // Simple array. $value = array('test1', 'test2', 'test3'); $this->assertEquals($value, EntityPropertyUtil::getRawValue($value)); // Complex array. $mock1 = $this->getMockBuilder('\\stdClass')->setMethods(array('raw'))->getMock(); $mock1->expects($this->once())->method('raw')->willReturn('rawVal1'); $mock2 = $this->getMockBuilder('\\stdClass')->setMethods(array('raw'))->getMock(); $mock2->expects($this->once())->method('raw')->willReturn('rawVal2'); $values = array($mock1, $mock2); $expected = array('rawVal1', 'rawVal2'); $this->assertEquals($expected, EntityPropertyUtil::getRawValue($values)); }
/** * {@inheritdoc} */ public function raw() { if (isset($this->_raw)) { return $this->_raw; } $this->_raw = new \stdClass(); $reflection = new \ReflectionClass($this); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { // Skip static methods. if ($method->isStatic()) { continue; } $methodName = $method->getName(); if (strpos($methodName, 'get') === 0) { $prop = lcfirst(substr($methodName, 3)); if (property_exists($this, $prop)) { $val = $this->{$methodName}(); $this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val); } } } return $this->_raw; }
/** * {@inheritdoc} */ public function raw() { if ($this->entity) { $this->_raw = $this->entity->raw(); return $this->_raw; } if (!isset($this->_raw)) { $this->_raw = new \stdClass(); } $reflection = new \ReflectionClass($this); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { $methodName = $method->getName(); if (strpos($methodName, 'get') === 0) { $prop = lcfirst(substr($methodName, 3)); if (isset($this->properties->{$prop}) && !property_exists($this->_raw, $prop)) { $this->_raw->{$prop} = NULL; $val = $this->{$methodName}(); $this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val); } } } return $this->_raw; }