Base::__clone()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tsukasa\QueryBuilder\LookupBuilder;
4
5
use Tsukasa\QueryBuilder\Callbacks\AbstractColumnCallback;
6
use Tsukasa\QueryBuilder\Callbacks\AbstractFetchColumnCallback;
7
use Tsukasa\QueryBuilder\Callbacks\AbstractJoinCallback;
8
use Tsukasa\QueryBuilder\Exception\QBException;
9
use Tsukasa\QueryBuilder\Interfaces\IAdapter;
10
use Tsukasa\QueryBuilder\Interfaces\ILookupBuilder;
11
use Tsukasa\QueryBuilder\Interfaces\ILookupCollection;
12
use Tsukasa\QueryBuilder\Interfaces\QueryBuilderInterface;
13
14
abstract class Base implements ILookupBuilder
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $default = 'exact';
20
    /**
21
     * @var string
22
     */
23
    protected $separator = '__';
24
    /**
25
     * @var AbstractColumnCallback|null
26
     */
27
    protected $columnCallback;
28
    /**
29
     * @var AbstractJoinCallback|null
30
     */
31
    protected $joinCallback;
32
    /**
33
     * @var AbstractFetchColumnCallback|null
34
     */
35
    protected $fetchColumnCallback;
36
    /**
37
     * @var ILookupCollection[]
38
     */
39
    private $_lookupCollections = [];
40
41
    public function __clone()
42
    {
43
        foreach ($this as $key => $val) {
44
            $this->{$key} = clone $val;
45
        }
46
    }
47
48
    /**
49
     * @param ILookupCollection $lookupCollection
50
     * @return $this
51
     */
52
    public function addLookupCollection(ILookupCollection $lookupCollection)
53
    {
54
        $this->_lookupCollections[] = $lookupCollection;
55
        return $this;
56
    }
57
58
    /**
59
     * @param mixed $columnCallback
60
     * @return $this
61
     */
62
    public function setColumnCallback($columnCallback)
63
    {
64
        $this->columnCallback = $columnCallback;
65
        return $this;
66
    }
67
68
    /**
69
     * @param $callback
70
     * @return $this
71
     */
72
    public function setJoinCallback($callback)
73
    {
74
        $this->joinCallback = $callback;
75
        return $this;
76
    }
77
    
78
    public function setFetchColumnCallback($callback)
79
    {
80
        $this->fetchColumnCallback = $callback;
81
        return $this;
82
    }
83
84
    public function getColumnCallback()
85
    {
86
        return $this->columnCallback;
87
    }
88
89
    public function getJoinCallback()
90
    {
91
        return $this->joinCallback;
92
    }
93
94
    public function fetchColumnName($column)
95
    {
96
        if ($this->fetchColumnCallback !== null) {
97
            if ($this->fetchColumnCallback instanceof \Closure) {
0 ignored issues
show
introduced by
$this->fetchColumnCallback is never a sub-type of Closure.
Loading history...
98
                $call = $this->fetchColumnCallback;
99
                return $call($column);
100
            }
101
102
103
            if ($this->fetchColumnCallback instanceof AbstractFetchColumnCallback) {
0 ignored issues
show
introduced by
$this->fetchColumnCallback is always a sub-type of Tsukasa\QueryBuilder\Cal...ractFetchColumnCallback.
Loading history...
104
                return $this->fetchColumnCallback->run($column);
105
            }
106
        }
107
108
        return $column;
109
    }
110
111
    public function runCallback(QueryBuilderInterface $queryBuilder, $lookupNodes, $value)
112
    {
113
        if ($this->columnCallback !== null) {
114
            if ($this->columnCallback instanceof \Closure) {
0 ignored issues
show
introduced by
$this->columnCallback is never a sub-type of Closure.
Loading history...
115
                $call = $this->columnCallback;
116
                return $call($queryBuilder, $this, $lookupNodes, $value);
117
            }
118
119
            if ($this->columnCallback instanceof AbstractColumnCallback) {
0 ignored issues
show
introduced by
$this->columnCallback is always a sub-type of Tsukasa\QueryBuilder\Cal...\AbstractColumnCallback.
Loading history...
120
                return $this->columnCallback->run($queryBuilder, $this, $lookupNodes, $value);
121
            }
122
        }
123
124
        return null;
125
    }
126
127
    public function runJoinCallback(QueryBuilderInterface $queryBuilder, $lookupNodes)
128
    {
129
        if ($this->joinCallback !== null) {
130
            if ($this->joinCallback instanceof \Closure) {
0 ignored issues
show
introduced by
$this->joinCallback is never a sub-type of Closure.
Loading history...
131
                $call = $this->joinCallback->bindTo($this, $this);
132
                return $call($queryBuilder, $this, $lookupNodes);
133
            }
134
135
            if ($this->joinCallback instanceof AbstractJoinCallback) {
0 ignored issues
show
introduced by
$this->joinCallback is always a sub-type of Tsukasa\QueryBuilder\Cal...ks\AbstractJoinCallback.
Loading history...
136
                return $this->joinCallback->run($queryBuilder, $this, $lookupNodes);
137
            }
138
        }
139
140
        return null;
141
    }
142
143
    public function getSeparator()
144
    {
145
        return $this->separator;
146
    }
147
148
    public function getDefault()
149
    {
150
        return $this->default;
151
    }
152
153
    /**
154
     * @param $lookup
155
     * @return bool
156
     */
157
    public function hasLookup($lookup)
158
    {
159
        foreach ($this->_lookupCollections as $collection) {
160
            if ($collection->has($lookup)) {
161
                return true;
162
            }
163
        }
164
        return false;
165
    }
166
167
168
    /**
169
     * @param IAdapter $adapter
170
     * @param $lookup
171
     * @param $column
172
     * @param $value
173
     * @return string
174
     * @exception \Exception
175
     */
176
    public function runLookup(IAdapter $adapter, $lookup, $column, $value)
177
    {
178
        foreach ($this->_lookupCollections as $collection) {
179
            if ($collection->has($lookup)) {
180
                return $collection->process($adapter, $lookup, $column, $value);
181
            }
182
        }
183
        throw new QBException('Unknown lookup: ' . $lookup . ', column: ' . $column . ', value: ' . (is_array($value) ? print_r($value, true) : $value));
184
    }
185
186
    abstract public function parse(QueryBuilderInterface $queryBuilder, array $where);
187
}