SelectContract::getResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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