/** * Called for each node of the graph that is being traversed. * * @param mixed $data the data depends on the direction, and type of visitor * @param Type $type array has the format ["name" => string, "params" => array] * @param SerializationContext|DeserializationContext|Context $context * * @return mixed the return value depends on the direction, and type of visitor */ public function accept($data, Type $type = null, Context $context) { // If the data is null, we have to force the type to null regardless of the input in order to // guarantee correct handling of null values, and not have any internal auto-casting behavior. if ($context instanceof SerializationContext && null === $data) { $type = Type::null(); } elseif (null === $type) { if ($context instanceof DeserializationContext) { throw new RuntimeException('The type must be given for all properties when deserializing.'); } $type = $this->guessType($data); } if ($context instanceof SerializationContext) { return $this->serialize($data, $type, $context); } return $this->deserialize($data, $type, $context); }
public function visitDouble($data, Type $type, Context $context) { if ($this->isNullNode($data)) { return $this->visitNull(null, Type::null(), $context); } return parent::visitDouble($data, $type, $context); }
/** * @dataProvider getPrimitiveTypes */ public function testPrimitiveTypes($primitiveType, $data) { $visitor = $this->serializationVisitors['json']; $functionToCall = 'visit' . ucfirst($primitiveType); $result = $visitor->{$functionToCall}($data, Type::null(), $this->createMock(Context::class)); if ($primitiveType == 'double') { $primitiveType = 'float'; } $this->assertInternalType($primitiveType, $result); }