forked from loafle/openapi-generator-original
commit c5a0d0f7394aa742fa336fff7e7c1d3049761868 Merge: 8c4991ba3ed f8ff8c87609 Author: William Cheng <wing328hk@gmail.com> Date: Tue Aug 17 18:28:12 2021 +0800 Merge branch 'mustache-linting' of https://github.com/NathanBaulch/openapi-generator into NathanBaulch-mustache-linting commit f8ff8c87609b1ca36fa26fb8474806999638195e Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Thu Aug 5 14:12:47 2021 +1000 Reorder tags that handle missing values commit f5d8a33709d6a3f846a9fe4520b78c3d637051d9 Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Thu Aug 5 14:08:59 2021 +1000 Use dot notation where possible commit 493d14921e2333f3ae19ef6fc89318b7e263a80c Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Thu Aug 5 14:10:49 2021 +1000 Remove empty tags commit 32480dc53f48227d55531b94e307d72671373737 Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Thu Aug 5 10:41:58 2021 +1000 Remove redundant sections commit a8edabd722c34aa094b4aeb11c22664529c3a219 Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Wed Aug 4 22:02:22 2021 +1000 Trim extra EOF new lines commit e89bd7458e3594bf0d30e580bc9408e45b018a57 Author: Nathan Baulch <nathan.baulch@gmail.com> Date: Wed Aug 4 21:59:26 2021 +1000 Trim trailing whitespace
116 lines
3.2 KiB
PHP
116 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace OpenAPI\Server\Service;
|
|
|
|
use JMS\Serializer\SerializerBuilder;
|
|
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
|
|
use JMS\Serializer\XmlDeserializationVisitor;
|
|
|
|
class JmsSerializer implements SerializerInterface
|
|
{
|
|
protected $serializer;
|
|
|
|
public function __construct()
|
|
{
|
|
$naming_strategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
|
|
$this->serializer = SerializerBuilder::create()
|
|
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitor($naming_strategy))
|
|
->setDeserializationVisitor('xml', new XmlDeserializationVisitor($naming_strategy))
|
|
->build();
|
|
}
|
|
|
|
public function serialize($data, $format)
|
|
{
|
|
return SerializerBuilder::create()->build()->serialize($data, $this->convertFormat($format));
|
|
}
|
|
|
|
public function deserialize($data, $type, $format)
|
|
{
|
|
if ($format == 'string') {
|
|
return $this->deserializeString($data, $type);
|
|
}
|
|
|
|
// If we end up here, let JMS serializer handle the deserialization
|
|
return $this->serializer->deserialize($data, $type, $this->convertFormat($format));
|
|
}
|
|
|
|
private function convertFormat($format)
|
|
{
|
|
switch ($format) {
|
|
case 'application/json':
|
|
return 'json';
|
|
case 'application/xml':
|
|
return 'xml';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function deserializeString($data, $type)
|
|
{
|
|
// Figure out if we have an array format
|
|
if (1 === preg_match('/array<(csv|ssv|tsv|pipes),(int|string)>/i', $type, $matches)) {
|
|
return $this->deserializeArrayString($matches[1], $matches[2], $data);
|
|
}
|
|
|
|
switch ($type) {
|
|
case 'int':
|
|
case 'integer':
|
|
if (is_int($data)) {
|
|
return $data;
|
|
}
|
|
|
|
if (is_numeric($data)) {
|
|
return $data + 0;
|
|
}
|
|
|
|
break;
|
|
case 'string':
|
|
break;
|
|
case 'boolean':
|
|
case 'bool':
|
|
if (strtolower($data) === 'true') {
|
|
return true;
|
|
}
|
|
|
|
if (strtolower($data) === 'false') {
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
// If we end up here, just return data
|
|
return $data;
|
|
}
|
|
|
|
private function deserializeArrayString($format, $type, $data)
|
|
{
|
|
// Parse the string using the correct separator
|
|
switch ($format) {
|
|
case 'csv':
|
|
$data = explode(',', $data);
|
|
break;
|
|
case 'ssv':
|
|
$data = explode(' ', $data);
|
|
break;
|
|
case 'tsv':
|
|
$data = explode("\t", $data);
|
|
break;
|
|
case 'pipes':
|
|
$data = explode('|', $data);
|
|
break;
|
|
default;
|
|
$data = [];
|
|
}
|
|
|
|
// Deserialize each of the array elements
|
|
foreach ($data as $key => $item) {
|
|
$data[$key] = $this->deserializeString($item, $type);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|