TensideApiDocHandler::convertChildren()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
nop 2
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[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
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core-bundle
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle\Annotation;
22
23
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
24
use Nelmio\ApiDocBundle\DataTypes;
25
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
26
use Symfony\Component\Routing\Route;
27
28
/**
29
 * This class parses the API annotation.
30
 */
31
class TensideApiDocHandler implements HandlerInterface
32
{
33
    /**
34
     * The field names of parameters to convert by default.
35
     *
36
     * @var array
37
     */
38
    public static $convertFields = [
39
        'default',
40
        'description',
41
        'format',
42
        'actualType',
43
        'subType',
44
        'sinceVersion',
45
        'untilVersion',
46
        'requirement'
47
    ];
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
53
    {
54
        foreach ($annotations as $description) {
55
            if (!($description instanceof ApiDescription)) {
56
                continue;
57
            }
58
59
            $current = $annotation->toArray();
60
61
            $request = [];
62
            foreach ($description->getRequest() as $name => $field) {
63
                $request[$name] = $this->convertField($field);
64
            }
65
66
            $annotation->setParameters(array_merge($annotation->getParameters(), $request));
67
68
            if (!isset($current['response'])) {
69
                $response = [];
70
                foreach ($description->getResponse() as $name => $field) {
71
                    $response[$name] = $this->convertField($field);
72
                }
73
74
                $annotation->setResponse($response);
75
            }
76
        }
77
    }
78
79
    /**
80
     * Convert the annotation for a field.
81
     *
82
     * @param array $array The information for the field.
83
     *
84
     * @return array
85
     */
86
    private function convertField($array)
87
    {
88
        $result = [];
89
90
        // Copy over well known keys.
91
        foreach (static::$convertFields as $key) {
92
            if (isset($array[$key])) {
93
                $result[$key] = $array[$key];
94
            }
95
        }
96
97
        if (isset($array['dataType'])) {
98
            $result['dataType'] = $this->inferType($array['dataType']);
99
        } else {
100
            $result['dataType'] = isset($array['children']) ? 'object' : DataTypes::STRING;
101
        }
102
103
        $result['required'] = isset($array['required']) && (bool) $array['required'];
104
        $result['readonly'] = isset($array['readonly']) && (bool) $array['readonly'];
105
106
        $result = $this->convertChildren($array, $result);
107
108
        return $result;
109
    }
110
111
    /**
112
     * Convert the children key of an array.
113
     *
114
     * @param array $array  The source array.
115
     *
116
     * @param array $result The partly converted array.
117
     *
118
     * @return array
119
     */
120
    private function convertChildren($array, $result)
121
    {
122
        if (isset($array['children'])) {
123
            foreach ($array['children'] as $key => $value) {
124
                $result['children'][$key] = $this->convertField($value);
125
                if (isset($result['children'][$key]['required'])) {
126
                    $result['required'] = $result['required'] || (bool) $result['children'][$key]['required'];
127
                }
128
            }
129
130
            return $result;
131
        }
132
133
        return $result;
134
    }
135
136
    /**
137
     * Convert the type.
138
     *
139
     * @param string $type The type name.
140
     *
141
     * @return string
142
     */
143
    public function inferType($type)
144
    {
145
        if (DataTypes::isPrimitive($type)) {
146
            return $type;
147
        } elseif (DataTypes::COLLECTION === strtolower($type)) {
148
            return $type;
149
        }
150
        return DataTypes::STRING;
151
    }
152
}
153