|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PostmanGeneratorBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Vincent Chalamon <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PostmanGeneratorBundle\Normalizer; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
15
|
|
|
use PostmanGeneratorBundle\Model\Request; |
|
16
|
|
|
use PostmanGeneratorBundle\Model\Test; |
|
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class RequestNormalizer implements NormalizerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
* |
|
24
|
|
|
* @param Request $object |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function normalize($object, $format = null, array $context = []) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$data = []; |
|
29
|
1 |
|
$reflectionClass = new \ReflectionClass($object); |
|
30
|
|
|
/** @var \ReflectionProperty[] $properties */ |
|
31
|
|
|
$properties = array_filter($reflectionClass->getProperties(), function (\ReflectionProperty $property) { |
|
32
|
1 |
|
return 'resource' !== $property->getName(); |
|
33
|
1 |
|
}); |
|
34
|
|
|
|
|
35
|
1 |
|
foreach ($properties as $property) { |
|
36
|
1 |
|
if ('fromCollection' !== $property->getName()) { |
|
37
|
1 |
|
$method = $reflectionClass->getMethod('get'.Inflector::classify($property->getName())); |
|
38
|
1 |
|
} else { |
|
39
|
1 |
|
$method = $reflectionClass->getMethod('is'.Inflector::classify($property->getName())); |
|
40
|
|
|
} |
|
41
|
1 |
|
if ('folder' === $property->getName()) { |
|
42
|
1 |
|
$data[$property->getName()] = $method->invoke($object)->getId(); |
|
43
|
1 |
|
} elseif ('collection' === $property->getName()) { |
|
44
|
1 |
|
$data[$property->getName().'Id'] = $method->invoke($object)->getId(); |
|
45
|
1 |
|
} elseif ('headers' === $property->getName()) { |
|
46
|
1 |
|
$data[$property->getName()] = ''; |
|
47
|
1 |
|
foreach ($method->invoke($object) as $key => $value) { |
|
48
|
1 |
|
$data[$property->getName()] .= "$key: $value\n"; |
|
49
|
1 |
|
} |
|
50
|
1 |
|
} elseif ('rawModeData' === $property->getName()) { |
|
51
|
1 |
|
$rawModeData = json_encode((object)$method->invoke($object)); |
|
52
|
1 |
|
if ('{}' !== $rawModeData) { |
|
53
|
1 |
|
$rawModeData = preg_replace('/([{,])/', "\$1\n ", $rawModeData); |
|
54
|
1 |
|
$rawModeData = preg_replace('/([}])/', "\n}", $rawModeData); |
|
55
|
1 |
|
} |
|
56
|
1 |
|
$data[$property->getName()] = $rawModeData; |
|
57
|
1 |
|
} elseif ('tests' === $property->getName()) { |
|
58
|
1 |
|
$tests = implode("\n\n", array_map(function (Test $test) { |
|
59
|
1 |
|
return sprintf('tests["%s"] = %s;', $test->getMessage(), $test->getExecutor()); |
|
60
|
1 |
|
}, $object->getTests())); |
|
61
|
1 |
|
$data[$property->getName()] = $tests; |
|
62
|
1 |
|
} else { |
|
63
|
1 |
|
$data[$property->getName()] = $method->invoke($object); |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function supportsNormalization($data, $format = null) |
|
74
|
|
|
{ |
|
75
|
2 |
|
return 'json' === $format && is_object($data) && $data instanceof Request; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|