added Constructor

This commit is contained in:
Lukas Kahwe Smith 2011-05-29 10:55:54 +02:00
parent 46da5ff069
commit 52b8cb622d

View File

@ -27,9 +27,11 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException;
* *
* $serializer->serialize($obj, 'xml') * $serializer->serialize($obj, 'xml')
* $serializer->decode($data, 'xml') * $serializer->decode($data, 'xml')
* $serializer->denormalizeObject($data, 'Class', 'xml') * $serializer->denormalize($data, 'Class', 'xml')
* *
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/ */
class Serializer implements SerializerInterface class Serializer implements SerializerInterface
{ {
@ -38,10 +40,27 @@ class Serializer implements SerializerInterface
protected $normalizerCache = array(); protected $normalizerCache = array();
protected $denormalizerCache = array(); protected $denormalizerCache = array();
public function __construct(array $normalizers = array(), array $encoders = array())
{
foreach ($normalizers as $normalizer) {
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($this);
}
}
$this->normalizers = $normalizers;
foreach ($encoders as $encoder) {
if ($encoder instanceof SerializerAwareInterface) {
$encoder->setSerializer($this);
}
}
$this->encoders = $encoders;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function serialize($data, $format) public final function serialize($data, $format)
{ {
if (!isset($this->encoders[$format])) { if (!isset($this->encoders[$format])) {
throw new UnexpectedValueException('No encoder registered for the '.$format.' format'); throw new UnexpectedValueException('No encoder registered for the '.$format.' format');