Completed
Push — master ( 101fe8...ffb575 )
by Vitaly
02:56
created

Generic::primary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, samsoncms\api\query\Material.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 Generic::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 Generic::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 Generic::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 Generic::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 Generic::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 Material[] Collection of found entities
138
     */
139
    public function find()
140
    {
141
        // Proxy to regular database query
142
        return $this->query->entity(static::$identifier)->whereCondition($this->conditions)->exec();
143
    }
144
145
    /**
146
     * Generic constructor.
147
     *
148
     * @param QueryInterface $query Database query instance
149
     */
150
    public function __construct(QueryInterface $query)
151
    {
152
        $this->query = $query;
153
        $this->conditions = new Condition();
154
    }
155
}
156