1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Application |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Application\Factory; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Application\Service\TypeService; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\ResourceInterface; |
43
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
44
|
|
|
use Apparat\Object\Domain\Model\Properties\MetaProperties; |
45
|
|
|
use Apparat\Object\Domain\Model\Properties\SystemProperties; |
46
|
|
|
use Apparat\Object\Domain\Model\Uri\RepositoryLocatorInterface; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Object factory |
50
|
|
|
* |
51
|
|
|
* @package Apparat\Object |
52
|
|
|
* @subpackage Apparat\Object\Application |
53
|
|
|
*/ |
54
|
|
|
class ObjectFactory |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Create an object |
58
|
|
|
* |
59
|
|
|
* @param RepositoryLocatorInterface $locator Repository object locator |
60
|
|
|
* @param ResourceInterface $objectResource |
61
|
|
|
* @return ObjectInterface Object |
62
|
|
|
* @throws InvalidArgumentException If the object type is undefined |
63
|
|
|
*/ |
64
|
42 |
|
public static function createFromResource(RepositoryLocatorInterface $locator, ResourceInterface $objectResource) |
65
|
|
|
{ |
66
|
42 |
|
$propertyData = $objectResource->getPropertyData(); |
67
|
|
|
|
68
|
|
|
// If the object type is undefined |
69
|
42 |
|
if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) || |
70
|
41 |
|
!is_array($propertyData[SystemProperties::COLLECTION]) || |
71
|
42 |
|
empty($propertyData[SystemProperties::COLLECTION]['type']) |
72
|
|
|
) { |
73
|
1 |
|
throw new InvalidArgumentException( |
74
|
1 |
|
'Undefined object type', |
75
|
1 |
|
InvalidArgumentException::UNDEFINED_OBJECT_TYPE |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Determine the object class |
80
|
41 |
|
$objectClass = self::objectClassFromType($locator->getObjectType()); |
81
|
|
|
|
82
|
|
|
// Instantiate the object |
83
|
40 |
|
return Kernel::create($objectClass, [$locator, $objectResource->getPayload(), $propertyData]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Determine and validate the object class name from its type |
88
|
|
|
* |
89
|
|
|
* @param Type $type Object type |
90
|
|
|
* @return string Object class name |
91
|
|
|
* @throws InvalidArgumentException If the object type is invalid |
92
|
|
|
*/ |
93
|
47 |
|
protected static function objectClassFromType(Type $type) |
94
|
|
|
{ |
95
|
|
|
// If the object type is invalid |
96
|
47 |
|
$objectType = $type->getType(); |
97
|
47 |
|
$objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType); |
98
|
47 |
|
if (!TypeService::isEnabled($objectType) || !class_exists($objectClass)) { |
99
|
1 |
|
throw new InvalidArgumentException( |
100
|
1 |
|
sprintf('Invalid object type "%s"', $objectType), |
101
|
1 |
|
InvalidArgumentException::INVALID_OBJECT_TYPE |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
46 |
|
return $objectClass; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create and return a new object |
110
|
|
|
* |
111
|
|
|
* @param RepositoryLocatorInterface $locator Repository object locator |
112
|
|
|
* @param string $payload Object payload |
113
|
|
|
* @param array $propertyData Object property data |
114
|
|
|
* @return ObjectInterface Object |
115
|
|
|
*/ |
116
|
7 |
|
public static function createFromParams( |
117
|
|
|
RepositoryLocatorInterface $locator, |
118
|
|
|
$payload = '', |
119
|
|
|
array $propertyData = [] |
120
|
|
|
) { |
121
|
|
|
// Determine the object class |
122
|
7 |
|
$objectClass = self::objectClassFromType($locator->getObjectType()); |
123
|
|
|
|
124
|
|
|
// Prepare the system properties collection |
125
|
7 |
|
$systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) || |
126
|
7 |
|
!is_array($propertyData[SystemProperties::COLLECTION])) ? [] : $propertyData[SystemProperties::COLLECTION]; |
127
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_ID] = $locator->getId()->getId(); |
128
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_TYPE] = $locator->getObjectType()->getType(); |
129
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_REVISION] = $locator->getRevision()->getRevision(); |
130
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_CREATED] = |
131
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_MODIFIED] = $locator->getCreationDate(); |
132
|
7 |
|
if (empty($systemPropertyData[SystemProperties::PROPERTY_LANGUAGE])) { |
133
|
7 |
|
$systemPropertyData[SystemProperties::PROPERTY_LANGUAGE] = getenv('OBJECT_DEFAULT_LANGUAGE'); |
134
|
|
|
} |
135
|
7 |
|
$propertyData[SystemProperties::COLLECTION] = $systemPropertyData; |
136
|
|
|
|
137
|
|
|
// Prepare the meta properties collection |
138
|
7 |
|
$metaPropertyData = (empty($propertyData[MetaProperties::COLLECTION]) || |
139
|
7 |
|
!is_array($propertyData[MetaProperties::COLLECTION])) ? [] : $propertyData[MetaProperties::COLLECTION]; |
140
|
7 |
|
$metaPropertyData[MetaProperties::PROPERTY_PRIVACY] = getenv('OBJECT_DEFAULT_PRIVACY'); |
141
|
7 |
|
$propertyData[MetaProperties::COLLECTION] = $metaPropertyData; |
142
|
|
|
|
143
|
|
|
// Instantiate the object |
144
|
|
|
/** @var ObjectInterface $object */ |
145
|
7 |
|
$object = Kernel::create($objectClass, [$locator, '', $propertyData]); |
146
|
7 |
|
return $object->setPayload($payload); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|