[Serializer] updated SerializerInterface

This commit is contained in:
Johannes Schmitt 2011-05-06 13:01:19 +02:00
parent dfd5b653cb
commit ec1199eda7
2 changed files with 36 additions and 62 deletions

View File

@ -46,7 +46,14 @@ class Serializer implements SerializerInterface
*/
public function serialize($data, $format)
{
return $this->encode($data, $format);
return $this->encode($this->normalize($data, $format), $format);
}
/**
* {@inheritDoc}
*/
public function deserialize($data, $type, $format) {
return $this->denormalize($this->decode($data, $format), $type, $format);
}
/**
@ -95,8 +102,11 @@ class Serializer implements SerializerInterface
/**
* {@inheritdoc}
*/
public function normalize($data, $format)
public function normalize($data, $format = null)
{
if (!$this->isStructuredType($data)) {
return $data;
}
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
@ -109,6 +119,14 @@ class Serializer implements SerializerInterface
throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
}
/**
* {@inheritDoc}
*/
public function denormalize($data, $type, $format = null)
{
return $this->denormalizeObject($data, $type, $format);
}
/**
* {@inheritdoc}
*/

View File

@ -31,6 +31,15 @@ interface SerializerInterface
*/
function serialize($data, $format);
/**
* Deserializes data into the given type.
*
* @param mixed $data
* @param string $type
* @param string $format
*/
function deserialize($data, $type, $format);
/**
* Normalizes any data into a set of arrays/scalars
*
@ -39,27 +48,18 @@ interface SerializerInterface
* @return array|scalar
* @api
*/
function normalize($data, $format);
function normalize($data, $format = null);
/**
* Normalizes an object into a set of arrays/scalars
* Denormalizes data into the given type.
*
* @param object $object object to normalize
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @param array $properties a list of properties to extract, if null all properties are returned
* @return array|scalar
*/
function normalizeObject($object, $format, $properties = null);
/**
* Denormalizes data back into an object of the given class
* @param mixed $data
* @param string $type
* @param string $format
*
* @param mixed $data data to restore
* @param string $class the expected class to instantiate
* @param string $format format name, present to give the option to normalizers to act differently based on formats
* @return object
* @return mixed
*/
function denormalizeObject($data, $class, $format = null);
function denormalize($data, $type, $format = null);
/**
* Encodes data into the given format
@ -80,48 +80,4 @@ interface SerializerInterface
* @api
*/
function decode($data, $format);
/**
* @param NormalizerInterface $normalizer
*/
function addNormalizer(NormalizerInterface $normalizer);
/**
* @return array[]NormalizerInterface
*/
function getNormalizers();
/**
* @param NormalizerInterface $normalizer
*/
function removeNormalizer(NormalizerInterface $normalizer);
/**
* @param string $format format name
* @param EncoderInterface $encoder
*/
function setEncoder($format, EncoderInterface $encoder);
/**
* @return EncoderInterface
*/
function getEncoders();
/**
* @return array[]EncoderInterface
*/
function getEncoder($format);
/**
* Checks whether the serializer has an encoder registered for the given format
*
* @param string $format format name
* @return Boolean
*/
function hasEncoder($format);
/**
* @param string $format format name
*/
function removeEncoder($format);
}