Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Smd.php
Go to the documentation of this file.
1 <?php
31 {
32  const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
33  const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
34  const SMD_VERSION = '2.0';
35 
40  protected $_contentType = 'application/json';
41 
46  protected $_contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
47 
52  protected $_description;
53 
58  protected $_dojoCompatible = false;
59 
65 
70  protected $_envelopeTypes = array(
71  self::ENV_JSONRPC_1,
72  self::ENV_JSONRPC_2,
73  );
74 
79  protected $_id;
80 
85  protected $_services = array();
86 
91  protected $_target;
92 
97  protected $_transport = 'POST';
98 
103  protected $_transportTypes = array('POST');
104 
111  public function setOptions(array $options)
112  {
113  $methods = get_class_methods($this);
114  foreach ($options as $key => $value) {
115  $method = 'set' . ucfirst($key);
116  if (in_array($method, $methods)) {
117  $this->$method($value);
118  }
119  }
120  return $this;
121  }
122 
129  public function setTransport($transport)
130  {
131  if (!in_array($transport, $this->_transportTypes)) {
132  #require_once 'Zend/Json/Server/Exception.php';
133  throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s" specified', $transport));
134  }
135  $this->_transport = $transport;
136  return $this;
137  }
138 
144  public function getTransport()
145  {
146  return $this->_transport;
147  }
148 
155  public function setEnvelope($envelopeType)
156  {
157  if (!in_array($envelopeType, $this->_envelopeTypes)) {
158  #require_once 'Zend/Json/Server/Exception.php';
159  throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"', $envelopeType));
160  }
161  $this->_envelope = $envelopeType;
162  return $this;
163  }
164 
170  public function getEnvelope()
171  {
172  return $this->_envelope;
173  }
174 
175  // Content-Type of response; default to application/json
182  public function setContentType($type)
183  {
184  if (!preg_match($this->_contentTypeRegex, $type)) {
185  #require_once 'Zend/Json/Server/Exception.php';
186  throw new Zend_Json_Server_Exception(sprintf('Invalid content type "%s" specified', $type));
187  }
188  $this->_contentType = $type;
189  return $this;
190  }
191 
197  public function getContentType()
198  {
199  return $this->_contentType;
200  }
201 
208  public function setTarget($target)
209  {
210  $this->_target = (string) $target;
211  return $this;
212  }
213 
219  public function getTarget()
220  {
221  return $this->_target;
222  }
223 
230  public function setId($id)
231  {
232  $this->_id = (string) $id;
233  return $this->_id;
234  }
235 
241  public function getId()
242  {
243  return $this->_id;
244  }
245 
252  public function setDescription($description)
253  {
254  $this->_description = (string) $description;
255  return $this->_description;
256  }
257 
263  public function getDescription()
264  {
265  return $this->_description;
266  }
267 
274  public function setDojoCompatible($flag)
275  {
276  $this->_dojoCompatible = (bool) $flag;
277  return $this;
278  }
279 
285  public function isDojoCompatible()
286  {
287  return $this->_dojoCompatible;
288  }
289 
296  public function addService($service)
297  {
298  #require_once 'Zend/Json/Server/Smd/Service.php';
299 
300  if ($service instanceof Zend_Json_Server_Smd_Service) {
301  $name = $service->getName();
302  } elseif (is_array($service)) {
304  $name = $service->getName();
305  } else {
306  #require_once 'Zend/Json/Server/Exception.php';
307  throw new Zend_Json_Server_Exception('Invalid service passed to addService()');
308  }
309 
310  if (array_key_exists($name, $this->_services)) {
311  #require_once 'Zend/Json/Server/Exception.php';
312  throw new Zend_Json_Server_Exception('Attempt to register a service already registered detected');
313  }
314  $this->_services[$name] = $service;
315  return $this;
316  }
317 
324  public function addServices(array $services)
325  {
326  foreach ($services as $service) {
327  $this->addService($service);
328  }
329  return $this;
330  }
331 
338  public function setServices(array $services)
339  {
340  $this->_services = array();
341  return $this->addServices($services);
342  }
343 
350  public function getService($name)
351  {
352  if (array_key_exists($name, $this->_services)) {
353  return $this->_services[$name];
354  }
355  return false;
356  }
357 
363  public function getServices()
364  {
365  return $this->_services;
366  }
367 
374  public function removeService($name)
375  {
376  if (array_key_exists($name, $this->_services)) {
377  unset($this->_services[$name]);
378  return true;
379  }
380  return false;
381  }
382 
388  public function toArray()
389  {
390  if ($this->isDojoCompatible()) {
391  return $this->toDojoArray();
392  }
393 
394  $transport = $this->getTransport();
395  $envelope = $this->getEnvelope();
396  $contentType = $this->getContentType();
397  $SMDVersion = self::SMD_VERSION;
398  $service = compact('transport', 'envelope', 'contentType', 'SMDVersion');
399 
400  if (null !== ($target = $this->getTarget())) {
401  $service['target'] = $target;
402  }
403  if (null !== ($id = $this->getId())) {
404  $service['id'] = $id;
405  }
406 
407  $services = $this->getServices();
408  if (!empty($services)) {
409  $service['services'] = array();
410  foreach ($services as $name => $svc) {
411  $svc->setEnvelope($envelope);
412  $service['services'][$name] = $svc->toArray();
413  }
414  $service['methods'] = $service['services'];
415  }
416 
417  return $service;
418  }
419 
425  public function toDojoArray()
426  {
427  $SMDVersion = '.1';
428  $serviceType = 'JSON-RPC';
429  $service = compact('SMDVersion', 'serviceType');
430 
431  $target = $this->getTarget();
432 
433  $services = $this->getServices();
434  if (!empty($services)) {
435  $service['methods'] = array();
436  foreach ($services as $name => $svc) {
437  $method = array(
438  'name' => $name,
439  'serviceURL' => $target,
440  );
441  $params = array();
442  foreach ($svc->getParams() as $param) {
443  $paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
444  $params[] = array(
445  'name' => $paramName,
446  'type' => $param['type'],
447  );
448  }
449  if (!empty($params)) {
450  $method['parameters'] = $params;
451  }
452  $service['methods'][] = $method;
453  }
454  }
455 
456  return $service;
457  }
458 
464  public function toJson()
465  {
466  #require_once 'Zend/Json.php';
467  return Zend_Json::encode($this->toArray());
468  }
469 
475  public function __toString()
476  {
477  return $this->toJson();
478  }
479 }
480 
removeService($name)
Definition: Smd.php:374
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
setContentType($type)
Definition: Smd.php:182
const ENV_JSONRPC_1
Definition: Smd.php:32
setDojoCompatible($flag)
Definition: Smd.php:274
$target
Definition: skip.phtml:8
getService($name)
Definition: Smd.php:350
setDescription($description)
Definition: Smd.php:252
setEnvelope($envelopeType)
Definition: Smd.php:155
const ENV_JSONRPC_2
Definition: Smd.php:33
$type
Definition: item.phtml:13
setServices(array $services)
Definition: Smd.php:338
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
setTransport($transport)
Definition: Smd.php:129
setTarget($target)
Definition: Smd.php:208
static encode($valueToEncode, $cycleCheck=false, $options=array())
Definition: Json.php:130
const SMD_VERSION
Definition: Smd.php:34
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
addServices(array $services)
Definition: Smd.php:324
addService($service)
Definition: Smd.php:296
setOptions(array $options)
Definition: Smd.php:111
if(!isset($_GET['name'])) $name
Definition: log.php:14