1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object\Domain |
8
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
9
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/*********************************************************************************** |
14
|
|
|
* The MIT License (MIT) |
15
|
|
|
* |
16
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
19
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
20
|
|
|
* the Software without restriction, including without limitation the rights to |
21
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
22
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
23
|
|
|
* subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
31
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
32
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
33
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
34
|
|
|
***********************************************************************************/ |
35
|
|
|
|
36
|
|
|
namespace Apparat\Object\Domain\Repository; |
37
|
|
|
|
38
|
|
|
use Apparat\Kernel\Ports\Kernel; |
39
|
|
|
use Apparat\Object\Domain\Model\Object\Collection; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\ManagerInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
43
|
|
|
use Apparat\Object\Domain\Model\Path\PathInterface; |
44
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
45
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Abstract object repository |
49
|
|
|
* |
50
|
|
|
* @package Apparat\Object\Domain\Repository |
51
|
|
|
*/ |
52
|
|
|
class Repository implements RepositoryInterface |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* Apparat base URL |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $url = null; |
60
|
|
|
/** |
61
|
|
|
* Adapter strategy |
62
|
|
|
* |
63
|
|
|
* @var AdapterStrategyInterface |
64
|
|
|
*/ |
65
|
|
|
protected $adapterStrategy = null; |
66
|
|
|
|
67
|
|
|
/******************************************************************************* |
68
|
|
|
* PUBLIC METHODS |
69
|
|
|
*******************************************************************************/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Repository constructor |
73
|
|
|
* |
74
|
|
|
* @param string $url Apparat base URL |
75
|
|
|
* @param array $config Adapter strategy configuration |
76
|
|
|
*/ |
77
|
17 |
|
public function __construct( |
78
|
|
|
$url, |
79
|
|
|
array $config |
80
|
|
|
) { |
81
|
17 |
|
$this->url = rtrim('/'.$url, '/'); |
82
|
17 |
|
$this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config); |
83
|
11 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Find objects by selector |
87
|
|
|
* |
88
|
|
|
* @param SelectorInterface $selector Object selector |
89
|
|
|
* @return Collection Object collection |
90
|
|
|
*/ |
91
|
7 |
|
public function findObjects(SelectorInterface $selector) |
92
|
|
|
{ |
93
|
7 |
|
return Kernel::create(Collection::class, [$this->adapterStrategy->findObjectPaths($selector, $this)]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create an object and add it to the repository |
98
|
|
|
* |
99
|
|
|
* @param string|Type $type Object type |
100
|
|
|
* @param string $payload Object payload |
101
|
|
|
* @param array $propertyData Object property data |
102
|
|
|
* @return ObjectInterface Object |
103
|
|
|
*/ |
104
|
2 |
|
public function createObject($type, $payload = '', array $propertyData = []) |
105
|
|
|
{ |
106
|
|
|
// Instantiate the object type |
107
|
2 |
|
if (!($type instanceof Type)) { |
108
|
|
|
/** @var Type $type */ |
109
|
2 |
|
$type = Kernel::create(Type::class, [$type]); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** @var ManagerInterface $objectManager */ |
113
|
2 |
|
$objectManager = Kernel::create(Service::class)->getObjectManager(); |
114
|
|
|
|
115
|
|
|
// Create and return the object |
116
|
2 |
|
return $objectManager->createObject($this, $type, $payload, $propertyData); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Delete and object from the repository |
121
|
|
|
* |
122
|
|
|
* @param ObjectInterface $object Object |
123
|
|
|
* @return boolean Success |
124
|
|
|
*/ |
125
|
|
|
public function deleteObject(ObjectInterface $object) |
126
|
|
|
{ |
127
|
|
|
// TODO: Implement deleteObject() method. |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update an object in the repository |
132
|
|
|
* |
133
|
|
|
* @param ObjectInterface $object Object |
134
|
|
|
* @return bool Success |
135
|
|
|
*/ |
136
|
|
|
public function updateObject(ObjectInterface $object) |
137
|
|
|
{ |
138
|
|
|
echo 'persisting'; |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Load an object from this repository |
145
|
|
|
* |
146
|
|
|
* @param PathInterface $path Object path |
147
|
|
|
* @return ObjectInterface Object |
148
|
|
|
*/ |
149
|
19 |
|
public function loadObject(PathInterface $path) |
150
|
|
|
{ |
151
|
|
|
/** @var ManagerInterface $objectManager */ |
152
|
19 |
|
$objectManager = Kernel::create(Service::class)->getObjectManager(); |
153
|
|
|
|
154
|
|
|
/** @var RepositoryPathInterface $repositoryPath */ |
155
|
19 |
|
$repositoryPath = Kernel::create(RepositoryPath::class, [$this, $path]); |
156
|
|
|
|
157
|
|
|
// Load and return the object |
158
|
19 |
|
return $objectManager->loadObject($repositoryPath); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return the repository's adapter strategy |
163
|
|
|
* |
164
|
|
|
* @return AdapterStrategyInterface Adapter strategy |
165
|
|
|
*/ |
166
|
21 |
|
public function getAdapterStrategy() |
167
|
|
|
{ |
168
|
21 |
|
return $this->adapterStrategy; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return the repository URL (relative to Apparat base URL) |
173
|
|
|
* |
174
|
|
|
* @return string Repository URL |
175
|
|
|
*/ |
176
|
14 |
|
public function getUrl() |
177
|
|
|
{ |
178
|
14 |
|
return $this->url; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return the repository size (number of objects in the repository) |
183
|
|
|
* |
184
|
|
|
* @return int Repository size |
185
|
|
|
*/ |
186
|
1 |
|
public function getSize() |
187
|
|
|
{ |
188
|
|
|
// TODO: Implement getSize() method. |
189
|
1 |
|
} |
190
|
|
|
} |
191
|
|
|
|