-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSelectable.php
More file actions
100 lines (78 loc) · 3.02 KB
/
Selectable.php
File metadata and controls
100 lines (78 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace UniMapper\Query;
use UniMapper\Association;
use UniMapper\Exception;
use UniMapper\Entity\Reflection;
use UniMapper\Entity\Reflection\Property\Option\Assoc;
trait Selectable
{
/** @var array */
protected $adapterAssociations = [];
/** @var array */
protected $remoteAssociations = [];
/** @var array */
protected $selection = [];
public function associate($args)
{
foreach (func_get_args() as $arg) {
if (!is_array($arg)) {
$arg = [$arg];
}
foreach ($arg as $name) {
if (!$this->reflection->hasProperty($name)) {
throw new Exception\QueryException(
"Property '" . $name . "' is not defined on entity "
. $this->reflection->getClassName() . "!"
);
}
$property = $this->reflection->getProperty($name);
if (!$property->hasOption(Assoc::KEY)) {
throw new Exception\QueryException(
"Property '" . $name . "' is not defined as association"
. " on entity " . $this->reflection->getClassName()
. "!"
);
}
$option = $property->getOption(Assoc::KEY);
if ($option->getSourceReflection()->getAdapterName() === $option->getTargetReflection()->getAdapterName()) {
$this->adapterAssociations[$name] = $option;
} else {
$this->remoteAssociations[$name] = Association::create(
$option
);
}
}
}
return $this;
}
public function select($args)
{
foreach (func_get_args() as $arg) {
if (!is_array($arg)) {
$arg = [$arg];
}
foreach ($arg as $name) {
if (!$this->reflection->hasProperty($name)) {
throw new Exception\QueryException(
"Property '" . $name . "' is not defined on entity "
. $this->reflection->getClassName() . "!"
);
}
$property = $this->reflection->getProperty($name);
if ($property->hasOption(Reflection\Property\Option\Assoc::KEY)
|| $property->hasOption(Reflection\Property\Option\Computed::KEY)
|| ($property->hasOption(Reflection\Property\Option\Map::KEY)
&& !$property->getOption(Reflection\Property\Option\Map::KEY))
) {
throw new Exception\QueryException(
"Associations, computed and properties with disabled mapping can not be selected!"
);
}
if (!array_search($name, $this->selection)) {
$this->selection[] = $name;
}
}
}
return $this;
}
}