Issues (3641)

Component/SchemaSpecificationComponent.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\DocumentationGeneratorRestApi\Business\Renderer\Component;
9
10
use Generated\Shared\Transfer\SchemaComponentTransfer;
11
12
/**
13
 * Specification:
14
 *  - This component describes a single Schema Object.
15
 *  - This component partly covers Schema Object in OpenAPI specification format (see https://swagger.io/specification/#schemaObject).
16
 */
17
class SchemaSpecificationComponent implements SchemaSpecificationComponentInterface
18
{
19
    /**
20
     * @var \Generated\Shared\Transfer\SchemaComponentTransfer|null
21
     */
22
    protected $schemaComponentTransfer;
23
24
    /**
25
     * @param \Generated\Shared\Transfer\SchemaComponentTransfer $schemaComponentTransfer
26
     *
27
     * @return void
28
     */
29
    public function setSchemaComponentTransfer(SchemaComponentTransfer $schemaComponentTransfer): void
30
    {
31
        $this->schemaComponentTransfer = $schemaComponentTransfer;
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getSpecificationComponentData(): array
38
    {
39
        if (!$this->validateSchemaComponentTransfer()) {
40
            return [];
41
        }
42
43
        if (count($this->schemaComponentTransfer->getProperties()) === 0 && !$this->schemaComponentTransfer->getItems()) {
44
            //empty object is needed for generation of valid OpenAPI scheme
45
            return [
46
                $this->schemaComponentTransfer->getName() => (object)[],
47
            ];
48
        }
49
50
        $schemaData = [];
51
        $schemaData = $this->addProperties($schemaData);
52
        $schemaData = $this->addItems($schemaData);
53
        $schemaData = $this->addRequired($schemaData);
54
        $schemaData = $this->addType($schemaData);
55
56
        return $schemaData;
57
    }
58
59
    /**
60
     * @param array $schemaData
61
     *
62
     * @return array
63
     */
64
    protected function addProperties(array $schemaData): array
65
    {
66
        if (count($this->schemaComponentTransfer->getProperties())) {
0 ignored issues
show
The method getProperties() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if (count($this->schemaComponentTransfer->/** @scrutinizer ignore-call */ getProperties())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            $schemaData[$this->schemaComponentTransfer->getName()][SchemaComponentTransfer::PROPERTIES] = array_merge(...$this->schemaComponentTransfer->getProperties());
68
        }
69
70
        return $schemaData;
71
    }
72
73
    /**
74
     * @param array $schemaData
75
     *
76
     * @return array
77
     */
78
    protected function addItems(array $schemaData): array
79
    {
80
        if ($this->schemaComponentTransfer->getItems()) {
81
            $schemaData[$this->schemaComponentTransfer->getName()][SchemaComponentTransfer::ITEMS] = $this->schemaComponentTransfer->getItems();
82
        }
83
84
        return $schemaData;
85
    }
86
87
    /**
88
     * @param array $schemaData
89
     *
90
     * @return array
91
     */
92
    protected function addRequired(array $schemaData): array
93
    {
94
        if ($this->schemaComponentTransfer->getRequired()) {
95
            $schemaData[$this->schemaComponentTransfer->getName()][SchemaComponentTransfer::REQUIRED] = $this->schemaComponentTransfer->getRequired();
96
        }
97
98
        return $schemaData;
99
    }
100
101
    /**
102
     * @param array $schemaData
103
     *
104
     * @return array
105
     */
106
    protected function addType(array $schemaData): array
107
    {
108
        if ($this->schemaComponentTransfer->getType()) {
109
            $schemaData[$this->schemaComponentTransfer->getName()][SchemaComponentTransfer::TYPE] = $this->schemaComponentTransfer->getType();
110
        }
111
112
        return $schemaData;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    protected function validateSchemaComponentTransfer(): bool
119
    {
120
        if (!$this->schemaComponentTransfer) {
121
            return false;
122
        }
123
124
        $this->schemaComponentTransfer->requireName();
125
126
        return true;
127
    }
128
}
129