1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jensk |
4
|
|
|
* Date: 13-3-2017 |
5
|
|
|
* Time: 16:24 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace library\storage; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class DocumentFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param array $postValues |
15
|
|
|
* @param JsonStorage $jsonStorage |
16
|
|
|
* |
17
|
|
|
* @return \library\storage\Document |
18
|
|
|
*/ |
19
|
|
|
public static function createDocumentFromPostValues($postValues, $jsonStorage) |
20
|
|
|
{ |
21
|
|
|
$postValues = utf8Convert($postValues); |
22
|
|
|
$documentType = $jsonStorage->getDocumentTypeBySlug($postValues['documentType']); |
23
|
|
|
|
24
|
|
|
$staticBricks = $documentType->bricks; |
25
|
|
|
|
26
|
|
|
$documentObj = self::createInitialDocumentObject($postValues, $documentType); |
27
|
|
|
|
28
|
|
|
$documentObj->fields = isset($postValues['fields']) ? $postValues['fields'] : array(); |
29
|
|
|
$documentObj->bricks = array(); |
30
|
|
|
|
31
|
|
|
$documentObj = self::createBrickArrayForDocument($postValues, $documentObj, $staticBricks); |
32
|
|
|
$documentObj = self::createDynamicBrickArrayForDocument($postValues, $documentObj); |
33
|
|
|
|
34
|
|
|
return $documentObj; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $postValues |
39
|
|
|
* @param \stdClass $documentType |
40
|
|
|
* |
41
|
|
|
* @return Document |
42
|
|
|
*/ |
43
|
|
|
private static function createInitialDocumentObject($postValues, $documentType) |
44
|
|
|
{ |
45
|
|
|
$documentObj = new Document(); |
46
|
|
|
$documentObj->title = $postValues['title']; |
47
|
|
|
$documentObj->slug = slugify($postValues['title']); |
48
|
|
|
$documentObj->type = 'document'; |
49
|
|
|
$documentObj->documentType = $documentType->title; |
50
|
|
|
$documentObj->documentTypeSlug = $documentType->slug; |
51
|
|
|
$documentObj->state = isset($postValues['state']) ? 'published' : 'unpublished'; |
52
|
|
|
$documentObj->lastModificationDate = time(); |
53
|
|
|
$documentObj->creationDate = isset($postValues['creationDate']) ? intval($postValues['creationDate']) : time(); |
54
|
|
|
$documentObj->lastModifiedBy = $_SESSION['cloudcontrol']->username; |
55
|
|
|
|
56
|
|
|
return $documentObj; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $postValues |
61
|
|
|
* @param Document $documentObj |
62
|
|
|
* @param array $staticBricks |
63
|
|
|
* |
64
|
|
|
* @return Document |
65
|
|
|
*/ |
66
|
|
|
private static function createBrickArrayForDocument($postValues, $documentObj, $staticBricks) |
67
|
|
|
{ |
68
|
|
|
if (isset($postValues['bricks'])) { |
69
|
|
|
foreach ($postValues['bricks'] as $brickSlug => $brick) { |
70
|
|
|
// Check if its multiple |
71
|
|
|
$multiple = false; |
72
|
|
|
$staticBrick = null; |
73
|
|
|
foreach ($staticBricks as $staticBrick) { |
74
|
|
|
if ($staticBrick->slug === $brickSlug) { |
75
|
|
|
$multiple = $staticBrick->multiple; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($multiple) { |
81
|
|
|
$brickArray = array(); |
82
|
|
View Code Duplication |
foreach ($brick as $brickInstance) { |
|
|
|
|
83
|
|
|
$brickObj = new \stdClass(); |
84
|
|
|
$brickObj->fields = new \stdClass(); |
85
|
|
|
$brickObj->type = $staticBrick->brickSlug; |
86
|
|
|
|
87
|
|
|
foreach ($brickInstance['fields'] as $fieldName => $fieldValues) { |
88
|
|
|
$brickObj->fields->$fieldName = $fieldValues; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$brickArray[] = $brickObj; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$bricks = $documentObj->bricks; |
95
|
|
|
$bricks[$brickSlug] = $brickArray; |
96
|
|
|
$documentObj->bricks = $bricks; |
97
|
|
|
} else { |
98
|
|
|
$bricks = $documentObj->bricks; |
99
|
|
|
$bricks[$brickSlug] = $brick; |
100
|
|
|
$documentObj->bricks = $bricks; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $documentObj; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $postValues |
109
|
|
|
* @param Document $documentObj |
110
|
|
|
* |
111
|
|
|
* @return Document |
112
|
|
|
*/ |
113
|
|
|
private static function createDynamicBrickArrayForDocument($postValues, $documentObj) |
114
|
|
|
{ |
115
|
|
|
$documentObj->dynamicBricks = array(); |
116
|
|
|
if (isset($postValues['dynamicBricks'])) { |
117
|
|
View Code Duplication |
foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) { |
|
|
|
|
118
|
|
|
foreach ($brick as $brickContent) { |
119
|
|
|
$brickObj = new \stdClass(); |
120
|
|
|
$brickObj->type = $brickTypeSlug; |
121
|
|
|
$brickObj->fields = $brickContent; |
122
|
|
|
$dynamicBricks = $documentObj->dynamicBricks; |
123
|
|
|
$dynamicBricks[] = $brickObj; |
124
|
|
|
$documentObj->dynamicBricks = $dynamicBricks; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $documentObj; |
129
|
|
|
} |
130
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.