Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

UserUpdate.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Server\Input\Parser;
8
9 use eZ\Publish\Core\REST\Common\Input\BaseParser;
10 use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
11 use eZ\Publish\Core\REST\Common\Input\FieldTypeParser;
12 use eZ\Publish\Core\REST\Common\Input\ParserTools;
13 use eZ\Publish\Core\REST\Common\Exceptions;
14 use eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct;
15 use eZ\Publish\API\Repository\UserService;
16 use eZ\Publish\API\Repository\ContentService;
17
21 class UserUpdate extends BaseParser
22 {
28 protected $userService;
29
35 protected $contentService;
36
42 protected $fieldTypeParser;
43
49 protected $parserTools;
50
59 public function __construct(UserService $userService, ContentService $contentService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools)
60 {
61 $this->userService = $userService;
62 $this->contentService = $contentService;
63 $this->fieldTypeParser = $fieldTypeParser;
64 $this->parserTools = $parserTools;
65 }
66
75 public function parse(array $data, ParsingDispatcher $parsingDispatcher)
76 {
77 $parsedData = [];
78
79 //@todo XSD has a login element, but it's not possible to update login
80
81 if (array_key_exists('email', $data)) {
82 $parsedData['email'] = $data['email'];
83 }
84
85 if (array_key_exists('password', $data)) {
86 $parsedData['password'] = $data['password'];
87 }
88
89 if (array_key_exists('enabled', $data)) {
90 $parsedData['enabled'] = $this->parserTools->parseBooleanValue($data['enabled']);
91 }
92
93 if (array_key_exists('mainLanguageCode', $data)) {
94 $parsedData['mainLanguageCode'] = $data['mainLanguageCode'];
95 }
96
97 if (array_key_exists('Section', $data) && is_array($data['Section'])) {
98 if (!array_key_exists('_href', $data['Section'])) {
99 throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserUpdate.");
100 }
101
102 $parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId');
103 }
104
105 if (array_key_exists('remoteId', $data)) {
106 $parsedData['remoteId'] = $data['remoteId'];
107 }
108
109 if (array_key_exists('fields', $data)) {
110 $userId = $this->requestParser->parseHref($data['__url'], 'userId');
111
112 if (!is_array($data['fields']) || !array_key_exists('field', $data['fields']) || !is_array($data['fields']['field'])) {
113 throw new Exceptions\Parser("Invalid 'fields' element for UserUpdate.");
114 }
115
116 $parsedData['fields'] = [];
117 foreach ($data['fields']['field'] as $fieldData) {
118 if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) {
119 throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserUpdate.");
120 }
121
122 if (!array_key_exists('fieldValue', $fieldData)) {
123 throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserUpdate.");
124 }
125
126 $fieldValue = $this->fieldTypeParser->parseFieldValue($userId, $fieldData['fieldDefinitionIdentifier'], $fieldData['fieldValue']);
127
128 $languageCode = null;
129 if (array_key_exists('languageCode', $fieldData)) {
130 $languageCode = $fieldData['languageCode'];
131 }
132
133 $parsedData['fields'][$fieldData['fieldDefinitionIdentifier']] = [
134 'fieldValue' => $fieldValue,
135 'languageCode' => $languageCode,
136 ];
137 }
138 }
139
140 $userUpdateStruct = $this->userService->newUserUpdateStruct();
141
142 if (!empty($parsedData)) {
143 if (array_key_exists('email', $parsedData)) {
144 $userUpdateStruct->email = $parsedData['email'];
145 }
146
147 if (array_key_exists('password', $parsedData)) {
148 $userUpdateStruct->password = $parsedData['password'];
149 }
150
151 if (array_key_exists('enabled', $parsedData)) {
152 $userUpdateStruct->enabled = $parsedData['enabled'];
153 }
154
155 if (array_key_exists('mainLanguageCode', $parsedData) || array_key_exists('remoteId', $parsedData)) {
156 $userUpdateStruct->contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
157
158 if (array_key_exists('mainLanguageCode', $parsedData)) {
159 $userUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = $parsedData['mainLanguageCode'];
160 }
161
162 if (array_key_exists('remoteId', $parsedData)) {
163 $userUpdateStruct->contentMetadataUpdateStruct->remoteId = $parsedData['remoteId'];
164 }
165 }
166
167 if (array_key_exists('fields', $parsedData)) {
168 $userUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct();
169
170 foreach ($parsedData['fields'] as $fieldDefinitionIdentifier => $fieldValue) {
171 $userUpdateStruct->contentUpdateStruct->setField(
172 $fieldDefinitionIdentifier,
173 $fieldValue['fieldValue'],
174 $fieldValue['languageCode']
175 );
176 }
177 }
178 }
179
180 return new RestUserUpdateStruct(
181 $userUpdateStruct,
182 array_key_exists('sectionId', $parsedData) ? $parsedData['sectionId'] : null
183 );
184 }
185 }
eZ\Publish\Core\REST\Common\Input\BaseParser
Definition: BaseParser.php:12
eZ\Publish\Core\REST\Common\Input\FieldTypeParser
Definition: FieldTypeParser.php:15
eZ\Publish\Core\REST\Common\Input\ParserTools
Tools object to be used in Input Parsers.
Definition: ParserTools.php:17
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher
Parsing dispatcher.
Definition: ParsingDispatcher.php:15
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate
Parser for UserUpdate.
Definition: UserUpdate.php:22
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\$fieldTypeParser
$fieldTypeParser
Definition: UserUpdate.php:42
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\$userService
$userService
Definition: UserUpdate.php:28
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\$parserTools
$parserTools
Definition: UserUpdate.php:49
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\__construct
__construct(UserService $userService, ContentService $contentService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools)
Construct.
Definition: UserUpdate.php:59
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\parse
parse(array $data, ParsingDispatcher $parsingDispatcher)
Parse input structure.
Definition: UserUpdate.php:75
eZ\Publish\Core\REST\Server\Input\Parser\UserUpdate\$contentService
$contentService
Definition: UserUpdate.php:35
eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct
RestUserUpdateStruct view model.
Definition: RestUserUpdateStruct.php:16
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\UserService
Definition: API/Repository/UserService.php:26
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Server\Input\Parser
Definition: ContentCreate.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.