Completed
Push — master ( 6a6774...9df172 )
by Vitaly
04:29
created

Generic   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 17
Bugs 1 Features 8
Metric Value
wmc 11
c 17
b 1
f 8
lcom 1
cbo 2
dl 0
loc 172
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDateTime() 0 4 1
A where() 0 6 1
A primary() 0 4 1
A identifier() 0 4 1
A published() 0 4 1
A created() 0 4 1
A modified() 0 4 1
A find() 0 8 1
A fields() 0 8 1
A first() 0 11 1
A __construct() 0 5 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
    /** @var QueryInterface Database query instance */
38
    protected $query;
39
40
    /** @var array Collection of entity fields to retrieved from database */
41
    protected $selectedFields;
42
43
    /** @var Condition Query conditions */
44
    protected $conditions;
45
46
    /**
47
     * Convert date value to database format.
48
     * TODO: Must implement at database layer
49
     *
50
     * @param string $date Date value for conversion
51
     * @return string Converted date to correct format
52
     */
53
    protected function convertToDateTime($date)
54
    {
55
        return date("Y-m-d H:i:s", strtotime($date));
56
    }
57
58
    /**
59
     * Add condition to current query.
60
     *
61
     * @param string $fieldName Entity field name
62
     * @param string $fieldValue Value
63
     * @return self Chaining
64
     */
65
    public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)
66
    {
67
        $this->conditions->add($fieldName, $fieldValue, $fieldRelation);
68
69
        return $this;
70
    }
71
72
    /**
73
     * Add primary field query condition.
74
     *
75
     * @param string $value Field value
76
     * @return self Chaining
77
     * @see Material::where()
78
     */
79
    public function primary($value)
80
    {
81
        return $this->where(Material::F_PRIMARY, $value);
82
    }
83
84
    /**
85
     * Add identifier field query condition.
86
     *
87
     * @param string $value Field value
88
     * @return self Chaining
89
     * @see Material::where()
90
     */
91
    public function identifier($value)
92
    {
93
        return $this->where(Material::F_IDENTIFIER, $value);
94
    }
95
96
    /**
97
     * Add entity published field query condition.
98
     *
99
     * @param string $value Field value
100
     * @return self Chaining
101
     * @see Material::where()
102
     */
103
    public function published($value)
104
    {
105
        return $this->where(Material::F_PUBLISHED, $value);
106
    }
107
108
    /**
109
     * Add entity creation field query condition.
110
     *
111
     * @param string $value Field value
112
     * @param string $relation @see ArgumentInterface types
113
     * @return self Chaining
114
     * @see Material::where()
115
     */
116
    public function created($value, $relation = ArgumentInterface::EQUAL)
117
    {
118
        return $this->where(Material::F_CREATED, $this->convertToDateTime($value), $relation);
119
    }
120
121
    /**
122
     * Add entity modification field query condition.
123
     *
124
     * @param string $value Field value
125
     * @param string $relation @see ArgumentInterface types
126
     * @return self Chaining
127
     * @see Material::where()
128
     */
129
    public function modified($value, $relation = ArgumentInterface::EQUAL)
130
    {
131
        return $this->where(Material::F_MODIFIED, $this->convertToDateTime($value), $relation);
132
    }
133
134
    /**
135
     * Perform SamsonCMS query and get entities collection.
136
     *
137
     * @return \samsoncms\api\Entity[] Collection of found entities
138
     */
139
    public function find()
140
    {
141
        // Proxy to regular database query
142
        return $this->query
143
            ->entity(static::$identifier)
144
            ->whereCondition($this->conditions)
145
            ->exec();
146
    }
147
148
    /**
149
     * Perform SamsonCMS query and get collection of entities fields.
150
     *
151
     * @param string $fieldName Entity field name
152
     * @return array Collection of entity fields
153
     */
154
    public function fields($fieldName)
155
    {
156
        // Proxy to regular database query
157
        return $this->query
158
            ->entity(static::$identifier)
159
            ->whereCondition($this->conditions)
160
            ->fields($fieldName);
161
    }
162
163
    /**
164
     * Perform SamsonCMS query and get first matching entity.
165
     *
166
     * @return \samsoncms\api\Entity Firt matching entity
167
     */
168
    public function first()
169
    {
170
        // Proxy to regular database query
171
        $return = $this->query
172
            ->entity(static::$identifier)
173
            ->limit(1)
174
            ->whereCondition($this->conditions)
175
            ->exec();
176
177
        return array_shift($return);
178
    }
179
180
    /**
181
     * Generic constructor.
182
     *
183
     * @param QueryInterface $query Database query instance
184
     */
185
    public function __construct(QueryInterface $query)
186
    {
187
        $this->query = $query;
188
        $this->conditions = new Condition();
189
    }
190
}
191