Completed
Push — master ( 7fc3f3...dbb6f1 )
by vistart
05:17
created

EntityQueryTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.27%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 9
c 10
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
ccs 17
cts 22
cp 0.7727
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildNoInitModel() 0 7 3
A id() 0 5 1
A guid() 0 5 1
A createdAt() 0 8 2
A updatedAt() 0 8 2
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 36
    public function buildNoInitModel()
31
    {
32 36
        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 36
    }
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 1
    public function guid($guid, $like = false)
45
    {
46 1
        $model = $this->noInitModel;
47 1
        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 10
    public function id($id, $like = false)
57
    {
58 10
        $model = $this->noInitModel;
59 10
        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