/** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = array()) { if ($object instanceof \Exception) { return $this->normalizeException($object); } return parent::normalize($object, $format, $context); }
public function testIgnoredAttributes() { $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase')); $obj = new GetSetDummy(); $obj->setFoo('foo'); $obj->setBar('bar'); $this->assertEquals(array('fooBar' => 'foobar'), $this->normalizer->normalize($obj, 'any')); }
/** * @expectedException \LogicException * @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer */ public function testUnableToNormalizeObjectAttribute() { $serializer = $this->getMock('Symfony\\Component\\Serializer\\SerializerInterface'); $this->normalizer->setSerializer($serializer); $obj = new GetSetDummy(); $object = new \stdClass(); $obj->setObject($object); $this->normalizer->normalize($obj, 'any'); }
public function testCircularReferenceHandler() { $serializer = new Serializer(array($this->normalizer)); $this->normalizer->setSerializer($serializer); $this->normalizer->setCircularReferenceHandler(function ($obj) { return get_class($obj); }); $obj = new CircularReferenceDummy(); $expected = array('me' => 'Symfony\\Component\\Serializer\\Tests\\Fixtures\\CircularReferenceDummy'); $this->assertEquals($expected, $this->normalizer->normalize($obj)); }
/** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = array()) { $camelizedKeyAttributes = parent::normalize($object, $format, $context); $attributes = array(); foreach ($camelizedKeyAttributes as $name => $value) { if (!isset($value)) { continue; } $attributes[$this->fromCamelCase($name)] = $value; } return $attributes; }
public function normalize($object, $format = null, array $context = array()) { // Look for setters with non-primitive parameters $reflectionObject = new \ReflectionObject($object); $reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC); $attributes = array(); foreach ($reflectionMethods as $method) { $params = $method->getParameters(); if (0 === strpos($method->name, 'set') && 3 < strlen($method->name) && 1 === $method->getNumberOfParameters() && $params[0]->getClass() !== NULL) { $attributeName = lcfirst(substr($method->name, 3)); $attributes[] = $attributeName; } } $this->setIgnoredAttributes($attributes); return parent::normalize($object, $format, $context); }