Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created June 15, 2019 20:58
Show Gist options
  • Save luisenriquecorona/b39ad357c23a1cd93e10895b0410acc4 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/b39ad357c23a1cd93e10895b0410acc4 to your computer and use it in GitHub Desktop.
We have put some effort into implementing a nice abstract factory for encoders, but the GenericEncoder class still has this ugly switch statement for preparing the data before it is encoded.
class GenericEncoder
{
private function prepareData($data, string $format)
{
switch ($format) {
case 'json':
$data = $this->forceArray($data);
$data = $this->fixKeys($data);
// fall through
case 'xml':
$data = $this->fixAttributes($data);
break;
default:
throw new InvalidArgumentException(
'Format not supported'
);
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment