Completed
Push — master ( 9aa93d...bd75c7 )
by vistart
08:03
created

EntityQueryTrait::guid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 *   _   __ __ _____ _____ ___  ____  _____
5
 *  | | / // // ___//_  _//   ||  __||_   _|
6
 *  | |/ // /(__  )  / / / /| || |     | |
7
 *  |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
/**
16
 * This trait is used for building entity query class for entity model.
17
 *
18
 * @version 1.0
19
 * @author vistart <[email protected]>
20
 */
21
trait EntityQueryTrait
22
{
23
    use QueryTrait;
24
    
25
    public $noInitModel;
26
27
    /**
28
     * Build model without any initializations.
29
     */
30 295
    public function buildNoInitModel()
31
    {
32 295
        if (empty($this->noInitModel) && is_string($this->modelClass)) {
33
            $modelClass = $this->modelClass;
0 ignored issues
show
Bug introduced by
The property modelClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
            $this->noInitModel = $modelClass::buildNoInitModel();
35
        }
36 295
    }
37
    
38
    /**
39
     * Specify guid attribute.
40
     * @param string|array $guid
41
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
42
     * @return $this
43
     */
44 14
    public function guid($guid, $like = false)
45
    {
46 14
        $model = $this->noInitModel;
47 14
        return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
48
    }
49
    
50
    /**
51
     * Specify id attribute.
52
     * @param string|integer|array $id
53
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
54
     * @return $this
55
     */
56 9
    public function id($id, $like = false)
57
    {
58 9
        $model = $this->noInitModel;
59 9
        return $this->likeCondition($id, $model->idAttribute, $like);
60
    }
61
    
62
    /**
63
     * Specify create time range.
64
     * @param string $start
65
     * @param string $end
66
     * @return $this
67
     */
68 5
    public function createdAt($start = null, $end = null)
69
    {
70 5
        $model = $this->noInitModel;
71
        /* @var $this yii\db\ActiveQuery */
72 5
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
73
            return $this;
74
        }
75 5
        return static::range($this, $model->createdAtAttribute, $start, $end);
76
    }
77
    
78
    /**
79
     * Specify order by creation time.
80
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
81
     * @return $this
82
     */
83 2
    public function orderByCreatedAt($sort = SORT_ASC)
84
    {
85 2
        $model = $this->noInitModel;
86
        /* @var $this yii\db\ActiveQuery */
87 2
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
88
            return $this;
89
        }
90 2
        return $this->addOrderBy([$model->createdAtAttribute => $sort]);
91
    }
92
    
93
    /**
94
     * Specify update time range.
95
     * @param string $start 
96
     * @param string $end
97
     * @return $this
98
     */
99 7
    public function updatedAt($start = null, $end = null)
100
    {
101 7
        $model = $this->noInitModel;
102
        /* @var $this yii\db\ActiveQuery */
103 7
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
104
            return $this;
105
        }
106 7
        return static::range($this, $model->updatedAtAttribute, $start, $end);
107
    }
108
    
109
    /**
110
     * Specify order by update time.
111
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
112
     * @return $this
113
     */
114 2
    public function orderByUpdatedAt($sort = SORT_ASC)
115
    {
116 2
        $model = $this->noInitModel;
117
        /* @var $this yii\db\ActiveQuery */
118 2
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
119
            return $this;
120
        }
121 2
        return $this->addOrderBy([$model->updatedAtAttribute => $sort]);
122
    }
123
    
124
    public static $pageAll = 'all';
125
    public static $defaultPageSize = 10;
126
    
127
    /**
128
     * Specify page condition.
129
     * @param string|int $pageSize It will return all models if it is 'all',
130
     * or it will be regarded as sum of models.
131
     * @param int $currentPage The current page number if it is integer begun with 0.
132
     * @return $this
133
     */
134 3
    public function page($pageSize = 10, $currentPage = 0)
135
    {
136 3
        if ($pageSize === static::$pageAll) {
137 2
            return $this;
138
        }
139
        /* normalize $currentPage and $currentPage */
140 1
        if (!is_numeric($currentPage) || $currentPage < 0) {
141 1
            $currentPage = 0;
142
        }
143 1
        $currentPage = (int) $currentPage;
144 1
        if (!is_numeric($pageSize) || $pageSize < 1) {
145 1
            $pageSize = static::$defaultPageSize;
146
        }
147 1
        $pageSize = (int) $pageSize;
148 1
        return $this->limit($pageSize)->offset($pageSize * $currentPage);
0 ignored issues
show
Bug introduced by
It seems like limit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
149
    }
150
}
151