Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class Validator
15 {
19  private $methodsMap;
20 
24  private $typeProcessor;
25 
32  public function __construct(
33  TypeProcessor $typeProcessor,
34  MethodsMap $methodsMap
35  ) {
36  $this->typeProcessor = $typeProcessor;
37  $this->methodsMap = $methodsMap;
38  }
39 
49  public function validateSchemaMethodType($schemaType, $schemaMethod, $topicName)
50  {
51  try {
52  $this->methodsMap->getMethodParams($schemaType, $schemaMethod);
53  } catch (\Exception $e) {
54  throw new \LogicException(
55  sprintf(
56  'Service method specified for topic "%s" is not available. Given "%s"',
57  $topicName,
58  $schemaType . '::' . $schemaMethod
59  )
60  );
61  }
62  }
63 
73  public function validateHandlerType($serviceName, $methodName, $consumerName)
74  {
75  try {
76  $this->methodsMap->getMethodParams($serviceName, $methodName);
77  } catch (\Exception $e) {
78  throw new \LogicException(
79  sprintf(
80  'Service method specified in handler for consumer "%s"'
81  . ' is not available. Given "%s"',
82  $consumerName,
83  $serviceName . '::' . $methodName
84  )
85  );
86  }
87  }
88 
97  public function validateBindTopic($topics, $topicName)
98  {
99  $isDefined = false;
100  if (strpos($topicName, '#') === false && strpos($topicName, '*') === false) {
101  if (in_array($topicName, $topics)) {
102  $isDefined = true;
103  }
104  } else {
105  $pattern = $this->buildWildcardPattern($topicName);
106  if (count(preg_grep($pattern, $topics))) {
107  $isDefined = true;
108  }
109  }
110  if (!$isDefined) {
111  throw new \LogicException(
112  sprintf('Topic "%s" declared in binds must be defined in topics', $topicName)
113  );
114  }
115  }
116 
123  public function buildWildcardPattern($wildcardKey)
124  {
125  $pattern = '/^' . str_replace('.', '\.', $wildcardKey);
126  $pattern = str_replace('#', '.+', $pattern);
127  $pattern = str_replace('*', '[^\.]+', $pattern);
128  if (strpos($wildcardKey, '#') === strlen($wildcardKey)) {
129  $pattern .= '/';
130  } else {
131  $pattern .= '$/';
132  }
133 
134  return $pattern;
135  }
136 
146  public function validateTopicPublisher($publishers, $publisherName, $topicName)
147  {
148  if (!in_array($publisherName, $publishers)) {
149  throw new \LogicException(
150  sprintf(
151  'Publisher "%s", specified in env.php for topic "%s" is not declared.',
152  $publisherName,
153  $topicName
154  )
155  );
156  }
157  }
158 
167  public function validateResponseSchemaType($responseSchema, $topicName)
168  {
169  try {
170  $this->validateType($responseSchema);
171  } catch (\Exception $e) {
172  throw new \LogicException(
173  sprintf(
174  'Response schema definition for topic "%s" should reference existing type or service class. '
175  . 'Given "%s"',
176  $topicName,
177  $responseSchema
178  )
179  );
180  }
181  }
182 
191  public function validateSchemaType($schema, $topicName)
192  {
193  try {
194  $this->validateType($schema);
195  } catch (\Exception $e) {
196  throw new \LogicException(
197  sprintf(
198  'Schema definition for topic "%s" should reference existing type or service class. '
199  . 'Given "%s"',
200  $topicName,
201  $schema
202  )
203  );
204  }
205  }
206 
214  protected function validateType($typeName)
215  {
216  if ($this->typeProcessor->isTypeSimple($typeName)) {
217  return $this;
218  }
219  if ($this->typeProcessor->isArrayType($typeName)) {
220  $arrayItemType = $this->typeProcessor->getArrayItemType($typeName);
221  $this->methodsMap->getMethodsMap($arrayItemType);
222  } else {
223  $this->methodsMap->getMethodsMap($typeName);
224  }
225  return $this;
226  }
227 }
validateSchemaMethodType($schemaType, $schemaMethod, $topicName)
Definition: Validator.php:49
$pattern
Definition: website.php:22
__construct(TypeProcessor $typeProcessor, MethodsMap $methodsMap)
Definition: Validator.php:32
validateResponseSchemaType($responseSchema, $topicName)
Definition: Validator.php:167
validateTopicPublisher($publishers, $publisherName, $topicName)
Definition: Validator.php:146
validateHandlerType($serviceName, $methodName, $consumerName)
Definition: Validator.php:73