Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MessageValidator.php
Go to the documentation of this file.
1 <?php
7 
8 use Doctrine\Instantiator\Exception\InvalidArgumentException;
11 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
12 
18 {
22  private $communicationConfig;
23 
32  protected function getTopicSchema($topic, $requestType)
33  {
34  $topicConfig = $this->getCommunicationConfig()->getTopic($topic);
35  if ($topicConfig === null) {
36  throw new LocalizedException(new Phrase('Specified topic "%topic" is not declared.', ['topic' => $topic]));
37  }
38  if ($requestType) {
39  return [
40  'schema_type' => $topicConfig[CommunicationConfig::TOPIC_REQUEST_TYPE],
41  'schema_value' => $topicConfig[CommunicationConfig::TOPIC_REQUEST]
42  ];
43  } else {
44  return [
45  'schema_type' => isset($topicConfig[CommunicationConfig::TOPIC_RESPONSE])
46  ? CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS
47  : null,
48  'schema_value' => $topicConfig[CommunicationConfig::TOPIC_RESPONSE]
49  ];
50  }
51  }
52 
62  public function validate($topic, $message, $requestType = true)
63  {
64  $topicSchema = $this->getTopicSchema($topic, $requestType);
65  if ($topicSchema['schema_type'] == CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS) {
66  $messageDataType = $topicSchema['schema_value'];
67  $this->validateMessage($message, $messageDataType, $topic);
68  } else {
70  $message = (array)$message;
71  $isIndexedArray = array_keys($message) === range(0, count($message) - 1);
72  foreach ($topicSchema['schema_value'] as $methodParameterMeta) {
73  $paramName = $methodParameterMeta[CommunicationConfig::SCHEMA_METHOD_PARAM_NAME];
74  $paramType = $methodParameterMeta[CommunicationConfig::SCHEMA_METHOD_PARAM_TYPE];
75  if ($isIndexedArray) {
76  $paramPosition = $methodParameterMeta[CommunicationConfig::SCHEMA_METHOD_PARAM_POSITION];
77  if (isset($message[$paramPosition])) {
78  $this->validateMessage($message[$paramPosition], $paramType, $topic);
79  }
80  } else {
81  if (isset($message[$paramName])) {
82  if (isset($message[$paramName])) {
83  $this->validateMessage($message[$paramName], $paramType, $topic);
84  }
85  }
86  }
87  }
88  }
89  }
90 
97  protected function validateMessage($message, $messageType, $topic)
98  {
99  if (preg_match_all("/\\\\/", $messageType)) {
100  $this->validateClassType($message, $messageType, $topic);
101  } else {
102  $this->validatePrimitiveType($message, $messageType, $topic);
103  }
104  }
105 
112  protected function validatePrimitiveType($message, $messageType, $topic)
113  {
114  $compareType = $messageType;
115  $realType = $this->getRealType($message);
116  if ($realType == 'array' && count($message) == 0) {
117  return;
118  } elseif ($realType == 'array' && count($message) > 0) {
119  $realType = $this->getRealType($message[0]);
120  $compareType = preg_replace('/\[\]/', '', $messageType);
121  }
122  if ($realType !== $compareType) {
123  throw new InvalidArgumentException(
124  new Phrase(
125  'Data in topic "%topic" must be of type "%expectedType". '
126  . '"%actualType" given.',
127  [
128  'topic' => $topic,
129  'expectedType' => $messageType,
130  'actualType' => $this->getRealType($message)
131  ]
132  )
133  );
134  }
135  }
136 
143  protected function validateClassType($message, $messageType, $topic)
144  {
145  $origMessage = $message;
146  $compareType = $messageType;
147  $realType = $this->getRealType($message);
148  if ($realType == 'array' && count($message) == 0) {
149  return;
150  } elseif ($realType == 'array' && count($message) > 0) {
151  $message = $message[0];
152  $compareType = preg_replace('/\[\]/', '', $messageType);
153  }
154  if (!($message instanceof $compareType)) {
155  throw new InvalidArgumentException(
156  new Phrase(
157  'Data in topic "%topic" must be of type "%expectedType". '
158  . '"%actualType" given.',
159  [
160  'topic' => $topic,
161  'expectedType' => $messageType,
162  'actualType' => $this->getRealType($origMessage)
163  ]
164  )
165  );
166  }
167  }
168 
173  private function getRealType($message)
174  {
175  $type = is_object($message) ? get_class($message) : gettype($message);
176  $type = $type == 'boolean' ? 'bool' : $type;
177  $type = $type == 'double' ? 'float' : $type;
178  return $type == "integer" ? "int" : $type;
179  }
180 
188  private function getCommunicationConfig()
189  {
190  if ($this->communicationConfig === null) {
191  $this->communicationConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(
192  CommunicationConfig::class
193  );
194  }
195  return $this->communicationConfig;
196  }
197 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$message
validateMessage($message, $messageType, $topic)
validateClassType($message, $messageType, $topic)
$type
Definition: item.phtml:13
validatePrimitiveType($message, $messageType, $topic)
validate($topic, $message, $requestType=true)