/** * Converts a method's name into a data field name. * * @param string $methodName * @return string|null */ public function getFieldNameForMethodName($methodName) { if (substr($methodName, 0, 2) === self::IS_METHOD_PREFIX) { return SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 2)); } elseif (substr($methodName, 0, 3) === self::HAS_METHOD_PREFIX) { return SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 3)); } elseif (substr($methodName, 0, 3) === self::GETTER_PREFIX) { return SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 3)); } return null; }
public function camelCaseToSnakeCase($data) { $result = \Magento\Framework\Api\SimpleDataObjectConverter::camelCaseToSnakeCase($data); return $result; }
/** * Check presence for both camelCase and snake_case keys in array and substitute if either is present * * @param array $requestData * @param string $key * @param string $value * @return void */ protected function substituteParameters(&$requestData, $key, $value) { $snakeCaseKey = SimpleDataObjectConverter::camelCaseToSnakeCase($key); $camelCaseKey = SimpleDataObjectConverter::snakeCaseToCamelCase($key); if (isset($requestData[$camelCaseKey])) { $requestData[$camelCaseKey] = $value; } else { $requestData[$snakeCaseKey] = $value; } }
/** * Converts associative array's key names from camelCase to snake_case, recursively. * * @param array $properties * @return array */ private function convertArrayToSnakeCase($properties) { foreach ($properties as $name => $value) { $snakeCaseName = SimpleDataObjectConverter::camelCaseToSnakeCase($name); if (is_array($value)) { $value = $this->convertArrayToSnakeCase($value); } unset($properties[$name]); $properties[$snakeCaseName] = $value; } return $properties; }
/** * Use class reflection on given data interface to build output data array * * @param mixed $dataObject * @param string $dataObjectType * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function buildOutputDataArray($dataObject, $dataObjectType) { $methods = $this->getMethodsMap($dataObjectType); $outputData = []; /** @var MethodReflection $method */ foreach ($methods as $methodName => $methodReflectionData) { // any method with parameter(s) gets ignored because we do not know the type and value of // the parameter(s), so we are not able to process if ($methodReflectionData['parameterCount'] > 0) { continue; } $returnType = $methodReflectionData['type']; if (substr($methodName, 0, 2) === self::IS_METHOD_PREFIX) { $value = $dataObject->{$methodName}(); if ($value === null && !$methodReflectionData['isRequired']) { continue; } $key = SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 2)); $outputData[$key] = $this->castValueToType($value, $returnType); } elseif (substr($methodName, 0, 3) === self::HAS_METHOD_PREFIX) { $value = $dataObject->{$methodName}(); if ($value === null && !$methodReflectionData['isRequired']) { continue; } $key = SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 3)); $outputData[$key] = $this->castValueToType($value, $returnType); } elseif (substr($methodName, 0, 3) === self::GETTER_PREFIX) { $value = $dataObject->{$methodName}(); if ($methodName === 'getCustomAttributes' && $value === []) { continue; } if ($value === null && !$methodReflectionData['isRequired']) { continue; } $key = SimpleDataObjectConverter::camelCaseToSnakeCase(substr($methodName, 3)); if ($key === CustomAttributesDataInterface::CUSTOM_ATTRIBUTES) { $value = $this->convertCustomAttributes($value, $dataObjectType); } elseif (is_object($value) && !$value instanceof Phrase) { $value = $this->buildOutputDataArray($value, $returnType); } elseif (is_array($value)) { $valueResult = []; $arrayElementType = substr($returnType, 0, -2); foreach ($value as $singleValue) { if (is_object($singleValue) && !$singleValue instanceof Phrase) { $singleValue = $this->buildOutputDataArray($singleValue, $arrayElementType); } $valueResult[] = $this->castValueToType($singleValue, $arrayElementType); } $value = $valueResult; } $outputData[$key] = $this->castValueToType($value, $returnType); } } return $outputData; }