Passed
Push — master ( 1c3995...861401 )
by Alex
48s
created

TDataServicesType::isOK()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
namespace AlgoWeb\ODataMetadata\MetadataV3\edmx;
3
4
use AlgoWeb\ODataMetadata\IsOK;
5
use AlgoWeb\ODataMetadata\IsOKTraits\IsOKToolboxTrait;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\Schema;
7
8
class TDataServicesType extends IsOK
9
{
10
    use IsOKToolboxTrait;
11
    /**
12
     * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $schema
13
     */
14
    private $schema = [];
15
16
    /**
17
     * @property string $maxDataServiceVersion
18
     */
19
    private $maxDataServiceVersion;
20
21
    /**
22
     * @property string $dataServiceVersion
23
     */
24
    private $dataServiceVersion;
25
26
    public function __construct($maxDataServiceVersion = '3.0', $dataServiceVersion = '3.0')
27
    {
28
        if (!is_numeric($maxDataServiceVersion)) {
29
            $msg = "Maximum service version must be numeric";
30
            throw new \InvalidArgumentException($msg);
31
        }
32
        if (!is_numeric($dataServiceVersion)) {
33
            $msg = "Data service version must be numeric";
34
            throw new \InvalidArgumentException($msg);
35
        }
36
37
        if (floatval($maxDataServiceVersion) < floatval($dataServiceVersion)) {
38
            $msg = "Data service version cannot be greater than maximum service version";
39
            throw new \InvalidArgumentException($msg);
40
        }
41
        $this->setDataServiceVersion($dataServiceVersion);
42
        $this->setMaxDataServiceVersion($maxDataServiceVersion);
43
    }
44
45
    /**
46
     * Gets as MaxDataServiceVersion
47
     *
48
     * @return string
49
     */
50
    public function getMaxDataServiceVersion()
51
    {
52
        return $this->maxDataServiceVersion;
53
    }
54
55
    /**
56
     * Sets a new DataServiceVersion
57
     *
58
     * @param  string $maxDataServiceVersion
59
     * @return self
60
     */
61
    public function setMaxDataServiceVersion($maxDataServiceVersion)
62
    {
63
        $maxDataValid = ['3.0', '4.0'];
64
        if (!in_array($maxDataServiceVersion, $maxDataValid)) {
65
            $msg = "Maximum data service version must be 3.0 or 4.0";
66
            throw new \InvalidArgumentException($msg);
67
        }
68
        $this->maxDataServiceVersion = $maxDataServiceVersion;
69
        return $this;
70
    }
71
72
    /**
73
     * Gets as DataServiceVersion
74
     *
75
     * @return string
76
     */
77
    public function getDataServiceVersion()
78
    {
79
        return $this->dataServiceVersion;
80
    }
81
82
    /**
83
     * Sets a new DataServiceVersion
84
     *
85
     * @param  string $dataServiceVersion
86
     * @return self
87
     */
88
    public function setDataServiceVersion($dataServiceVersion)
89
    {
90
        $dataValid = ['1.0', '2.0', '3.0', '4.0'];
91
        if (!in_array($dataServiceVersion, $dataValid)) {
92
            $msg = "Data service version must be 1.0, 2.0, 3.0 or 4.0";
93
            throw new \InvalidArgumentException($msg);
94
        }
95
        $this->dataServiceVersion = $dataServiceVersion;
96
        return $this;
97
    }
98
    /**
99
     * Adds as schema
100
     *
101
     * @return self
102
     * @param  \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema $schema
103
     */
104
    public function addToSchema(Schema $schema)
105
    {
106
        $msg = null;
107
        if (!$schema->isOK($msg)) {
108
            throw new \InvalidArgumentException($msg);
109
        }
110
        $this->schema[] = $schema;
111
        return $this;
112
    }
113
114
    /**
115
     * isset schema
116
     *
117
     * @param  scalar $index
118
     * @return boolean
119
     */
120
    public function issetSchema($index)
121
    {
122
        return isset($this->schema[$index]);
123
    }
124
125
    /**
126
     * unset schema
127
     *
128
     * @param  scalar $index
129
     * @return void
130
     */
131
    public function unsetSchema($index)
132
    {
133
        unset($this->schema[$index]);
134
    }
135
136
    /**
137
     * Gets as schema
138
     *
139
     * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[]
140
     */
141
    public function getSchema()
142
    {
143
        return $this->schema;
144
    }
145
146
    /**
147
     * Sets a new schema
148
     *
149
     * @param  \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $dataServices
150
     * @return self
151
     */
152 View Code Duplication
    public function setSchema(array $dataServices)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (!$this->isValidArrayOK(
155
            $dataServices,
156
            '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema',
157
            $msg,
158
            1
159
        )
160
        ) {
161
            $msg = "Data services array not a valid array";
162
            throw new \InvalidArgumentException($msg);
163
        }
164
        $this->schema = $dataServices;
165
        return $this;
166
    }
167
168
    public function isOK(&$msg = null)
169
    {
170
        if ('3.0' == $this->maxDataServiceVersion && '4.0' == $this->dataServiceVersion) {
171
            $msg = "Data service version cannot be greater than maximum service version";
172
            return false;
173
        }
174
175
        if (!$this->isValidArrayOK($this->schema, '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema', $msg, 1)) {
176
            return false;
177
        }
178
179
        return true;
180
    }
181
}
182