1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Transfer\EzPlatform\Repository\Values; |
11
|
|
|
|
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
14
|
|
|
use Transfer\EzPlatform\Exception\InvalidDataStructureException; |
15
|
|
|
use Transfer\EzPlatform\Repository\Values\Mapper\ContentMapper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Content object. |
19
|
|
|
* |
20
|
|
|
* @see http://transfer-framework.com/docs/1.0/sources_and_targets/ezplatform/the_objects/contentobject.html |
21
|
|
|
*/ |
22
|
|
|
class ContentObject extends EzPlatformObject |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContentMapper |
26
|
|
|
*/ |
27
|
|
|
private $mapper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ContentMapper |
31
|
23 |
|
*/ |
32
|
|
|
public function getMapper() |
33
|
23 |
|
{ |
34
|
23 |
|
if (!$this->mapper) { |
35
|
23 |
|
$this->mapper = new ContentMapper($this); |
36
|
|
|
} |
37
|
23 |
|
|
38
|
1 |
|
return $this->mapper; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Allows direct control in ContentCreateStruct and ContentUpdateStruct. |
43
|
|
|
* |
44
|
|
|
* @param \Closure $callback |
45
|
2 |
|
*/ |
46
|
1 |
|
public function setStructCallback(\Closure $callback) |
47
|
2 |
|
{ |
48
|
2 |
|
$this->setProperty('struct_callback', $callback); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructs content object. |
53
|
|
|
* |
54
|
|
|
* @param array|Content $data Field data |
55
|
|
|
* @param array $properties Additional properties |
56
|
25 |
|
*/ |
57
|
|
|
public function __construct($data, array $properties = array()) |
58
|
25 |
|
{ |
59
|
1 |
|
if ($data instanceof Content) { |
60
|
1 |
|
$this->getMapper()->contentToObject($data); |
61
|
25 |
|
} else { |
62
|
|
|
parent::__construct($data, array_merge( |
63
|
25 |
|
array( |
64
|
25 |
|
'main_object' => true, |
65
|
25 |
|
'parent_locations' => [], |
66
|
|
|
), |
67
|
25 |
|
$properties |
68
|
|
|
)); |
69
|
|
|
} |
70
|
25 |
|
|
71
|
3 |
|
if (isset($properties['parent_locations'])) { |
72
|
3 |
|
$this->setParentLocations($properties['parent_locations']); |
73
|
25 |
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Values in array must be of type Location, LocationObject or int. |
78
|
|
|
* |
79
|
|
|
* @param array $parentLocations |
80
|
6 |
|
*/ |
81
|
|
|
public function setParentLocations(array $parentLocations) |
82
|
6 |
|
{ |
83
|
6 |
|
$this->properties['parent_locations'] = []; |
84
|
6 |
|
foreach ($parentLocations as $location) { |
85
|
6 |
|
$this->addParentLocation($location); |
86
|
6 |
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Convert parameters to LocationCreateStruct and stores it on the ContentObject. |
91
|
|
|
* |
92
|
|
|
* @param Location|LocationObject|int $parentLocation |
93
|
|
|
* |
94
|
|
|
* @throws InvalidDataStructureException |
95
|
10 |
|
*/ |
96
|
|
|
public function addParentLocation($parentLocation) |
97
|
10 |
|
{ |
98
|
|
|
$locationObject = $this->convertToLocationObject($parentLocation); |
99
|
10 |
|
|
100
|
1 |
|
if (!isset($locationObject->data['parent_location_id']) || (int) $locationObject->data['parent_location_id'] < 1) { |
101
|
|
|
throw new InvalidDataStructureException('Parent location id must be an integer of 2 or above.'); |
102
|
|
|
} |
103
|
9 |
|
|
104
|
9 |
|
if (!isset($locationObject->data['content_id'])) { |
105
|
2 |
|
if ($this->getProperty('id')) { |
106
|
2 |
|
$locationObject->data['content_id'] = $this->getProperty('id'); |
107
|
9 |
|
} |
108
|
|
|
} |
109
|
9 |
|
|
110
|
9 |
|
$this->properties['parent_locations'][$locationObject->data['parent_location_id']] = $locationObject; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int|Location|LocationObject $parentLocation |
115
|
|
|
* |
116
|
|
|
* @return LocationObject |
117
|
10 |
|
*/ |
118
|
|
|
private function convertToLocationObject($parentLocation) |
119
|
10 |
|
{ |
120
|
|
|
$locationObject = new LocationObject(array()); |
121
|
10 |
|
|
122
|
10 |
|
switch (true) { |
123
|
1 |
|
case $parentLocation instanceof Location: |
124
|
1 |
|
$locationObject->getMapper()->locationToObject($parentLocation); |
125
|
10 |
|
break; |
126
|
8 |
|
case is_int($parentLocation): |
127
|
8 |
|
$locationObject->data['parent_location_id'] = $parentLocation; |
128
|
8 |
|
break; |
129
|
8 |
|
case $parentLocation instanceof LocationObject: |
130
|
8 |
|
default: |
131
|
8 |
|
$locationObject = $parentLocation; |
132
|
|
|
} |
133
|
10 |
|
|
134
|
|
|
return $locationObject; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|