PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-json/src/Server/Smd.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 461 lines | 219 code | 47 blank | 195 comment | 15 complexity | 85e24ec9ae50ac6c1ed3bfa74cf58f1c MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Json\Server;
  10. use Zend\Json\Server\Exception\InvalidArgumentException;
  11. use Zend\Json\Server\Exception\RuntimeException;
  12. class Smd
  13. {
  14. const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
  15. const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
  16. const SMD_VERSION = '2.0';
  17. /**
  18. * Content type
  19. * @var string
  20. */
  21. protected $contentType = 'application/json';
  22. /**
  23. * Content type regex
  24. * @var string
  25. */
  26. protected $contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
  27. /**
  28. * Service description
  29. * @var string
  30. */
  31. protected $description;
  32. /**
  33. * Generate Dojo-compatible SMD
  34. * @var bool
  35. */
  36. protected $dojoCompatible = false;
  37. /**
  38. * Current envelope
  39. * @var string
  40. */
  41. protected $envelope = self::ENV_JSONRPC_1;
  42. /**
  43. * Allowed envelope types
  44. * @var array
  45. */
  46. protected $envelopeTypes = array(
  47. self::ENV_JSONRPC_1,
  48. self::ENV_JSONRPC_2,
  49. );
  50. /**
  51. * Service id
  52. * @var string
  53. */
  54. protected $id;
  55. /**
  56. * Services offered
  57. * @var array
  58. */
  59. protected $services = array();
  60. /**
  61. * Service target
  62. * @var string
  63. */
  64. protected $target;
  65. /**
  66. * Global transport
  67. * @var string
  68. */
  69. protected $transport = 'POST';
  70. /**
  71. * Allowed transport types
  72. * @var array
  73. */
  74. protected $transportTypes = array('POST');
  75. /**
  76. * Set object state via options
  77. *
  78. * @param array $options
  79. * @return Smd
  80. */
  81. public function setOptions(array $options)
  82. {
  83. foreach ($options as $key => $value) {
  84. $method = 'set' . ucfirst($key);
  85. if (method_exists($this, $method)) {
  86. $this->$method($value);
  87. }
  88. }
  89. return $this;
  90. }
  91. /**
  92. * Set transport
  93. *
  94. * @param string $transport
  95. * @throws Exception\InvalidArgumentException
  96. * @return \Zend\Json\Server\Smd
  97. */
  98. public function setTransport($transport)
  99. {
  100. if (!in_array($transport, $this->transportTypes)) {
  101. throw new InvalidArgumentException("Invalid transport '{$transport}' specified");
  102. }
  103. $this->transport = $transport;
  104. return $this;
  105. }
  106. /**
  107. * Get transport
  108. *
  109. * @return string
  110. */
  111. public function getTransport()
  112. {
  113. return $this->transport;
  114. }
  115. /**
  116. * Set envelope
  117. *
  118. * @param string $envelopeType
  119. * @throws Exception\InvalidArgumentException
  120. * @return Smd
  121. */
  122. public function setEnvelope($envelopeType)
  123. {
  124. if (!in_array($envelopeType, $this->envelopeTypes)) {
  125. throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'");
  126. }
  127. $this->envelope = $envelopeType;
  128. return $this;
  129. }
  130. /**
  131. * Retrieve envelope
  132. *
  133. * @return string
  134. */
  135. public function getEnvelope()
  136. {
  137. return $this->envelope;
  138. }
  139. // Content-Type of response; default to application/json
  140. /**
  141. * Set content type
  142. *
  143. * @param string $type
  144. * @throws Exception\InvalidArgumentException
  145. * @return \Zend\Json\Server\Smd
  146. */
  147. public function setContentType($type)
  148. {
  149. if (!preg_match($this->contentTypeRegex, $type)) {
  150. throw new InvalidArgumentException("Invalid content type '{$type}' specified");
  151. }
  152. $this->contentType = $type;
  153. return $this;
  154. }
  155. /**
  156. * Retrieve content type
  157. *
  158. * @return string
  159. */
  160. public function getContentType()
  161. {
  162. return $this->contentType;
  163. }
  164. /**
  165. * Set service target
  166. *
  167. * @param string $target
  168. * @return Smd
  169. */
  170. public function setTarget($target)
  171. {
  172. $this->target = (string) $target;
  173. return $this;
  174. }
  175. /**
  176. * Retrieve service target
  177. *
  178. * @return string
  179. */
  180. public function getTarget()
  181. {
  182. return $this->target;
  183. }
  184. /**
  185. * Set service ID
  186. *
  187. * @param string $id
  188. * @return Smd
  189. */
  190. public function setId($id)
  191. {
  192. $this->id = (string) $id;
  193. return $this->id;
  194. }
  195. /**
  196. * Get service id
  197. *
  198. * @return string
  199. */
  200. public function getId()
  201. {
  202. return $this->id;
  203. }
  204. /**
  205. * Set service description
  206. *
  207. * @param string $description
  208. * @return Smd
  209. */
  210. public function setDescription($description)
  211. {
  212. $this->description = (string) $description;
  213. return $this->description;
  214. }
  215. /**
  216. * Get service description
  217. *
  218. * @return string
  219. */
  220. public function getDescription()
  221. {
  222. return $this->description;
  223. }
  224. /**
  225. * Indicate whether or not to generate Dojo-compatible SMD
  226. *
  227. * @param bool $flag
  228. * @return Smd
  229. */
  230. public function setDojoCompatible($flag)
  231. {
  232. $this->dojoCompatible = (bool) $flag;
  233. return $this;
  234. }
  235. /**
  236. * Is this a Dojo compatible SMD?
  237. *
  238. * @return bool
  239. */
  240. public function isDojoCompatible()
  241. {
  242. return $this->dojoCompatible;
  243. }
  244. /**
  245. * Add Service
  246. *
  247. * @param Smd\Service|array $service
  248. * @throws Exception\RuntimeException
  249. * @throws Exception\InvalidArgumentException
  250. * @return Smd
  251. */
  252. public function addService($service)
  253. {
  254. if ($service instanceof Smd\Service) {
  255. $name = $service->getName();
  256. } elseif (is_array($service)) {
  257. $service = new Smd\Service($service);
  258. $name = $service->getName();
  259. } else {
  260. throw new InvalidArgumentException('Invalid service passed to addService()');
  261. }
  262. if (array_key_exists($name, $this->services)) {
  263. throw new RuntimeException('Attempt to register a service already registered detected');
  264. }
  265. $this->services[$name] = $service;
  266. return $this;
  267. }
  268. /**
  269. * Add many services
  270. *
  271. * @param array $services
  272. * @return Smd
  273. */
  274. public function addServices(array $services)
  275. {
  276. foreach ($services as $service) {
  277. $this->addService($service);
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Overwrite existing services with new ones
  283. *
  284. * @param array $services
  285. * @return Smd
  286. */
  287. public function setServices(array $services)
  288. {
  289. $this->services = array();
  290. return $this->addServices($services);
  291. }
  292. /**
  293. * Get service object
  294. *
  295. * @param string $name
  296. * @return bool|Smd\Service
  297. */
  298. public function getService($name)
  299. {
  300. if (array_key_exists($name, $this->services)) {
  301. return $this->services[$name];
  302. }
  303. return false;
  304. }
  305. /**
  306. * Return services
  307. *
  308. * @return array
  309. */
  310. public function getServices()
  311. {
  312. return $this->services;
  313. }
  314. /**
  315. * Remove service
  316. *
  317. * @param string $name
  318. * @return bool
  319. */
  320. public function removeService($name)
  321. {
  322. if (array_key_exists($name, $this->services)) {
  323. unset($this->services[$name]);
  324. return true;
  325. }
  326. return false;
  327. }
  328. /**
  329. * Cast to array
  330. *
  331. * @return array
  332. */
  333. public function toArray()
  334. {
  335. if ($this->isDojoCompatible()) {
  336. return $this->toDojoArray();
  337. }
  338. $description = $this->getDescription();
  339. $transport = $this->getTransport();
  340. $envelope = $this->getEnvelope();
  341. $contentType = $this->getContentType();
  342. $SMDVersion = static::SMD_VERSION;
  343. $service = compact('transport', 'envelope', 'contentType', 'SMDVersion', 'description');
  344. if (null !== ($target = $this->getTarget())) {
  345. $service['target'] = $target;
  346. }
  347. if (null !== ($id = $this->getId())) {
  348. $service['id'] = $id;
  349. }
  350. $services = $this->getServices();
  351. if (!empty($services)) {
  352. $service['services'] = array();
  353. foreach ($services as $name => $svc) {
  354. $svc->setEnvelope($envelope);
  355. $service['services'][$name] = $svc->toArray();
  356. }
  357. $service['methods'] = $service['services'];
  358. }
  359. return $service;
  360. }
  361. /**
  362. * Export to DOJO-compatible SMD array
  363. *
  364. * @return array
  365. */
  366. public function toDojoArray()
  367. {
  368. $SMDVersion = '.1';
  369. $serviceType = 'JSON-RPC';
  370. $service = compact('SMDVersion', 'serviceType');
  371. $target = $this->getTarget();
  372. $services = $this->getServices();
  373. if (!empty($services)) {
  374. $service['methods'] = array();
  375. foreach ($services as $name => $svc) {
  376. $method = array(
  377. 'name' => $name,
  378. 'serviceURL' => $target,
  379. );
  380. $params = array();
  381. foreach ($svc->getParams() as $param) {
  382. $paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
  383. $params[] = array(
  384. 'name' => $paramName,
  385. 'type' => $param['type'],
  386. );
  387. }
  388. if (!empty($params)) {
  389. $method['parameters'] = $params;
  390. }
  391. $service['methods'][] = $method;
  392. }
  393. }
  394. return $service;
  395. }
  396. /**
  397. * Cast to JSON
  398. *
  399. * @return string
  400. */
  401. public function toJson()
  402. {
  403. return \Zend\Json\Json::encode($this->toArray());
  404. }
  405. /**
  406. * Cast to string (JSON)
  407. *
  408. * @return string
  409. */
  410. public function __toString()
  411. {
  412. return $this->toJson();
  413. }
  414. }