Completed
Push — master ( 89d22a...101fe8 )
by Vitaly
03:34
created

Generic::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 4
Metric Value
c 7
b 0
f 4
dl 0
loc 5
rs 9.4286
cc 1
eloc 2
nc 1
nop 0
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\QueryInterface;
13
14
/**
15
 * Material with additional fields query.
16
 * @package samsoncms\api
17
 */
18
class Generic
19
{
20
    /** @var array Collection of all supported entity fields */
21
    protected static $parentFields = array(
22
        Material::F_PRIORITY => Material::F_PRIORITY,
23
        Material::F_IDENTIFIER => Material::F_IDENTIFIER,
24
        Material::F_DELETION => Material::F_DELETION,
25
        Material::F_PUBLISHED => Material::F_PUBLISHED,
26
        Material::F_PARENT => Material::F_PARENT,
27
        Material::F_CREATED => Material::F_CREATED,
28
    );
29
30
    /** @var string Entity identifier */
31
    protected static $identifier;
32
33
    /** @var string Entity navigation identifiers */
34
    protected static $navigationIDs = array();
35
36
    /** @var QueryInterface Database query instance */
37
    protected $query;
38
39
    /** @var array Collection of entity fields to retrieved from database */
40
    protected $selectedFields;
41
42
    /**
43
     * Add condition to current query.
44
     *
45
     * @param string $fieldName Entity field name
46
     * @param string $fieldValue Value
47
     * @return self Chaining
48
     */
49
    public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)
50
    {
51
        // Proxy call
52
        $this->query->where($fieldName, $fieldValue, $fieldRelation);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Add identifier field query condition.
59
     *
60
     * @param string $value Field value
61
     * @return self Chaining
62
     * @see Generic::where()
63
     */
64
    public function identifier($value)
65
    {
66
        return $this->where(Material::F_IDENTIFIER, $value);
67
    }
68
69
    /**
70
     * Add entity published field query condition.
71
     *
72
     * @param string $value Field value
73
     * @return self Chaining
74
     * @see Generic::where()
75
     */
76
    public function published($value)
77
    {
78
        return $this->where(Material::F_PUBLISHED, $value);
79
    }
80
81
    /**
82
     * Add entity creation field query condition.
83
     *
84
     * @param string $value Field value
85
     * @param string $relation @see ArgumentInterface types
86
     * @return self Chaining
87
     * @see Generic::where()
88
     */
89
    public function created($value, $relation = ArgumentInterface::EQUAL)
90
    {
91
        return $this->where(Material::F_CREATED, $value, $relation);
92
    }
93
94
    /**
95
     * Add entity modification field query condition.
96
     *
97
     * @param string $value Field value
98
     * @param string $relation @see ArgumentInterface types
99
     * @return self Chaining
100
     * @see Generic::where()
101
     */
102
    public function modified($value, $relation = ArgumentInterface::EQUAL)
103
    {
104
        return $this->where(Material::F_MODIFIED, $value, $relation);
105
    }
106
107
    /**
108
     * Perform SamsonCMS query and get entities collection.
109
     *
110
     * @return Material[] Collection of found entities
111
     */
112
    public function find()
113
    {
114
        // Proxy to regular database query
115
        return $this->query->exec();
116
    }
117
118
    /**
119
     * Generic constructor.
120
     *
121
     * @param QueryInterface $query Database query instance
122
     */
123
    public function __construct(QueryInterface $query)
124
    {
125
        $this->query = $query;
126
    }
127
}
128