|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database\Storage\Query; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Represents a join from one resource to another. |
|
6
|
|
|
* |
|
7
|
|
|
* TODO: Formalise join types as constants. |
|
8
|
|
|
* |
|
9
|
|
|
* @property-read string $type |
|
10
|
|
|
* @property-read string $resource |
|
11
|
|
|
* @property-read string $alias |
|
12
|
|
|
* @property-read array $conditions |
|
13
|
|
|
* @property-read array $filter |
|
14
|
|
|
* |
|
15
|
|
|
* @author Chris Andrew <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Join |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The type of the join. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $type; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The resource to join to. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $resource; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* An alias for the resource to join to. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $alias; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Condition strings to join on. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $conditions = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Conditions, with values, to join on. |
|
49
|
|
|
* |
|
50
|
|
|
* @var mixed[] |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $filter = array(); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Instantiate a new join. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $type |
|
58
|
|
|
* @param string $resource |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($type, $resource) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->type = $type; |
|
63
|
|
|
|
|
64
|
|
|
list($resource, $alias) = static::resolveResource($resource); |
|
65
|
|
|
|
|
66
|
|
|
$this->to($resource, $alias); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Resolve the given resource string to a resource name and optional alias. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $resource |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function resolveResource($resource) |
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($resource)) { |
|
78
|
|
|
return array(null, null); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$alias = null; |
|
82
|
|
|
|
|
83
|
|
|
$parts = preg_split('/\s+/', $resource, 3); |
|
84
|
|
|
|
|
85
|
|
|
if (count($parts) > 2 && strtolower($parts[1]) === 'as') { |
|
86
|
|
|
$alias = $parts[2]; |
|
87
|
|
|
} else if (count($parts) > 1) { |
|
88
|
|
|
$alias = $parts[1]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$resource = $parts[0]; |
|
92
|
|
|
|
|
93
|
|
|
return array($resource, $alias); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set the resource to join to, optionally providing an alias to use. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $resource |
|
100
|
|
|
* @param string $alias [optional] |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function to($resource, $alias = null) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->resource = $resource; |
|
106
|
|
|
|
|
107
|
|
|
$this->alias = $alias ?: $this->alias; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add a plain condition string to join on. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $condition |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function on($condition) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->conditions[] = $condition; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Add a complex condition value to join on. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $field |
|
129
|
|
|
* @param mixed $value |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function where($field, $value) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->filter[$field] = $value; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Dynamically retrieve a property. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $property |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
|
|
public function __get($property) |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->$property; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|