Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
ParamsOverrider Class Reference

Public Member Functions

 __construct (array $paramOverriders=[])
 
 override (array $inputData, array $parameters)
 
 overrideRequestBodyIdWithPathParam (array $urlPathParams, array $requestBodyParams, $serviceClassName, $serviceMethodName)
 

Protected Member Functions

 isNestedArrayValueSet (&$nestedArray, $arrayKeys)
 
 setNestedArrayValue (&$nestedArray, $arrayKeys, $valueToSet)
 

Detailed Description

Override parameter values

Definition at line 17 of file ParamsOverrider.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $paramOverriders = [])

Initialize dependencies

Parameters
ParamOverriderInterface[]$paramOverriders

Definition at line 34 of file ParamsOverrider.php.

36  {
37  $this->paramOverriders = $paramOverriders;
38  }

Member Function Documentation

◆ isNestedArrayValueSet()

isNestedArrayValueSet ( $nestedArray,
  $arrayKeys 
)
protected

Determine if a nested array value is set.

Parameters
array&$nestedArray
string[]$arrayKeys
Returns
bool true if array value is set

Definition at line 71 of file ParamsOverrider.php.

72  {
73  $currentArray = &$nestedArray;
74 
75  foreach ($arrayKeys as $key) {
76  if (!isset($currentArray[$key])) {
77  return false;
78  }
79  $currentArray = &$currentArray[$key];
80  }
81  return true;
82  }

◆ override()

override ( array  $inputData,
array  $parameters 
)

Override parameter values based on webapi.xml

Parameters
array$inputDataIncoming data from request
array$parametersContains parameters to replace or default
Returns
array Data in same format as $inputData with appropriate parameters added or changed

Definition at line 47 of file ParamsOverrider.php.

48  {
49  foreach ($parameters as $name => $paramData) {
50  $arrayKeys = explode('.', $name);
51  if ($paramData[Converter::KEY_FORCE] || !$this->isNestedArrayValueSet($inputData, $arrayKeys)) {
52  $paramValue = $paramData[Converter::KEY_VALUE];
53  if (isset($this->paramOverriders[$paramValue])) {
54  $value = $this->paramOverriders[$paramValue]->getOverriddenValue();
55  } else {
56  $value = $paramData[Converter::KEY_VALUE];
57  }
58  $this->setNestedArrayValue($inputData, $arrayKeys, $value);
59  }
60  }
61  return $inputData;
62  }
setNestedArrayValue(&$nestedArray, $arrayKeys, $valueToSet)
$value
Definition: gender.phtml:16
isNestedArrayValueSet(&$nestedArray, $arrayKeys)
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ overrideRequestBodyIdWithPathParam()

overrideRequestBodyIdWithPathParam ( array  $urlPathParams,
array  $requestBodyParams,
  $serviceClassName,
  $serviceMethodName 
)

Override request body property value with matching url path parameter value

This method assumes that webapi.xml url defines the substitution parameter as camelCase to the actual snake case key described as part of the api contract. example: /:parentId/nestedResource/:entityId. Here :entityId value will be used for overriding 'entity_id' property in the body. Since Webapi framework allows both camelCase and snakeCase, either of them will be substituted for now. If the request body is missing url path parameter as property, it will be added to the body. This method works only requests with scalar properties at top level or properties of single object embedded in the request body. Only the last path parameter value will be substituted from the url in case of multiple parameters.

Parameters
array$urlPathParamsurl path parameters as array
array$requestBodyParamsbody parameters as array
string$serviceClassNamename of the service class that we are trying to call
string$serviceMethodNamename of the method that we are trying to call
Returns
array

Definition at line 125 of file ParamsOverrider.php.

130  {
131  if (empty($urlPathParams)) {
132  return $requestBodyParams;
133  }
134  $pathParamValue = end($urlPathParams);
135  // Self apis should not be overridden
136  if ($pathParamValue === 'me') {
137  return $requestBodyParams;
138  }
139  $pathParamKey = key($urlPathParams);
140  // Check if the request data is a top level object of body
141  if (count($requestBodyParams) == 1 && is_array(end($requestBodyParams))) {
142  $requestDataKey = key($requestBodyParams);
143  if ($this->isPropertyDeclaredInDataObject(
144  $serviceClassName,
145  $serviceMethodName,
146  $requestDataKey,
147  $pathParamKey
148  )
149  ) {
150  $this->substituteParameters($requestBodyParams[$requestDataKey], $pathParamKey, $pathParamValue);
151  } else {
152  $this->substituteParameters($requestBodyParams, $pathParamKey, $pathParamValue);
153  }
154  } else { // Else parameters passed as scalar values in body will be overridden
155  $this->substituteParameters($requestBodyParams, $pathParamKey, $pathParamValue);
156  }
157  return $requestBodyParams;
158  }

◆ setNestedArrayValue()

setNestedArrayValue ( $nestedArray,
  $arrayKeys,
  $valueToSet 
)
protected

Set a nested array value.

Parameters
array&$nestedArray
string[]$arrayKeys
string$valueToSet
Returns
void

Definition at line 92 of file ParamsOverrider.php.

93  {
94  $currentArray = &$nestedArray;
95  $lastKey = array_pop($arrayKeys);
96 
97  foreach ($arrayKeys as $key) {
98  if (!isset($currentArray[$key])) {
99  $currentArray[$key] = [];
100  }
101  $currentArray = &$currentArray[$key];
102  }
103 
104  $currentArray[$lastKey] = $valueToSet;
105  }

The documentation for this class was generated from the following file: