Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExtensionAttributesProcessor.php
Go to the documentation of this file.
1 <?php
8 
14 use Zend\Code\Reflection\MethodReflection;
15 
22 {
26  private $dataObjectProcessor;
27 
31  private $methodsMapProcessor;
32 
36  private $authorization;
37 
41  private $config;
42 
46  private $isPermissionChecked;
47 
51  private $fieldNamer;
52 
56  private $typeCaster;
57 
67  public function __construct(
68  DataObjectProcessor $dataObjectProcessor,
69  MethodsMap $methodsMapProcessor,
70  TypeCaster $typeCaster,
71  FieldNamer $fieldNamer,
72  AuthorizationInterface $authorization,
73  Config $config,
74  $isPermissionChecked = false
75  ) {
76  $this->dataObjectProcessor = $dataObjectProcessor;
77  $this->methodsMapProcessor = $methodsMapProcessor;
78  $this->typeCaster = $typeCaster;
79  $this->fieldNamer = $fieldNamer;
80  $this->authorization = $authorization;
81  $this->config = $config;
82  $this->isPermissionChecked = $isPermissionChecked;
83  }
84 
93  public function buildOutputDataArray(ExtensionAttributesInterface $dataObject, $dataObjectType)
94  {
95  $methods = $this->methodsMapProcessor->getMethodsMap($dataObjectType);
96  $outputData = [];
97 
99  foreach (array_keys($methods) as $methodName) {
100  if (!$this->methodsMapProcessor->isMethodValidForDataField($dataObjectType, $methodName)) {
101  continue;
102  }
103 
104  $key = $this->fieldNamer->getFieldNameForMethodName($methodName);
105  if ($this->isPermissionChecked && !$this->isAttributePermissionValid($dataObjectType, $key)) {
106  continue;
107  }
108 
109  $value = $dataObject->{$methodName}();
110  if ($value === null) {
111  // all extension attributes are optional so don't need to check if isRequired
112  continue;
113  }
114 
115  $returnType = $this->methodsMapProcessor->getMethodReturnType($dataObjectType, $methodName);
116 
117  if (is_object($value) && !($value instanceof Phrase)) {
118  $value = $this->dataObjectProcessor->buildOutputDataArray($value, $returnType);
119  } elseif (is_array($value)) {
120  $valueResult = [];
121  $arrayElementType = substr($returnType, 0, -2);
122  foreach ($value as $singleValue) {
123  if (is_object($singleValue) && !($singleValue instanceof Phrase)) {
124  $singleValue = $this->dataObjectProcessor->buildOutputDataArray(
125  $singleValue,
126  $arrayElementType
127  );
128  }
129  $valueResult[] = $this->typeCaster->castValueToType($singleValue, $arrayElementType);
130  }
131  $value = $valueResult;
132  } else {
133  $value = $this->typeCaster->castValueToType($value, $returnType);
134  }
135 
136  $outputData[$key] = $value;
137  }
138 
139  return $outputData;
140  }
141 
147  private function isAttributePermissionValid($dataObjectType, $attributeCode)
148  {
149  $typeName = $this->getRegularTypeForExtensionAttributesType($dataObjectType);
150  $permissions = $this->getPermissionsForTypeAndMethod($typeName, $attributeCode);
151  foreach ($permissions as $permission) {
152  if (!$this->authorization->isAllowed($permission)) {
153  return false;
154  }
155  }
156 
157  return true;
158  }
159 
164  private function getRegularTypeForExtensionAttributesType($name)
165  {
166  return ltrim(str_replace('ExtensionInterface', 'Interface', $name), '\\');
167  }
168 
174  private function getPermissionsForTypeAndMethod($typeName, $attributeCode)
175  {
176  $attributes = $this->config->get();
177  if (isset($attributes[$typeName]) && isset($attributes[$typeName][$attributeCode])) {
178  $attributeMetadata = $attributes[$typeName][$attributeCode];
179  $permissions = [];
180  foreach ($attributeMetadata[Converter::RESOURCE_PERMISSIONS] as $permission) {
181  $permissions[] = $permission;
182  }
183  return $permissions;
184  }
185 
186  return [];
187  }
188 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
__construct(DataObjectProcessor $dataObjectProcessor, MethodsMap $methodsMapProcessor, TypeCaster $typeCaster, FieldNamer $fieldNamer, AuthorizationInterface $authorization, Config $config, $isPermissionChecked=false)
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
$permissions
if(!isset($_GET['name'])) $name
Definition: log.php:14