123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Serializer\Tests\Normalizer;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
- use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
- use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
- use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
- use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
- use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
- use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
- use Symfony\Component\Serializer\Serializer;
- use Symfony\Component\Serializer\SerializerInterface;
- use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
- use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
- use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
- use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
- use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
- use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
- use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
- /**
- * @author Kévin Dunglas <dunglas@gmail.com>
- */
- class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var ObjectNormalizer
- */
- private $normalizer;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- protected function setUp()
- {
- $this->serializer = $this->getMockBuilder(__NAMESPACE__.'\ObjectSerializerNormalizer')->getMock();
- $this->normalizer = new ObjectNormalizer();
- $this->normalizer->setSerializer($this->serializer);
- }
- public function testNormalize()
- {
- $obj = new ObjectDummy();
- $object = new \stdClass();
- $obj->setFoo('foo');
- $obj->bar = 'bar';
- $obj->setBaz(true);
- $obj->setCamelCase('camelcase');
- $obj->setObject($object);
- $this->serializer
- ->expects($this->once())
- ->method('normalize')
- ->with($object, 'any')
- ->will($this->returnValue('string_object'))
- ;
- $this->assertEquals(
- array(
- 'foo' => 'foo',
- 'bar' => 'bar',
- 'baz' => true,
- 'fooBar' => 'foobar',
- 'camelCase' => 'camelcase',
- 'object' => 'string_object',
- ),
- $this->normalizer->normalize($obj, 'any')
- );
- }
- public function testDenormalize()
- {
- $obj = $this->normalizer->denormalize(
- array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
- __NAMESPACE__.'\ObjectDummy',
- 'any'
- );
- $this->assertEquals('foo', $obj->getFoo());
- $this->assertEquals('bar', $obj->bar);
- $this->assertTrue($obj->isBaz());
- }
- public function testDenormalizeWithObject()
- {
- $data = new \stdClass();
- $data->foo = 'foo';
- $data->bar = 'bar';
- $data->fooBar = 'foobar';
- $obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectDummy', 'any');
- $this->assertEquals('foo', $obj->getFoo());
- $this->assertEquals('bar', $obj->bar);
- }
- public function testDenormalizeNull()
- {
- $this->assertEquals(new ObjectDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\ObjectDummy'));
- }
- public function testConstructorDenormalize()
- {
- $obj = $this->normalizer->denormalize(
- array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
- __NAMESPACE__.'\ObjectConstructorDummy', 'any');
- $this->assertEquals('foo', $obj->getFoo());
- $this->assertEquals('bar', $obj->bar);
- $this->assertTrue($obj->isBaz());
- }
- public function testConstructorDenormalizeWithNullArgument()
- {
- $obj = $this->normalizer->denormalize(
- array('foo' => 'foo', 'bar' => null, 'baz' => true),
- __NAMESPACE__.'\ObjectConstructorDummy', 'any');
- $this->assertEquals('foo', $obj->getFoo());
- $this->assertNull($obj->bar);
- $this->assertTrue($obj->isBaz());
- }
- public function testConstructorDenormalizeWithMissingOptionalArgument()
- {
- $obj = $this->normalizer->denormalize(
- array('foo' => 'test', 'baz' => array(1, 2, 3)),
- __NAMESPACE__.'\ObjectConstructorOptionalArgsDummy', 'any');
- $this->assertEquals('test', $obj->getFoo());
- $this->assertEquals(array(), $obj->bar);
- $this->assertEquals(array(1, 2, 3), $obj->getBaz());
- }
- public function testConstructorDenormalizeWithOptionalDefaultArgument()
- {
- $obj = $this->normalizer->denormalize(
- array('bar' => 'test'),
- __NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any');
- $this->assertEquals(array(), $obj->getFoo());
- $this->assertEquals('test', $obj->getBar());
- }
- public function testConstructorWithObjectDenormalize()
- {
- $data = new \stdClass();
- $data->foo = 'foo';
- $data->bar = 'bar';
- $data->baz = true;
- $data->fooBar = 'foobar';
- $obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectConstructorDummy', 'any');
- $this->assertEquals('foo', $obj->getFoo());
- $this->assertEquals('bar', $obj->bar);
- }
- public function testGroupsNormalize()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory);
- $this->normalizer->setSerializer($this->serializer);
- $obj = new GroupDummy();
- $obj->setFoo('foo');
- $obj->setBar('bar');
- $obj->setFooBar('fooBar');
- $obj->setSymfony('symfony');
- $obj->setKevin('kevin');
- $obj->setCoopTilleuls('coopTilleuls');
- $this->assertEquals(array(
- 'bar' => 'bar',
- ), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('c'))));
- $this->assertEquals(array(
- 'symfony' => 'symfony',
- 'foo' => 'foo',
- 'fooBar' => 'fooBar',
- 'bar' => 'bar',
- 'kevin' => 'kevin',
- 'coopTilleuls' => 'coopTilleuls',
- ), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('a', 'c'))));
- }
- public function testGroupsDenormalize()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory);
- $this->normalizer->setSerializer($this->serializer);
- $obj = new GroupDummy();
- $obj->setFoo('foo');
- $toNormalize = array('foo' => 'foo', 'bar' => 'bar');
- $normalized = $this->normalizer->denormalize(
- $toNormalize,
- 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
- null,
- array(ObjectNormalizer::GROUPS => array('a'))
- );
- $this->assertEquals($obj, $normalized);
- $obj->setBar('bar');
- $normalized = $this->normalizer->denormalize(
- $toNormalize,
- 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
- null,
- array(ObjectNormalizer::GROUPS => array('a', 'b'))
- );
- $this->assertEquals($obj, $normalized);
- }
- public function testNormalizeNoPropertyInGroup()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory);
- $this->normalizer->setSerializer($this->serializer);
- $obj = new GroupDummy();
- $obj->setFoo('foo');
- $this->assertEquals(array(), $this->normalizer->normalize($obj, null, array('groups' => array('notExist'))));
- }
- public function testGroupsNormalizeWithNameConverter()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
- $this->normalizer->setSerializer($this->serializer);
- $obj = new GroupDummy();
- $obj->setFooBar('@dunglas');
- $obj->setSymfony('@coopTilleuls');
- $obj->setCoopTilleuls('les-tilleuls.coop');
- $this->assertEquals(
- array(
- 'bar' => null,
- 'foo_bar' => '@dunglas',
- 'symfony' => '@coopTilleuls',
- ),
- $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('name_converter')))
- );
- }
- public function testGroupsDenormalizeWithNameConverter()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
- $this->normalizer->setSerializer($this->serializer);
- $obj = new GroupDummy();
- $obj->setFooBar('@dunglas');
- $obj->setSymfony('@coopTilleuls');
- $this->assertEquals(
- $obj,
- $this->normalizer->denormalize(array(
- 'bar' => null,
- 'foo_bar' => '@dunglas',
- 'symfony' => '@coopTilleuls',
- 'coop_tilleuls' => 'les-tilleuls.coop',
- ), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(ObjectNormalizer::GROUPS => array('name_converter')))
- );
- }
- /**
- * @dataProvider provideCallbacks
- */
- public function testCallbacks($callbacks, $value, $result, $message)
- {
- $this->normalizer->setCallbacks($callbacks);
- $obj = new ObjectConstructorDummy('', $value, true);
- $this->assertEquals(
- $result,
- $this->normalizer->normalize($obj, 'any'),
- $message
- );
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testUncallableCallbacks()
- {
- $this->normalizer->setCallbacks(array('bar' => null));
- $obj = new ObjectConstructorDummy('baz', 'quux', true);
- $this->normalizer->normalize($obj, 'any');
- }
- public function testIgnoredAttributes()
- {
- $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
- $obj = new ObjectDummy();
- $obj->setFoo('foo');
- $obj->bar = 'bar';
- $obj->setBaz(true);
- $this->assertEquals(
- array('fooBar' => 'foobar'),
- $this->normalizer->normalize($obj, 'any')
- );
- }
- public function testIgnoredAttributesDenormalize()
- {
- $this->normalizer->setIgnoredAttributes(array('fooBar', 'bar', 'baz'));
- $obj = new ObjectDummy();
- $obj->setFoo('foo');
- $this->assertEquals(
- $obj,
- $this->normalizer->denormalize(array('fooBar' => 'fooBar', 'foo' => 'foo', 'baz' => 'baz'), __NAMESPACE__.'\ObjectDummy')
- );
- }
- public function provideCallbacks()
- {
- return array(
- array(
- array(
- 'bar' => function ($bar) {
- return 'baz';
- },
- ),
- 'baz',
- array('foo' => '', 'bar' => 'baz', 'baz' => true),
- 'Change a string',
- ),
- array(
- array(
- 'bar' => function ($bar) {
- return;
- },
- ),
- 'baz',
- array('foo' => '', 'bar' => null, 'baz' => true),
- 'Null an item',
- ),
- array(
- array(
- 'bar' => function ($bar) {
- return $bar->format('d-m-Y H:i:s');
- },
- ),
- new \DateTime('2011-09-10 06:30:00'),
- array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
- 'Format a date',
- ),
- array(
- array(
- 'bar' => function ($bars) {
- $foos = '';
- foreach ($bars as $bar) {
- $foos .= $bar->getFoo();
- }
- return $foos;
- },
- ),
- array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
- array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
- 'Collect a property',
- ),
- array(
- array(
- 'bar' => function ($bars) {
- return count($bars);
- },
- ),
- array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
- array('foo' => '', 'bar' => 2, 'baz' => true),
- 'Count a property',
- ),
- );
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\LogicException
- * @expectedExceptionMessage Cannot normalize attribute "object" because the injected serializer is not a normalizer
- */
- public function testUnableToNormalizeObjectAttribute()
- {
- $serializer = $this->getMockBuilder('Symfony\Component\Serializer\SerializerInterface')->getMock();
- $this->normalizer->setSerializer($serializer);
- $obj = new ObjectDummy();
- $object = new \stdClass();
- $obj->setObject($object);
- $this->normalizer->normalize($obj, 'any');
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
- */
- public function testUnableToNormalizeCircularReference()
- {
- $serializer = new Serializer(array($this->normalizer));
- $this->normalizer->setSerializer($serializer);
- $this->normalizer->setCircularReferenceLimit(2);
- $obj = new CircularReferenceDummy();
- $this->normalizer->normalize($obj);
- }
- public function testSiblingReference()
- {
- $serializer = new Serializer(array($this->normalizer));
- $this->normalizer->setSerializer($serializer);
- $siblingHolder = new SiblingHolder();
- $expected = array(
- 'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
- 'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
- 'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
- );
- $this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
- }
- 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));
- }
- public function testDenormalizeNonExistingAttribute()
- {
- $this->assertEquals(
- new ObjectDummy(),
- $this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\ObjectDummy')
- );
- }
- public function testNoTraversableSupport()
- {
- $this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
- }
- public function testNormalizeStatic()
- {
- $this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
- }
- public function testNormalizeNotSerializableContext()
- {
- $objectDummy = new ObjectDummy();
- $expected = array(
- 'foo' => null,
- 'baz' => null,
- 'fooBar' => '',
- 'camelCase' => null,
- 'object' => null,
- 'bar' => null,
- );
- $this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, array('not_serializable' => function () {
- })));
- }
- public function testMaxDepth()
- {
- $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
- $this->normalizer = new ObjectNormalizer($classMetadataFactory);
- $serializer = new Serializer(array($this->normalizer));
- $this->normalizer->setSerializer($serializer);
- $level1 = new MaxDepthDummy();
- $level1->foo = 'level1';
- $level2 = new MaxDepthDummy();
- $level2->foo = 'level2';
- $level1->child = $level2;
- $level3 = new MaxDepthDummy();
- $level3->foo = 'level3';
- $level2->child = $level3;
- $result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
- $expected = array(
- 'bar' => null,
- 'foo' => 'level1',
- 'child' => array(
- 'bar' => null,
- 'foo' => 'level2',
- 'child' => array(
- 'bar' => null,
- 'child' => null,
- ),
- ),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
- */
- public function testThrowUnexpectedValueException()
- {
- $this->normalizer->denormalize(array('foo' => 'bar'), ObjectTypeHinted::class);
- }
- public function testDenomalizeRecursive()
- {
- $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
- $normalizer = new ObjectNormalizer(null, null, null, $extractor);
- $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
- $obj = $serializer->denormalize(array(
- 'inner' => array('foo' => 'foo', 'bar' => 'bar'),
- 'date' => '1988/01/21',
- 'inners' => array(array('foo' => 1), array('foo' => 2)),
- ), ObjectOuter::class);
- $this->assertSame('foo', $obj->getInner()->foo);
- $this->assertSame('bar', $obj->getInner()->bar);
- $this->assertSame('1988-01-21', $obj->getDate()->format('Y-m-d'));
- $this->assertSame(1, $obj->getInners()[0]->foo);
- $this->assertSame(2, $obj->getInners()[1]->foo);
- }
- public function testAcceptJsonNumber()
- {
- $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
- $normalizer = new ObjectNormalizer(null, null, null, $extractor);
- $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
- $this->assertSame(10.0, $serializer->denormalize(array('number' => 10), JsonNumber::class, 'json')->number);
- $this->assertSame(10.0, $serializer->denormalize(array('number' => 10), JsonNumber::class, 'jsonld')->number);
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
- * @expectedExceptionMessage The type of the "date" attribute for class "Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter" must be one of "DateTimeInterface" ("string" given).
- */
- public function testRejectInvalidType()
- {
- $normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
- $serializer = new Serializer(array($normalizer));
- $serializer->denormalize(array('date' => 'foo'), ObjectOuter::class);
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
- * @expectedExceptionMessage The type of the key "a" must be "int" ("string" given).
- */
- public function testRejectInvalidKey()
- {
- $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor()));
- $normalizer = new ObjectNormalizer(null, null, null, $extractor);
- $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer));
- $serializer->denormalize(array('inners' => array('a' => array('foo' => 1))), ObjectOuter::class);
- }
- public function testExtractAttributesRespectsFormat()
- {
- $normalizer = new FormatAndContextAwareNormalizer();
- $data = new ObjectDummy();
- $data->setFoo('bar');
- $data->bar = 'foo';
- $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, 'foo_and_bar_included'));
- }
- public function testExtractAttributesRespectsContext()
- {
- $normalizer = new FormatAndContextAwareNormalizer();
- $data = new ObjectDummy();
- $data->setFoo('bar');
- $data->bar = 'foo';
- $this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
- }
- }
- class ObjectDummy
- {
- protected $foo;
- public $bar;
- private $baz;
- protected $camelCase;
- protected $object;
- public function getFoo()
- {
- return $this->foo;
- }
- public function setFoo($foo)
- {
- $this->foo = $foo;
- }
- public function isBaz()
- {
- return $this->baz;
- }
- public function setBaz($baz)
- {
- $this->baz = $baz;
- }
- public function getFooBar()
- {
- return $this->foo.$this->bar;
- }
- public function getCamelCase()
- {
- return $this->camelCase;
- }
- public function setCamelCase($camelCase)
- {
- $this->camelCase = $camelCase;
- }
- public function otherMethod()
- {
- throw new \RuntimeException('Dummy::otherMethod() should not be called');
- }
- public function setObject($object)
- {
- $this->object = $object;
- }
- public function getObject()
- {
- return $this->object;
- }
- }
- class ObjectConstructorDummy
- {
- protected $foo;
- public $bar;
- private $baz;
- public function __construct($foo, $bar, $baz)
- {
- $this->foo = $foo;
- $this->bar = $bar;
- $this->baz = $baz;
- }
- public function getFoo()
- {
- return $this->foo;
- }
- public function isBaz()
- {
- return $this->baz;
- }
- public function otherMethod()
- {
- throw new \RuntimeException('Dummy::otherMethod() should not be called');
- }
- }
- abstract class ObjectSerializerNormalizer implements SerializerInterface, NormalizerInterface
- {
- }
- class ObjectConstructorOptionalArgsDummy
- {
- protected $foo;
- public $bar;
- private $baz;
- public function __construct($foo, $bar = array(), $baz = array())
- {
- $this->foo = $foo;
- $this->bar = $bar;
- $this->baz = $baz;
- }
- public function getFoo()
- {
- return $this->foo;
- }
- public function getBaz()
- {
- return $this->baz;
- }
- public function otherMethod()
- {
- throw new \RuntimeException('Dummy::otherMethod() should not be called');
- }
- }
- class ObjectConstructorArgsWithDefaultValueDummy
- {
- protected $foo;
- protected $bar;
- public function __construct($foo = array(), $bar)
- {
- $this->foo = $foo;
- $this->bar = $bar;
- }
- public function getFoo()
- {
- return $this->foo;
- }
- public function getBar()
- {
- return $this->bar;
- }
- public function otherMethod()
- {
- throw new \RuntimeException('Dummy::otherMethod() should not be called');
- }
- }
- class ObjectWithStaticPropertiesAndMethods
- {
- public $foo = 'K';
- public static $bar = 'A';
- public static function getBaz()
- {
- return 'L';
- }
- }
- class ObjectTypeHinted
- {
- public function setFoo(array $f)
- {
- }
- }
- class ObjectOuter
- {
- private $inner;
- private $date;
- /**
- * @var ObjectInner[]
- */
- private $inners;
- public function getInner()
- {
- return $this->inner;
- }
- public function setInner(ObjectInner $inner)
- {
- $this->inner = $inner;
- }
- public function setDate(\DateTimeInterface $date)
- {
- $this->date = $date;
- }
- public function getDate()
- {
- return $this->date;
- }
- public function setInners(array $inners)
- {
- $this->inners = $inners;
- }
- public function getInners()
- {
- return $this->inners;
- }
- }
- class ObjectInner
- {
- public $foo;
- public $bar;
- }
- class FormatAndContextAwareNormalizer extends ObjectNormalizer
- {
- protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
- {
- if (in_array($attribute, array('foo', 'bar')) && 'foo_and_bar_included' === $format) {
- return true;
- }
- if (in_array($attribute, array('foo', 'bar')) && isset($context['include_foo_and_bar']) && true === $context['include_foo_and_bar']) {
- return true;
- }
- return false;
- }
- }
- class JsonNumber
- {
- /**
- * @var float
- */
- public $number;
- }
|