/** * {@inheritdoc} */ public function setData($field, $object, $value) { if (!is_object($object)) { throw new DataMappingException('Reflection mapper needs object to retrieve data.'); } $objectReflection = ReflectionClass::factory($object); $camelField = $this->camelize($field); $setter = 'set' . $camelField; $adder = 'add' . $camelField; if ($objectReflection->hasMethod($setter)) { if (!$objectReflection->getMethod($setter)->isPublic()) { throw new DataMappingException(sprintf('Method "%s()" is not public in class "%s"', $setter, $objectReflection->name)); } return $object->{$setter}($value); } if ($objectReflection->hasMethod($adder)) { if (!$objectReflection->getMethod($adder)->isPublic()) { throw new DataMappingException(sprintf('Method "%s()" is not public in class "%s"', $adder, $objectReflection->name)); } return $object->{$adder}($value); } if ($objectReflection->hasProperty($field)) { if (!$objectReflection->getProperty($field)->isPublic()) { throw new DataMappingException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "%s()" or "%s()"?', $field, $objectReflection->name, $setter, $adder)); } $property = $objectReflection->getProperty($field); return $property->setValue($object, $value); } throw new DataMappingException(sprintf('Neither property "%s" nor method "%s()" exists in class "%s"', $setter, $adder, $objectReflection->name)); }
public function testGetParentClass() { $classFsiReflection = ReflectionClass::factory('FSi\\Component\\Reflection\\Tests\\Fixture\\ClassAParentParent'); $parentClassFsiReflection = $classFsiReflection->getParentClass(); $classReflection = new \ReflectionClass('FSi\\Component\\Reflection\\Tests\\Fixture\\ClassAParentParent'); $parentClassReflection = $classReflection->getParentClass(); $this->assertSame($parentClassFsiReflection, $parentClassReflection); $classFsiReflection1 = ReflectionClass::factory('FSi\\Component\\Reflection\\Tests\\Fixture\\ClassA'); $parentClassFsiReflection1 = $classFsiReflection1->getParentClass(); $classReflection1 = new \ReflectionClass('FSi\\Component\\Reflection\\Tests\\Fixture\\ClassA'); $parentClassReflection1 = $classReflection1->getParentClass(); $this->assertSame($parentClassFsiReflection1->name, $parentClassReflection1->name); }