1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the moss-storage package |
5
|
|
|
* |
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moss\Storage\Query; |
13
|
|
|
|
14
|
|
|
use Moss\Storage\Model\ModelInterface; |
15
|
|
|
use Moss\Storage\Query\Relation\RelationFactoryInterface; |
16
|
|
|
use Moss\Storage\Query\Relation\RelationInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Abstract Relational |
20
|
|
|
* Class implementing relational interface |
21
|
|
|
* |
22
|
|
|
* @package Moss\Storage |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractRelational |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RelationFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $factory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RelationInterface[] |
33
|
|
|
*/ |
34
|
|
|
protected $relations = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns model |
38
|
|
|
* |
39
|
|
|
* @return ModelInterface |
40
|
|
|
*/ |
41
|
|
|
abstract public function model(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds relation to query |
45
|
|
|
* |
46
|
|
|
* @param string|array $relation |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
* @throws QueryException |
50
|
|
|
*/ |
51
|
|
|
public function with($relation) |
52
|
|
|
{ |
53
|
|
|
foreach ((array) $relation as $node) { |
54
|
|
|
$instance = $this->factory->build($this->model(), $node); |
55
|
|
|
$this->relations[$instance->name()] = $instance; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns relation instance |
63
|
|
|
* |
64
|
|
|
* @param string $relation |
65
|
|
|
* |
66
|
|
|
* @return RelationInterface |
67
|
|
|
* @throws QueryException |
68
|
|
|
*/ |
69
|
|
|
public function relation($relation) |
70
|
|
|
{ |
71
|
|
|
list($name, $furtherRelations) = $this->factory->splitRelationName($relation); |
72
|
|
|
|
73
|
|
|
if (!isset($this->relations[$name])) { |
74
|
|
|
throw new QueryException(sprintf('Unable to retrieve relation "%s" query, relation does not exists in query "%s"', $name, $this->model()->entity())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($furtherRelations) { |
78
|
|
|
return $this->relations[$name]->relation($furtherRelations); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->relations[$name]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|