-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serializer.propertyNamingStrategy service
Becasue SerializerVistors and DeserializerVisitors typically take the naming strategy as an argument, it is helpful to have access to the naming strategy that the provider uses.
- Loading branch information
1 parent
8ee6a51
commit a7d2835
Showing
7 changed files
with
148 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/vendor/ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace JDesrosiers\Silex\Provider; | ||
|
||
use JMS\Serializer\SerializerBuilder; | ||
use Pimple\Container; | ||
|
||
class BuilderService | ||
{ | ||
public function __invoke(Container $app) { | ||
$serializerBuilder = SerializerBuilder::create(); | ||
|
||
if ($app->offsetExists("debug")) { | ||
$serializerBuilder->setDebug($app["debug"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.annotationReader")) { | ||
$serializerBuilder->setAnnotationReader($app["serializer.annotationReader"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.cacheDir")) { | ||
$serializerBuilder->setCacheDir($app["serializer.cacheDir"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.configureHandlers")) { | ||
$serializerBuilder->configureHandlers($app["serializer.configureHandlers"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.configureListeners")) { | ||
$serializerBuilder->configureListeners($app["serializer.configureListeners"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.objectConstructor")) { | ||
$serializerBuilder->setObjectConstructor($app["serializer.objectConstructor"]); | ||
} | ||
|
||
$serializerBuilder->setPropertyNamingStrategy($app["serializer.propertyNamingStrategy"]); | ||
|
||
if ($app->offsetExists("serializer.serializationVisitors")) { | ||
$this->setSerializationVisitors($app, $serializerBuilder); | ||
} | ||
|
||
if ($app->offsetExists("serializer.deserializationVisitors")) { | ||
$this->setDeserializationVisitors($app, $serializerBuilder); | ||
} | ||
|
||
if ($app->offsetExists("serializer.includeInterfaceMetadata")) { | ||
$serializerBuilder->includeInterfaceMetadata($app["serializer.includeInterfaceMetadata"]); | ||
} | ||
|
||
if ($app->offsetExists("serializer.metadataDirs")) { | ||
$serializerBuilder->setMetadataDirs($app["serializer.metadataDirs"]); | ||
} | ||
|
||
return $serializerBuilder; | ||
} | ||
|
||
/** | ||
* Override default serialization vistors | ||
* | ||
* @param Container $app | ||
* @param SerializerBuilder $serializerBuilder | ||
*/ | ||
protected function setSerializationVisitors(Container $app, SerializerBuilder $serializerBuilder) | ||
{ | ||
$serializerBuilder->addDefaultSerializationVisitors(); | ||
|
||
foreach ($app["serializer.serializationVisitors"] as $format => $visitor) { | ||
$serializerBuilder->setSerializationVisitor($format, $visitor); | ||
} | ||
} | ||
|
||
/** | ||
* Override default deserialization visitors | ||
* | ||
* @param Container $app | ||
* @param SerializerBuilder $serializerBuilder | ||
*/ | ||
protected function setDeserializationVisitors(Container $app, SerializerBuilder $serializerBuilder) | ||
{ | ||
$serializerBuilder->addDefaultDeserializationVisitors(); | ||
|
||
foreach ($app["serializer.deserializationVisitors"] as $format => $visitor) { | ||
$serializerBuilder->setDeserializationVisitor($format, $visitor); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace JDesrosiers\Silex\Provider; | ||
|
||
use JMS\Serializer\Naming\CamelCaseNamingStrategy; | ||
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
use JMS\Serializer\Naming\PropertyNamingStrategyInterface; | ||
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; | ||
use Pimple\Container; | ||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; | ||
|
||
class PropertyNamingStrategyService | ||
{ | ||
/** | ||
* Set the serialization naming strategy | ||
* | ||
* @param Container $app | ||
* @return PropertyNamingStrategyInterface | ||
* | ||
* @throws ServiceUnavailableHttpException | ||
*/ | ||
public function __invoke(Container $app) | ||
{ | ||
if ($app["serializer.namingStrategy"] instanceof PropertyNamingStrategyInterface) { | ||
$namingStrategy = $app["serializer.namingStrategy"]; | ||
} else { | ||
switch ($app["serializer.namingStrategy"]) { | ||
case "IdenticalProperty": | ||
$namingStrategy = new IdenticalPropertyNamingStrategy(); | ||
break; | ||
case "CamelCase": | ||
$separator = $app["serializer.namingStrategy.separator"]; | ||
$lowerCase = $app["serializer.namingStrategy.lowerCase"]; | ||
$namingStrategy = new CamelCaseNamingStrategy($separator, $lowerCase); | ||
break; | ||
default: | ||
throw new ServiceUnavailableHttpException( | ||
null, | ||
"Unknown property naming strategy '{$app["serializer.namingStrategy"]}'. " . | ||
"Allowed values are 'IdenticalProperty' or 'CamelCase'" | ||
); | ||
} | ||
|
||
$namingStrategy = new SerializedNameAnnotationStrategy($namingStrategy); | ||
} | ||
|
||
return $namingStrategy; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/JmsSerializerServiceProviderTest.php → ...Test/JmsSerializerServiceProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters