class Serializer {
public function serialize($data) {
$encoder = new JsonEncoder();
$nameConverter = new CamelCaseToSnakeCaseConverter();
$normalizer = new ObjectNormalizer(nameConverter);
return $encoder->encode($normalizer->normalize($data));
}
}
Ce n'est pas ça...
class Serializer {
public function serialize($data) {
$encoder = new XmlEncoder();
$nameConverter = new CamelCaseToSnakeCaseConverter();
$normalizer = new ArrayNormalizer(nameConverter);
return $encoder->encode($normalizer->normalize($data));
}
}
Pas très pratique, mon serializer a besoin de connaitre les implémentations des classes qu'il utilise...
Idéalement mon serializer n'a pas à connaitre les implémentations et se contente de demander des interfaces
class Serializer {
private $normalizer;
private $encoder;
public function setEncoder($encoder) {
$this->encoder = $encoder;
}
public function setEncoder($normalizer) {
$this->normalizer = $normalizer;
}
public function serialize($data) {
return $this->encoder->encode($this->normalizer->normalize($data));
}
}
class Serializer {
private $normalizer;
private $encoder;
public function __construct($encoder, $normalizer) {
$this->encoder = $encoder;
$this->normalizer = $normalizer;
}
public function serialize($data) {
return $this->encoder->encode($this->normalizer->normalize($data));
}
}