|
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\Domain\Model\Object\ObjectInterface; |
|
41
|
|
|
use Apparat\Object\Domain\Model\Object\ResourceInterface; |
|
42
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
|
43
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface; |
|
44
|
|
|
use Apparat\Object\Domain\Model\Properties\SystemProperties; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Object factory |
|
48
|
|
|
* |
|
49
|
|
|
* @package Apparat\Object |
|
50
|
|
|
* @subpackage Apparat\Object\Application |
|
51
|
|
|
*/ |
|
52
|
|
|
class ObjectFactory |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* Create an object |
|
56
|
|
|
* |
|
57
|
|
|
* @param RepositoryPathInterface $path Repository object path |
|
58
|
|
|
* @param ResourceInterface $objectResource |
|
59
|
|
|
* @return ObjectInterface Object |
|
60
|
|
|
* @throws InvalidArgumentException If the object type is undefined |
|
61
|
|
|
*/ |
|
62
|
26 |
|
public static function createFromResource(RepositoryPathInterface $path, ResourceInterface $objectResource) |
|
63
|
|
|
{ |
|
64
|
26 |
|
$propertyData = $objectResource->getPropertyData(); |
|
65
|
|
|
|
|
66
|
|
|
// If the object type is undefined |
|
67
|
26 |
|
if (!array_key_exists(SystemProperties::COLLECTION, $propertyData) || |
|
68
|
25 |
|
!is_array($propertyData[SystemProperties::COLLECTION]) || |
|
69
|
26 |
|
empty($propertyData[SystemProperties::COLLECTION]['type']) |
|
70
|
|
|
) { |
|
71
|
1 |
|
throw new InvalidArgumentException( |
|
72
|
1 |
|
'Undefined object type', |
|
73
|
1 |
|
InvalidArgumentException::UNDEFINED_OBJECT_TYPE |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Determine the object class |
|
78
|
25 |
|
$objectClass = self::objectClassFromType($path->getType()); |
|
79
|
|
|
|
|
80
|
|
|
// Instantiate the object |
|
81
|
24 |
|
return Kernel::create($objectClass, [$path, $objectResource->getPayload(), $propertyData]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create and return a new object |
|
86
|
|
|
* |
|
87
|
|
|
* @param RepositoryPathInterface $path Repository object path |
|
88
|
|
|
* @param string $payload Object payload |
|
89
|
|
|
* @param array $propertyData Object property data |
|
90
|
|
|
* @return ObjectInterface Object |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public static function createFromParams(RepositoryPathInterface $path, $payload = '', array $propertyData = []) |
|
93
|
|
|
{ |
|
94
|
|
|
// Determine the object class |
|
95
|
1 |
|
$objectClass = self::objectClassFromType($path->getType()); |
|
96
|
|
|
|
|
97
|
|
|
// Prepare the system properties collection |
|
98
|
1 |
|
$systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) || |
|
99
|
|
|
!is_array( |
|
100
|
1 |
|
$propertyData[SystemProperties::COLLECTION] |
|
101
|
1 |
|
)) ? [] : $propertyData[SystemProperties::COLLECTION]; |
|
102
|
1 |
|
$systemPropertyData[SystemProperties::PROPERTY_ID] = $path->getId()->getId(); |
|
103
|
1 |
|
$systemPropertyData[SystemProperties::PROPERTY_TYPE] = $path->getType()->getType(); |
|
104
|
1 |
|
$systemPropertyData[SystemProperties::PROPERTY_REVISION] = $path->getRevision()->getRevision(); |
|
105
|
1 |
|
$systemPropertyData[SystemProperties::PROPERTY_CREATED] = $path->getCreationDate()->format('U'); |
|
106
|
1 |
|
if (empty($systemPropertyData[SystemProperties::PROPERTY_LANGUAGE])) { |
|
107
|
|
|
$systemPropertyData[SystemProperties::PROPERTY_LANGUAGE] = getenv('OBJECT_DEFAULT_LANGUAGE'); |
|
108
|
|
|
} |
|
109
|
1 |
|
$propertyData[SystemProperties::COLLECTION] = $systemPropertyData; |
|
110
|
|
|
|
|
111
|
|
|
// Instantiate the object |
|
112
|
|
|
return Kernel::create($objectClass, [$path, $payload, $propertyData]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Determine and validate the object class name from its type |
|
117
|
|
|
* |
|
118
|
|
|
* @param Type $type Object type |
|
119
|
26 |
|
* @return string Object class name |
|
120
|
|
|
* @throws InvalidArgumentException If the object type is invalid |
|
121
|
|
|
*/ |
|
122
|
26 |
|
protected static function objectClassFromType(Type $type) |
|
123
|
26 |
|
{ |
|
124
|
26 |
|
// If the object type is invalid |
|
125
|
1 |
|
$objectType = $type->getType(); |
|
126
|
1 |
|
$objectClass = 'Apparat\\Object\\Application\\Model\\Object\\'.ucfirst($objectType); |
|
127
|
1 |
|
if (!Type::isValidType($objectType) || !class_exists($objectClass)) { |
|
128
|
|
|
throw new InvalidArgumentException( |
|
129
|
|
|
sprintf('Invalid object type "%s"', $objectType), |
|
130
|
|
|
InvalidArgumentException::INVALID_OBJECT_TYPE |
|
131
|
25 |
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $objectClass; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|