1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/discovery package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[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 Puli\Discovery\Binding; |
13
|
|
|
|
14
|
|
|
use Puli\Discovery\Api\Binding\Binding; |
15
|
|
|
use Puli\Discovery\Api\Binding\Initializer\NotInitializedException; |
16
|
|
|
use Puli\Discovery\Api\Type\MissingParameterException; |
17
|
|
|
use Puli\Discovery\Api\Type\NoSuchParameterException; |
18
|
|
|
use Puli\Repository\Api\ResourceCollection; |
19
|
|
|
use Puli\Repository\Api\ResourceRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Binds one or more resources to a binding type. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ResourceBinding extends AbstractBinding |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $query; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $language; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ResourceRepository |
42
|
|
|
*/ |
43
|
|
|
private $repo; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new resource binding. |
47
|
|
|
* |
48
|
|
|
* A resource binding has a query that is used to retrieve the resources |
49
|
|
|
* matched by the binding. |
50
|
|
|
* |
51
|
|
|
* You can pass parameters that have been defined for the type. If you pass |
52
|
|
|
* unknown parameters, or if a required parameter is missing, an exception |
53
|
|
|
* is thrown. |
54
|
|
|
* |
55
|
|
|
* All parameters that you do not set here will receive the default values |
56
|
|
|
* set for the parameter. |
57
|
|
|
* |
58
|
|
|
* @param string $query The resource query. |
59
|
|
|
* @param string $typeName The type to bind against. |
60
|
|
|
* @param array $parameterValues The values of the parameters defined |
61
|
|
|
* for the type. |
62
|
|
|
* @param string $language The language of the resource query. |
63
|
|
|
* |
64
|
|
|
* @throws NoSuchParameterException If an invalid parameter was passed. |
65
|
|
|
* @throws MissingParameterException If a required parameter was not passed. |
66
|
|
|
*/ |
67
|
115 |
|
public function __construct($query, $typeName, array $parameterValues = array(), $language = 'glob') |
68
|
|
|
{ |
69
|
115 |
|
parent::__construct($typeName, $parameterValues); |
70
|
|
|
|
71
|
114 |
|
$this->query = $query; |
72
|
114 |
|
$this->language = $language; |
73
|
114 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the query for the resources of the binding. |
77
|
|
|
* |
78
|
|
|
* @return string The resource query. |
79
|
|
|
*/ |
80
|
1 |
|
public function getQuery() |
81
|
|
|
{ |
82
|
1 |
|
return $this->query; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the language of the query. |
87
|
|
|
* |
88
|
|
|
* @return string The query language. |
89
|
|
|
*/ |
90
|
1 |
|
public function getLanguage() |
91
|
|
|
{ |
92
|
1 |
|
return $this->language; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the bound resources. |
97
|
|
|
* |
98
|
|
|
* @return ResourceCollection The bound resources. |
99
|
|
|
*/ |
100
|
2 |
|
public function getResources() |
101
|
|
|
{ |
102
|
2 |
|
if (null === $this->repo) { |
103
|
1 |
|
throw new NotInitializedException('The repository of the resource binding must be set before accessing resources.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $this->repo->find($this->query, $this->language); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets the repository used to load resources. |
111
|
|
|
* |
112
|
|
|
* @param ResourceRepository $repo The resource repository. |
113
|
|
|
*/ |
114
|
1 |
|
public function setRepository(ResourceRepository $repo) |
115
|
|
|
{ |
116
|
1 |
|
$this->repo = $repo; |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
58 |
|
public function equals(Binding $other) |
123
|
|
|
{ |
124
|
58 |
|
if (!parent::equals($other)) { |
125
|
17 |
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** @var ResourceBinding $other */ |
129
|
41 |
|
if ($this->query !== $other->query) { |
130
|
36 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
5 |
|
return $this->language === $other->language; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
71 |
|
protected function preSerialize(array &$data) |
140
|
|
|
{ |
141
|
71 |
|
parent::preSerialize($data); |
142
|
|
|
|
143
|
71 |
|
$data[] = $this->query; |
144
|
71 |
|
$data[] = $this->language; |
145
|
71 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
28 |
|
protected function postUnserialize(array &$data) |
151
|
|
|
{ |
152
|
28 |
|
$this->language = array_pop($data); |
153
|
28 |
|
$this->query = array_pop($data); |
154
|
|
|
|
155
|
28 |
|
parent::postUnserialize($data); |
156
|
28 |
|
} |
157
|
|
|
} |
158
|
|
|
|