handler = new XmlSchemaDateHandler();
$this->context = DeserializationContext::create();
$naming = new IdenticalPropertyNamingStrategy();
$dispatcher = new EventDispatcher();
$handlerRegistry= new HandlerRegistry();
$cons = new UnserializeObjectConstructor();
$navigator = class_exists('JMS\Serializer\GraphNavigator\DeserializationGraphNavigator')
? $this->initJmsv2($naming, $handlerRegistry, $cons, $dispatcher)
: $this->initJmsv1($naming, $handlerRegistry, $cons, $dispatcher)
;
$this->visitor->setNavigator($navigator);
}
private function initJmsv2($naming, $handlerRegistry, $cons, $dispatcher)
{
$accessor = new DefaultAccessorStrategy();
$this->visitor = new XmlDeserializationVisitor();
$metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader(), $naming));
return new GraphNavigator\DeserializationGraphNavigator($metadataFactory, $handlerRegistry, $cons, $accessor, $dispatcher);
}
private function initJmsv1($naming, $handlerRegistry, $cons, $dispatcher)
{
$this->visitor = new XmlDeserializationVisitor($naming);
$metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
return new GraphNavigator($metadataFactory, $handlerRegistry, $cons, $dispatcher);
}
/**
* @dataProvider getDeserializeDate
* @param string $date
* @param \DateTime $expected
*/
public function testDeserializeDate($date, \DateTime $expected)
{
$element = new \SimpleXMLElement("$date");
$deserialized = $this->handler->deserializeDate($this->visitor, $element, [], $this->context);
$this->assertEquals($expected, $deserialized);
}
public function getDeserializeDate()
{
return [
['2015-01-01', new \DateTime('2015-01-01')],
['2015-01-01Z', new \DateTime('2015-01-01', new \DateTimeZone("UTC"))],
['2015-01-01+06:00', new \DateTime('2015-01-01', new \DateTimeZone("+06:00"))],
['2015-01-01-20:00', new \DateTime('2015-01-01', new \DateTimeZone("-20:00"))],
];
}
/**
* @expectedException \RuntimeException
*/
public function testDeserializeInvalidDate()
{
$element = new \SimpleXMLElement("2015-01-01T");
$this->handler->deserializeDate($this->visitor, $element, [], $this->context);
}
}