EntityQueryTrait::guid()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
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 http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * This trait is used for building entity query class for entity model.
17
 *
18
 * @version 2.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 62
    public function buildNoInitModel()
31
    {
32 62
        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 62
    }
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 8
    public function guid($guid, $like = false)
45
    {
46 8
        $model = $this->noInitModel;
47 8
        return $this->likeCondition($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 12
    public function id($id, $like = false)
57
    {
58 12
        $model = $this->noInitModel;
59 12
        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 1
    public function createdAt($start = null, $end = null)
69
    {
70 1
        $model = $this->noInitModel;
71 1
        if (!is_string($model->createdAtAttribute)) {
72
            return $this;
73
        }
74 1
        return static::range($this, $model->createdAtAttribute, $start, $end);
75
    }
76
77
    /**
78
     * Specify update time range.
79
     * @param string $start 
80
     * @param string $end
81
     * @return $this
82
     */
83 1
    public function updatedAt($start = null, $end = null)
84
    {
85 1
        $model = $this->noInitModel;
86 1
        if (!is_string($model->updatedAtAttribute)) {
87
            return $this;
88
        }
89 1
        return static::range($this, $model->updatedAtAttribute, $start, $end);
90
    }
91
92
    public static $pageAll = 'all';
93
    public static $defaultPageSize = 10;
94
95
    /**
96
     * Specify page condition.
97
     * @param string|int $pageSize It will return all models if it is 'all',
98
     * or it will be regarded as sum of models.
99
     * @param int $currentPage The current page number if it is integer begun with 0.
100
     * @return $this
101
     */
102
    public function page($pageSize = 10, $currentPage = 0)
103
    {
104
        if ($pageSize === static::$pageAll) {
105
            return $this;
106
        }
107
108
        /* normalize $currentPage and $currentPage */
109
        if (!is_numeric($currentPage) || $currentPage < 0) {
110
            $currentPage = 0;
111
        }
112
        $currentPage = (int) $currentPage;
113
        if (!is_numeric($pageSize) || $pageSize < 1) {
114
            $pageSize = static::$defaultPageSize;
115
        }
116
        $pageSize = (int) $pageSize;
117
        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...
118
    }
119
}
120