Completed
Push — master ( 20ce86...00401a )
by Chris
02:49
created

Join   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 133
wmc 11
c 1
b 0
f 0
lcom 3
cbo 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B resolveResource() 0 19 5
A to() 0 8 2
A on() 0 6 1
A where() 0 6 1
A __get() 0 4 1
1
<?php
2
namespace Darya\Database\Storage\Query;
3
4
/**
5
 * Represents a join from one resource to another.
6
 * 
7
 * @property-read string $type
8
 * @property-read string $resource
9
 * @property-read string $alias
10
 * @property-read array  $conditions
11
 * @property-read array  $filters
12
 * 
13
 * @author Chris Andrew <[email protected]>
14
 */
15
class Join
16
{
17
	/**
18
	 * The type of the join.
19
	 * 
20
	 * @var string
21
	 */
22
	protected $type;
23
	
24
	/**
25
	 * The resource to join to.
26
	 * 
27
	 * @var string
28
	 */
29
	protected $resource;
30
	
31
	/**
32
	 * An alias for the resource to join to.
33
	 * 
34
	 * @var string
35
	 */
36
	protected $alias;
37
	
38
	/**
39
	 * Condition strings to join on.
40
	 * 
41
	 * @var string[]
42
	 */
43
	protected $conditions = array();
44
	
45
	/**
46
	 * Conditions, with values, to join on.
47
	 * 
48
	 * @var mixed[]
49
	 */
50
	protected $filter = array();
51
	
52
	/**
53
	 * Instantiate a new join.
54
	 * 
55
	 * @param string $type
56
	 * @param string $to
0 ignored issues
show
Bug introduced by
There is no parameter named $to. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
	 */
58
	public function __construct($type, $resource)
59
	{
60
		$this->type = $type;
61
		
62
		list($resource, $alias) = static::resolveResource($resource);
63
		
64
		$this->to($resource, $alias);
65
	}
66
	
67
	/**
68
	 * Resolve the given resource string to a resource name and optional alias.
69
	 * 
70
	 * @param string $resource
71
	 * @return array
72
	 */
73
	protected static function resolveResource($resource) {
74
		if (empty($resource)) {
75
			return array(null, null);
76
		}
77
		
78
		$alias = null;
79
		
80
		$parts = preg_split('/\s+/', $resource, 3);
81
		
82
		if (count($parts) > 2 && strtolower($parts[1]) === 'as') {
83
			$alias = $parts[2];
84
		} else if (count($parts) > 1) {
85
			$alias = $parts[1];
86
		}
87
		
88
		$resource = $parts[0];
89
		
90
		return array($resource, $alias);
91
	}
92
	
93
	/**
94
	 * Set the resource to join to, optionally providing an alias to use.
95
	 * 
96
	 * @param string $resource
97
	 * @param string $alias    [optional]
98
	 * @return $this
99
	 */
100
	public function to($resource, $alias = null)
101
	{
102
		$this->resource = $resource;
103
		
104
		$this->alias = $alias ?: $this->alias;
105
		
106
		return $this;
107
	}
108
	
109
	/**
110
	 * Add a plain condition string to join on.
111
	 * 
112
	 * @param string $condition
113
	 * @return $this
114
	 */
115
	public function on($condition)
116
	{
117
		$this->conditions[] = $condition;
118
		
119
		return $this;
120
	}
121
	
122
	/**
123
	 * Add a complex condition value to join on.
124
	 * 
125
	 * @param string $field
126
	 * @param mixed  $value
127
	 * @return $this
128
	 */
129
	public function where($field, $value)
130
	{
131
		$this->filter[$field] = $value;
132
		
133
		return $this;
134
	}
135
	
136
	/**
137
	 * Dynamically retrieve a property.
138
	 * 
139
	 * @param string $property
140
	 * @return mixed
141
	 */
142
	public function __get($property)
143
	{
144
		return $this->$property;
145
	}
146
	
147
}
148