8 declare(strict_types=1);
23 use Zend\Code\Reflection\ClassReflection;
69 private $serviceTypeToEntityTypeMap;
101 $this->serviceTypeToEntityTypeMap = $serviceTypeToEntityTypeMap
103 $this->config = $config
114 private function getNameFinder()
116 if ($this->nameFinder ===
null) {
118 ->get(\
Magento\Framework\Reflection\NameFinder::class);
120 return $this->nameFinder;
139 public function process($serviceClassName, $serviceMethodName, array $inputArray)
143 foreach ($this->methodsMap->getMethodParams($serviceClassName, $serviceMethodName) as $param) {
145 $snakeCaseParamName = strtolower(preg_replace(
"/(?<=\\w)(?=[A-Z])/",
"_$1", $paramName));
146 if (isset($inputArray[$paramName]) || isset($inputArray[$snakeCaseParamName])) {
147 $paramValue = isset($inputArray[$paramName])
148 ? $inputArray[$paramName]
149 : $inputArray[$snakeCaseParamName];
154 throw new WebapiException(
new Phrase($e->getMessage()));
160 $inputError[] = $paramName;
174 private function getConstructorData(
string $className, array
$data): array
176 $preferenceClass = $this->config->getPreference(
$className);
179 $constructor =
$class->getConstructor();
180 if ($constructor ===
null) {
185 $parameters = $constructor->getParameters();
186 foreach ($parameters as $parameter) {
187 if (isset(
$data[$parameter->getName()])) {
188 $res[$parameter->getName()] =
$data[$parameter->getName()];
219 $object = $this->objectManager->create(
$className, $constructorArgs);
223 if (isset($constructorArgs[$propertyName])) {
230 $methodName = $this->getNameFinder()->getGetterMethodName(
$class, $camelCaseProperty);
231 $methodReflection =
$class->getMethod($methodName);
232 if ($methodReflection->isPublic()) {
233 $returnType = $this->typeProcessor->getGetterReturnType($methodReflection)[
'type'];
235 $setterName = $this->getNameFinder()->getSetterMethodName(
$class, $camelCaseProperty);
244 if ($camelCaseProperty ===
'CustomAttributes') {
252 'Error occurred during "%field_name" processing. %details',
253 [
'field_name' => $propertyName,
'details' => $e->getMessage()]
257 $object->{$setterName}($setterValue);
274 $dataObjectClassName = ltrim($dataObjectClassName,
'\\');
276 foreach ($customAttributesValueArray as $key => $customAttribute) {
277 if (!is_array($customAttribute)) {
281 list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);
283 $entityType = $this->serviceTypeToEntityTypeMap->getEntityType($dataObjectClassName);
285 $type = $this->customAttributeTypeLocator->getType(
286 $customAttributeCode,
293 if ($this->typeProcessor->isTypeAny(
$type) || $this->typeProcessor->isTypeSimple(
$type)
294 || !is_array($customAttributeValue)
297 $attributeValue = $this->convertValue($customAttributeValue,
$type);
301 'Attribute "%attribute_code" has invalid value. %details',
302 [
'attribute_code' => $customAttributeCode,
'details' => $e->getMessage()]
307 $attributeValue = $this->_createDataObjectForTypeAndArrayValue(
$type, $customAttributeValue);
311 $result[$customAttributeCode] = $this->attributeValueFactory->create()
312 ->setAttributeCode($customAttributeCode)
313 ->setValue($attributeValue);
325 private function processCustomAttribute($customAttribute)
327 $camelCaseAttributeCodeKey = lcfirst(
333 }
elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
334 $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
336 $customAttributeCode =
null;
340 throw new SerializationException(
341 new Phrase(
'An empty custom attribute is specified. Enter the attribute and try again.')
343 }
elseif (!$customAttributeCode) {
344 throw new SerializationException(
346 'A custom attribute is specified with a missing attribute code. Verify the code and try again.' 350 throw new SerializationException(
352 'The "' . $customAttributeCode .
353 '" attribute code doesn\'t have a value set. Enter the value and try again.' 370 if (substr(
$type, -2) ===
"[]") {
372 $attributeValue = [];
373 foreach ($customAttributeValue as
$value) {
374 $attributeValue[] = $this->_createFromArray(
$type,
$value);
377 $attributeValue = $this->_createFromArray(
$type, $customAttributeValue);
380 return $attributeValue;
393 $isArrayType = $this->typeProcessor->isArrayType(
$type);
394 if ($isArrayType && isset(
$data[
'item'])) {
397 if ($this->typeProcessor->isTypeSimple(
$type) || $this->typeProcessor->isTypeAny(
$type)) {
404 $itemType = $this->typeProcessor->getArrayItemType(
$type);
405 if (is_array(
$data)) {
407 $result[$key] = $this->_createFromArray($itemType,
$item);
426 if (isset(
$value[
'item'])) {
427 if (is_array(
$value[
'item'])) {
433 throw new \InvalidArgumentException(
'Value must be an array and must contain "item" field.');
440 $isAssociative = array_keys(
$value) !== range(0, count(
$value) - 1);
453 if (!empty($inputError)) {
455 foreach ($inputError as $errorParamField) {
456 $exception->addError(
457 new Phrase(
'"%fieldName" is required. Enter and try again.', [
'fieldName' => $errorParamField])
460 if ($exception->wasErrorAdded()) {
const METHOD_META_DEFAULT_VALUE
is_subclass_of($obj, $className)
static snakeCaseToUpperCamelCase($input)
elseif(isset( $params[ 'redirect_parent']))
$_option $_optionId $class
const METHOD_META_HAS_DEFAULT_VALUE