1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the ContentUpdate parser class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Input\Parser; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
12
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
13
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Values\RestContentMetadataUpdateStruct; |
15
|
|
|
use DateTime; |
16
|
|
|
use Exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Parser for ContentUpdate. |
20
|
|
|
*/ |
21
|
|
|
class ContentUpdate extends BaseParser |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Parse input structure. |
25
|
|
|
* |
26
|
|
|
* @todo use url handler instead of hardcoded URL matching |
27
|
|
|
* |
28
|
|
|
* @param array $data |
29
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
30
|
|
|
* |
31
|
|
|
* @return \eZ\Publish\Core\REST\Common\Values\RestContentMetadataUpdateStruct |
32
|
|
|
* |
33
|
|
|
* @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser if $data is invalid |
34
|
|
|
*/ |
35
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
36
|
|
|
{ |
37
|
|
|
$parsedData = []; |
38
|
|
|
|
39
|
|
|
if (array_key_exists('Section', $data) && is_array($data['Section']) && isset($data['Section']['_href'])) { |
40
|
|
|
try { |
41
|
|
|
$parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId'); |
42
|
|
|
} catch (Exceptions\InvalidArgumentException $e) { |
43
|
|
|
throw new Exceptions\Parser('Invalid format for <Section> reference in <ContentUpdate>.'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (array_key_exists('Owner', $data) && is_array($data['Owner']) && isset($data['Owner']['_href'])) { |
48
|
|
|
try { |
49
|
|
|
$parsedData['ownerId'] = $this->requestParser->parseHref($data['Owner']['_href'], 'userId'); |
50
|
|
|
} catch (Exceptions\InvalidArgumentException $e) { |
51
|
|
|
throw new Exceptions\Parser('Invalid format for <Owner> reference in <ContentUpdate>.'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (array_key_exists('mainLanguageCode', $data)) { |
56
|
|
|
$parsedData['mainLanguageCode'] = $data['mainLanguageCode']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (array_key_exists('MainLocation', $data)) { |
60
|
|
|
try { |
61
|
|
|
$mainLocationIdParts = explode('/', $this->requestParser->parseHref($data['MainLocation']['_href'], 'locationPath')); |
62
|
|
|
$parsedData['mainLocationId'] = array_pop($mainLocationIdParts); |
63
|
|
|
} catch (Exceptions\InvalidArgumentException $e) { |
64
|
|
|
throw new Exceptions\Parser('Invalid format for <MainLocation> reference in <ContentUpdate>.'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (array_key_exists('alwaysAvailable', $data)) { |
69
|
|
|
if ($data['alwaysAvailable'] === 'true') { |
70
|
|
|
$parsedData['alwaysAvailable'] = true; |
71
|
|
|
} elseif ($data['alwaysAvailable'] === 'false') { |
72
|
|
|
$parsedData['alwaysAvailable'] = false; |
73
|
|
|
} else { |
74
|
|
|
throw new Exceptions\Parser('Invalid format for <alwaysAvailable> in <ContentUpdate>.'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// remoteId |
79
|
|
|
if (array_key_exists('remoteId', $data)) { |
80
|
|
|
$parsedData['remoteId'] = $data['remoteId']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// modificationDate |
84
|
|
View Code Duplication |
if (array_key_exists('modificationDate', $data)) { |
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$parsedData['modificationDate'] = new DateTime($data['modificationDate']); |
87
|
|
|
} catch (Exception $e) { |
88
|
|
|
throw new Exceptions\Parser('Invalid format for <modificationDate> in <ContentUpdate>', 0, $e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// publishDate |
93
|
|
View Code Duplication |
if (array_key_exists('publishDate', $data)) { |
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$parsedData['publishedDate'] = new DateTime($data['publishDate']); |
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
throw new Exceptions\Parser('Invalid format for <publishDate> in <ContentUpdate>', 0, $e); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return new RestContentMetadataUpdateStruct($parsedData); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.