Completed
Push — master ( 47e8e3...f34728 )
by Vitaly
02:52
created

Generic::convertToDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 08.12.15
6
 * Time: 23:11
7
 */
8
namespace samsoncms\api\query;
9
10
use samsoncms\api\Material;
11
use samsonframework\orm\ArgumentInterface;
12
use samsonframework\orm\Condition;
13
use samsonframework\orm\QueryInterface;
14
15
/**
16
 * Material with additional fields query.
17
 * @package samsoncms\api
18
 */
19
class Generic
20
{
21
    /** @var array Collection of all supported entity fields */
22
    protected static $parentFields = array(
23
        Material::F_PRIORITY => Material::F_PRIORITY,
24
        Material::F_IDENTIFIER => Material::F_IDENTIFIER,
25
        Material::F_DELETION => Material::F_DELETION,
26
        Material::F_PUBLISHED => Material::F_PUBLISHED,
27
        Material::F_PARENT => Material::F_PARENT,
28
        Material::F_CREATED => Material::F_CREATED,
29
    );
30
31
    /** @var string Entity identifier */
32
    protected static $identifier;
33
34
    /** @var string Entity navigation identifiers */
35
    protected static $navigationIDs = array();
36
37
    /**
38
     * @var string Collection of entity field names
39
     * @deprecated Created for old application who need real additional field names
40
     */
41
    public static $fieldRealNames = array();
42
43
    /** @var string Collection of entity field names */
44
    public static $fieldNames = array();
45
46
47
    /** @var QueryInterface Database query instance */
48
    protected $query;
49
50
    /** @var array Collection of entity fields to retrieved from database */
51
    protected $selectedFields;
52
53
    /** @var Condition Query conditions */
54
    protected $conditions;
55
56
    /** @var array Collection of ordering parameters */
57
    protected $orderBy = array();
58
59
    /** @var array Collection of limit parameters */
60
    protected $limit = array();
61
62
    /**
63
     * Convert date value to database format.
64
     * TODO: Must implement at database layer
65
     *
66
     * @param string $date Date value for conversion
67
     * @return string Converted date to correct format
68
     */
69
    protected function convertToDateTime($date)
70
    {
71
        return date("Y-m-d H:i:s", strtotime($date));
72
    }
73
74
    /**
75
     * Add condition to current query.
76
     *
77
     * @param string $fieldName Entity field name
78
     * @param string $fieldValue Value
79
     * @return $this Chaining
80
     */
81
    public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)
82
    {
83
        $this->conditions->add($fieldName, $fieldValue, $fieldRelation);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Set field for sorting.
90
     *
91
     * @param string $fieldName Additional field name
92
     * @param string $order Sorting order
93
     * @return $this Chaining
94
     */
95
    public function orderBy($fieldName, $order = 'ASC')
96
    {
97
        if (array_key_exists($fieldName, static::$parentFields)) {
98
            $this->orderBy = array($fieldName, $order);
99
        }
100
101
        return $this;
102
    }
103
104
    /**
105
     * Add primary field query condition.
106
     *
107
     * @param string $value Field value
108
     * @return $this Chaining
109
     * @see Material::where()
110
     */
111
    public function primary($value)
112
    {
113
        return $this->where(Material::F_PRIMARY, $value);
114
    }
115
116
    /**
117
     * Add identifier field query condition.
118
     *
119
     * @param string $value Field value
120
     * @return $this Chaining
121
     * @see Material::where()
122
     */
123
    public function identifier($value)
124
    {
125
        return $this->where(Material::F_IDENTIFIER, $value);
126
    }
127
128
    /**
129
     * Add active flag condition.
130
     *
131
     * @param bool $value Field value
132
     * @return $this Chaining
133
     * @see Material::where()
134
     */
135
    public function active($value)
136
    {
137
        return $this->where(Material::F_DELETION, $value);
0 ignored issues
show
Documentation introduced by
$value is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
    }
139
140
    /**
141
     * Add entity published field query condition.
142
     *
143
     * @param string $value Field value
144
     * @return $this Chaining
145
     * @see Material::where()
146
     */
147
    public function published($value)
148
    {
149
        return $this->where(Material::F_PUBLISHED, $value);
150
    }
151
152
    /**
153
     * Add entity creation field query condition.
154
     *
155
     * @param string $value Field value
156
     * @param string $relation @see ArgumentInterface types
157
     * @return $this Chaining
158
     * @see Material::where()
159
     */
160
    public function created($value, $relation = ArgumentInterface::EQUAL)
161
    {
162
        return $this->where(Material::F_CREATED, $this->convertToDateTime($value), $relation);
163
    }
164
165
    /**
166
     * Add entity modification field query condition.
167
     *
168
     * @param string $value Field value
169
     * @param string $relation @see ArgumentInterface types
170
     * @return $this Chaining
171
     * @see Material::where()
172
     */
173
    public function modified($value, $relation = ArgumentInterface::EQUAL)
174
    {
175
        return $this->where(Material::F_MODIFIED, $this->convertToDateTime($value), $relation);
176
    }
177
178
    /**
179
     * Perform SamsonCMS query and get entities collection.
180
     *
181
     * @return \samsoncms\api\Entity[] Collection of found entities
182
     */
183
    public function find()
184
    {
185
        $this->query->entity(static::$identifier);
186
187
        // Add query sorter for showed page
188
        if (count($this->orderBy) === 2) {
189
            $this->query->orderBy($this->orderBy[0], $this->orderBy[1]);
190
        }
191
192
        // Proxy to regular database query
193
        return $this->query
194
            ->whereCondition($this->conditions)
195
            ->exec();
196
    }
197
198
    /**
199
     * Perform SamsonCMS query and get collection of entities fields.
200
     *
201
     * @param string $fieldName Entity field name
202
     * @return array Collection of entity fields
203
     */
204
    public function fields($fieldName)
205
    {
206
        // Proxy to regular database query
207
        return $this->query
208
            ->entity(static::$identifier)
209
            ->whereCondition($this->conditions)
210
            ->fields($fieldName);
211
    }
212
213
    /**
214
     * Perform SamsonCMS query and get first matching entity.
215
     *
216
     * @return \samsoncms\api\Entity First matching entity
217
     */
218
    public function first()
219
    {
220
        // Proxy to regular database query
221
        $return = $this->query
222
            ->entity(static::$identifier)
223
            ->limit(1)
224
            ->whereCondition($this->conditions)
225
            ->exec();
226
227
        return array_shift($return);
228
    }
229
230
    /**
231
     * Perform SamsonCMS query and get amount resulting entities.
232
     *
233
     * @return int Amount of resulting entities
234
     */
235
    public function count()
236
    {
237
        // Proxy to regular database query
238
        return $this->query
239
            ->entity(static::$identifier)
240
            ->whereCondition($this->conditions)
241
            ->count();
242
    }
243
244
    /**
245
     * Generic constructor.
246
     *
247
     * @param QueryInterface $query Database query instance
248
     */
249
    public function __construct(QueryInterface $query)
250
    {
251
        $this->query = $query;
252
        $this->conditions = new Condition();
253
    }
254
}
255