Test Failed
Push — master ( 475bfc...ca0e1d )
by Oss
05:46
created

SelectContract::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @category    Brownie/BpmOnline
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\BpmOnline\DataService\Contract;
9
10
use Brownie\BpmOnline\DataService\Column\ColumnExpression;
11
use Brownie\BpmOnline\DataService\Contract;
12
use Brownie\BpmOnline\DataService\Column\ColumnFilter;
13
use Brownie\BpmOnline\Exception\ValidateException;
14
15
/**
16
 * SelectContract Data Contract.
17
 */
18
class SelectContract extends Contract
19
{
20
21
    /**
22
     * A collection of columns for readable records.
23
     *
24
     * @var array
25
     */
26
    private $columns = [];
27
28
    /**
29
     * Column filter.
30
     *
31
     * @var ColumnFilter
32
     */
33
    private $filter = null;
34
35
    /**
36
     * Flag of selection of all columns.
37
     *
38
     * @var bool
39
     */
40
    private $isAllColumns = false;
41
42
    /**
43
     * Flag of pagination.
44
     *
45
     * @var bool
46
     */
47
    private $isPageable = false;
48
49
    /**
50
     * Flag of uniqueness.
51
     *
52
     * @var bool
53
     */
54
    private $isDistinct = false;
55
56
    /**
57
     * Number of records selected.
58
     *
59
     * @var int
60
     */
61
    private $rowCount = -1;
62
63
    /**
64
     * Sets the input values.
65
     *
66
     * @param string $rootSchemaName The name of the root schema object.
67
     */
68 12
    public function __construct($rootSchemaName)
69
    {
70 12
        parent::__construct([
71 12
            'rootSchemaName' => $rootSchemaName,
72 12
            'operationType' => Contract::SELECT,
73 12
            'contractType' => Contract::SELECT_QUERY,
74
        ]);
75 12
    }
76
77
    /**
78
     * Sets the flag for selecting all columns.
79
     * Returns the current object.
80
     *
81
     * @param bool $isAllColumns Flag of selection of all columns.
82
     *
83
     * @return self
84
     */
85 1
    public function setIsAllColumns($isAllColumns)
86
    {
87 1
        $this->isAllColumns = (bool)$isAllColumns;
88 1
        return $this;
89
    }
90
91
    /**
92
     * Returns the flag for selecting all columns.
93
     *
94
     * @return bool
95
     */
96 7
    private function isAllColumns()
97
    {
98 7
        return $this->isAllColumns;
99
    }
100
101
    /**
102
     * Sets the pagination flag.
103
     * Returns the current object.
104
     *
105
     * @param bool $isPageable Flag of pagination.
106
     *
107
     * @return self
108
     */
109 1
    public function setIsPageable($isPageable)
110
    {
111 1
        $this->isPageable = $isPageable;
112 1
        return $this;
113
    }
114
115
    /**
116
     * Returns the pagination attribute.
117
     *
118
     * @return bool
119
     */
120 7
    private function isPageable()
121
    {
122 7
        return $this->isPageable;
123
    }
124
125
    /**
126
     * Sets flag of uniqueness.
127
     * Returns the current object.
128
     *
129
     * @param bool $isDistinct Flag of uniqueness.
130
     *
131
     * @return self
132
     */
133 1
    public function setIsDistinct($isDistinct)
134
    {
135 1
        $this->isDistinct = $isDistinct;
136 1
        return $this;
137
    }
138
139
    /**
140
     * Returns flag of uniqueness.
141
     *
142
     * @return bool
143
     */
144 7
    private function isDistinct()
145
    {
146 7
        return $this->isDistinct;
147
    }
148
149
    /**
150
     * Sets number of records selected.
151
     * Returns the current object.
152
     *
153
     * @param int $rowCount Number of records selected.
154
     *
155
     * @return self
156
     */
157 1
    public function setRowCount($rowCount)
158
    {
159 1
        $this->rowCount = $rowCount;
160 1
        return $this;
161
    }
162
163
    /**
164
     * Return number of records selected.
165
     *
166
     * @return int
167
     */
168 7
    private function getRowCount()
169
    {
170 7
        return $this->rowCount;
171
    }
172
173
    /**
174
     * Adding a query expression to a contract.
175
     * Returns the current object.
176
     *
177
     * @param string            $name               Column name.
178
     * @param string            $orderDirection     The sort order.
179
     * @param int               $orderPosition      Column position.
180
     * @param string            $caption            Headline.
181
     * @param ColumnExpression  $columnExpression   Query expression to the schema object.
182
     *
183
     * @return self
184
     */
185 1
    public function addColumn($name, $orderDirection, $orderPosition, $caption, ColumnExpression $columnExpression)
186
    {
187 1
        $this->columns[$name] = [
188 1
            'OrderDirection' => $orderDirection,
189 1
            'OrderPosition' => $orderPosition,
190 1
            'Caption' => $caption,
191 1
            'Expression' => $columnExpression,
192
        ];
193 1
        return $this;
194
    }
195
196
    /**
197
     * Adding query filter to contract.
198
     *
199
     * @param ColumnFilter $columnFilter Column filter.
200
     *
201
     * @return self
202
     */
203 1
    public function addFilter(ColumnFilter $columnFilter)
204
    {
205 1
        $this->filter = $columnFilter;
206 1
        return $this;
207
    }
208
209
    /**
210
     * Returns data as an associative array.
211
     *
212
     * @return array
213
     */
214 7
    public function toArray()
215
    {
216 7
        $columns = [];
217 7
        foreach ($this->columns as $name => $columnData) {
218 1
            $columns[$name] = [
219 1
                'OrderDirection' => $columnData['OrderDirection'],
220 1
                'OrderPosition' => $columnData['OrderPosition'],
221 1
                'Caption' => $columnData['Caption'],
222 1
                'Expression' => $columnData['Expression']->toArray(),
223
            ];
224
        }
225
        $data = [
226 7
            'RootSchemaName' => $this->getRootSchemaName(),
227 7
            'OperationType' => $this->getOperationType(),
228
            'Columns' => [
229 7
                'Items' => $columns
230
            ],
231 7
            'AllColumns' => $this->isAllColumns(),
232
            /*
233
            'ServerESQCacheParameters' => [
234
                'CacheLevel' => 0,
235
                'CacheGroup' => '',
236
                'CacheItemName' => '',
237
            ],
238
            */
239 7
            'IsPageable' => $this->isPageable(),
240 7
            'IsDistinct' => $this->isDistinct(),
241 7
            'RowCount' => $this->getRowCount(),
242
            //'ConditionalValues' => null,
243
            //'IsHierarchical' => false,
244
            //'HierarchicalMaxDepth' => 0,
245
            //'HierarchicalColumnName' => '',
246
            //'HierarchicalColumnValue' => '',
247
248
        ];
249 7
        if (!empty($this->filter)) {
250 1
            $data['Filters'] = $this->filter->toArray();
251
        }
252 7
        return $data;
253
    }
254
255
    /**
256
     * Validates contract data, throws an exception in case of an error.
257
     */
258 2
    public function validate()
259
    {
260 2
        if (0 != $this->getOperationType()) {
261 1
            throw new ValidateException('Invalid contract arguments.');
262
        }
263 1
    }
264
}
265