PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Json/Server/Smd/Service.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 473 lines | 242 code | 40 blank | 191 comment | 27 complexity | 01c05c3f005f6ba57d1d64d76fd896a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @see Zend_Json_Server_Smd
  22. */
  23. #require_once 'Zend/Json/Server/Smd.php';
  24. /**
  25. * Create Service Mapping Description for a method
  26. *
  27. * @package Zend_Json
  28. * @subpackage Server
  29. * @version $Id: Service.php 20096 2010-01-06 02:05:09Z bkarwin $
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Json_Server_Smd_Service
  34. {
  35. /**#@+
  36. * Service metadata
  37. * @var string
  38. */
  39. protected $_envelope = Zend_Json_Server_Smd::ENV_JSONRPC_1;
  40. protected $_name;
  41. protected $_return;
  42. protected $_target;
  43. protected $_transport = 'POST';
  44. /**#@-*/
  45. /**
  46. * Allowed envelope types
  47. * @var array
  48. */
  49. protected $_envelopeTypes = array(
  50. Zend_Json_Server_Smd::ENV_JSONRPC_1,
  51. Zend_Json_Server_Smd::ENV_JSONRPC_2,
  52. );
  53. /**
  54. * Regex for names
  55. * @var string
  56. */
  57. protected $_nameRegex = '/^[a-z][a-z0-9._]+$/i';
  58. /**
  59. * Parameter option types
  60. * @var array
  61. */
  62. protected $_paramOptionTypes = array(
  63. 'name' => 'is_string',
  64. 'optional' => 'is_bool',
  65. 'default' => null,
  66. 'description' => 'is_string',
  67. );
  68. /**
  69. * Service params
  70. * @var array
  71. */
  72. protected $_params = array();
  73. /**
  74. * Mapping of parameter types to JSON-RPC types
  75. * @var array
  76. */
  77. protected $_paramMap = array(
  78. 'any' => 'any',
  79. 'arr' => 'array',
  80. 'array' => 'array',
  81. 'assoc' => 'object',
  82. 'bool' => 'boolean',
  83. 'boolean' => 'boolean',
  84. 'dbl' => 'float',
  85. 'double' => 'float',
  86. 'false' => 'boolean',
  87. 'float' => 'float',
  88. 'hash' => 'object',
  89. 'integer' => 'integer',
  90. 'int' => 'integer',
  91. 'mixed' => 'any',
  92. 'nil' => 'null',
  93. 'null' => 'null',
  94. 'object' => 'object',
  95. 'string' => 'string',
  96. 'str' => 'string',
  97. 'struct' => 'object',
  98. 'true' => 'boolean',
  99. 'void' => 'null',
  100. );
  101. /**
  102. * Allowed transport types
  103. * @var array
  104. */
  105. protected $_transportTypes = array(
  106. 'POST',
  107. );
  108. /**
  109. * Constructor
  110. *
  111. * @param string|array $spec
  112. * @return void
  113. * @throws Zend_Json_Server_Exception if no name provided
  114. */
  115. public function __construct($spec)
  116. {
  117. if (is_string($spec)) {
  118. $this->setName($spec);
  119. } elseif (is_array($spec)) {
  120. $this->setOptions($spec);
  121. }
  122. if (null == $this->getName()) {
  123. #require_once 'Zend/Json/Server/Exception.php';
  124. throw new Zend_Json_Server_Exception('SMD service description requires a name; none provided');
  125. }
  126. }
  127. /**
  128. * Set object state
  129. *
  130. * @param array $options
  131. * @return Zend_Json_Server_Smd_Service
  132. */
  133. public function setOptions(array $options)
  134. {
  135. $methods = get_class_methods($this);
  136. foreach ($options as $key => $value) {
  137. if ('options' == strtolower($key)) {
  138. continue;
  139. }
  140. $method = 'set' . ucfirst($key);
  141. if (in_array($method, $methods)) {
  142. $this->$method($value);
  143. }
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Set service name
  149. *
  150. * @param string $name
  151. * @return Zend_Json_Server_Smd_Service
  152. * @throws Zend_Json_Server_Exception
  153. */
  154. public function setName($name)
  155. {
  156. $name = (string) $name;
  157. if (!preg_match($this->_nameRegex, $name)) {
  158. #require_once 'Zend/Json/Server/Exception.php';
  159. throw new Zend_Json_Server_Exception(sprintf('Invalid name "%s" provided for service; must follow PHP method naming conventions', $name));
  160. }
  161. $this->_name = $name;
  162. return $this;
  163. }
  164. /**
  165. * Retrieve name
  166. *
  167. * @return string
  168. */
  169. public function getName()
  170. {
  171. return $this->_name;
  172. }
  173. /**
  174. * Set Transport
  175. *
  176. * Currently limited to POST
  177. *
  178. * @param string $transport
  179. * @return Zend_Json_Server_Smd_Service
  180. */
  181. public function setTransport($transport)
  182. {
  183. if (!in_array($transport, $this->_transportTypes)) {
  184. #require_once 'Zend/Json/Server/Exception.php';
  185. throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s"; please select one of (%s)', $transport, implode(', ', $this->_transportTypes)));
  186. }
  187. $this->_transport = $transport;
  188. return $this;
  189. }
  190. /**
  191. * Get transport
  192. *
  193. * @return string
  194. */
  195. public function getTransport()
  196. {
  197. return $this->_transport;
  198. }
  199. /**
  200. * Set service target
  201. *
  202. * @param string $target
  203. * @return Zend_Json_Server_Smd_Service
  204. */
  205. public function setTarget($target)
  206. {
  207. $this->_target = (string) $target;
  208. return $this;
  209. }
  210. /**
  211. * Get service target
  212. *
  213. * @return string
  214. */
  215. public function getTarget()
  216. {
  217. return $this->_target;
  218. }
  219. /**
  220. * Set envelope type
  221. *
  222. * @param string $envelopeType
  223. * @return Zend_Json_Server_Smd_Service
  224. */
  225. public function setEnvelope($envelopeType)
  226. {
  227. if (!in_array($envelopeType, $this->_envelopeTypes)) {
  228. #require_once 'Zend/Json/Server/Exception.php';
  229. throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"; please specify one of (%s)', $envelopeType, implode(', ', $this->_envelopeTypes)));
  230. }
  231. $this->_envelope = $envelopeType;
  232. return $this;
  233. }
  234. /**
  235. * Get envelope type
  236. *
  237. * @return string
  238. */
  239. public function getEnvelope()
  240. {
  241. return $this->_envelope;
  242. }
  243. /**
  244. * Add a parameter to the service
  245. *
  246. * @param string|array $type
  247. * @param array $options
  248. * @param int|null $order
  249. * @return Zend_Json_Server_Smd_Service
  250. */
  251. public function addParam($type, array $options = array(), $order = null)
  252. {
  253. if (is_string($type)) {
  254. $type = $this->_validateParamType($type);
  255. } elseif (is_array($type)) {
  256. foreach ($type as $key => $paramType) {
  257. $type[$key] = $this->_validateParamType($paramType);
  258. }
  259. } else {
  260. #require_once 'Zend/Json/Server/Exception.php';
  261. throw new Zend_Json_Server_Exception('Invalid param type provided');
  262. }
  263. $paramOptions = array(
  264. 'type' => $type,
  265. );
  266. foreach ($options as $key => $value) {
  267. if (in_array($key, array_keys($this->_paramOptionTypes))) {
  268. if (null !== ($callback = $this->_paramOptionTypes[$key])) {
  269. if (!$callback($value)) {
  270. continue;
  271. }
  272. }
  273. $paramOptions[$key] = $value;
  274. }
  275. }
  276. $this->_params[] = array(
  277. 'param' => $paramOptions,
  278. 'order' => $order,
  279. );
  280. return $this;
  281. }
  282. /**
  283. * Add params
  284. *
  285. * Each param should be an array, and should include the key 'type'.
  286. *
  287. * @param array $params
  288. * @return Zend_Json_Server_Smd_Service
  289. */
  290. public function addParams(array $params)
  291. {
  292. ksort($params);
  293. foreach ($params as $options) {
  294. if (!is_array($options)) {
  295. continue;
  296. }
  297. if (!array_key_exists('type', $options)) {
  298. continue;
  299. }
  300. $type = $options['type'];
  301. $order = (array_key_exists('order', $options)) ? $options['order'] : null;
  302. $this->addParam($type, $options, $order);
  303. }
  304. return $this;
  305. }
  306. /**
  307. * Overwrite all parameters
  308. *
  309. * @param array $params
  310. * @return Zend_Json_Server_Smd_Service
  311. */
  312. public function setParams(array $params)
  313. {
  314. $this->_params = array();
  315. return $this->addParams($params);
  316. }
  317. /**
  318. * Get all parameters
  319. *
  320. * Returns all params in specified order.
  321. *
  322. * @return array
  323. */
  324. public function getParams()
  325. {
  326. $params = array();
  327. $index = 0;
  328. foreach ($this->_params as $param) {
  329. if (null === $param['order']) {
  330. if (array_search($index, array_keys($params), true)) {
  331. ++$index;
  332. }
  333. $params[$index] = $param['param'];
  334. ++$index;
  335. } else {
  336. $params[$param['order']] = $param['param'];
  337. }
  338. }
  339. ksort($params);
  340. return $params;
  341. }
  342. /**
  343. * Set return type
  344. *
  345. * @param string|array $type
  346. * @return Zend_Json_Server_Smd_Service
  347. */
  348. public function setReturn($type)
  349. {
  350. if (is_string($type)) {
  351. $type = $this->_validateParamType($type, true);
  352. } elseif (is_array($type)) {
  353. foreach ($type as $key => $returnType) {
  354. $type[$key] = $this->_validateParamType($returnType, true);
  355. }
  356. } else {
  357. #require_once 'Zend/Json/Server/Exception.php';
  358. throw new Zend_Json_Server_Exception('Invalid param type provided ("' . gettype($type) .'")');
  359. }
  360. $this->_return = $type;
  361. return $this;
  362. }
  363. /**
  364. * Get return type
  365. *
  366. * @return string|array
  367. */
  368. public function getReturn()
  369. {
  370. return $this->_return;
  371. }
  372. /**
  373. * Cast service description to array
  374. *
  375. * @return array
  376. */
  377. public function toArray()
  378. {
  379. $name = $this->getName();
  380. $envelope = $this->getEnvelope();
  381. $target = $this->getTarget();
  382. $transport = $this->getTransport();
  383. $parameters = $this->getParams();
  384. $returns = $this->getReturn();
  385. if (empty($target)) {
  386. return compact('envelope', 'transport', 'parameters', 'returns');
  387. }
  388. return $paramInfo = compact('envelope', 'target', 'transport', 'parameters', 'returns');
  389. }
  390. /**
  391. * Return JSON encoding of service
  392. *
  393. * @return string
  394. */
  395. public function toJson()
  396. {
  397. $service = array($this->getName() => $this->toArray());
  398. #require_once 'Zend/Json.php';
  399. return Zend_Json::encode($service);
  400. }
  401. /**
  402. * Cast to string
  403. *
  404. * @return string
  405. */
  406. public function __toString()
  407. {
  408. return $this->toJson();
  409. }
  410. /**
  411. * Validate parameter type
  412. *
  413. * @param string $type
  414. * @return true
  415. * @throws Zend_Json_Server_Exception
  416. */
  417. protected function _validateParamType($type, $isReturn = false)
  418. {
  419. if (!is_string($type)) {
  420. #require_once 'Zend/Json/Server/Exception.php';
  421. throw new Zend_Json_Server_Exception('Invalid param type provided ("' . $type .'")');
  422. }
  423. if (!array_key_exists($type, $this->_paramMap)) {
  424. $type = 'object';
  425. }
  426. $paramType = $this->_paramMap[$type];
  427. if (!$isReturn && ('null' == $paramType)) {
  428. #require_once 'Zend/Json/Server/Exception.php';
  429. throw new Zend_Json_Server_Exception('Invalid param type provided ("' . $type . '")');
  430. }
  431. return $paramType;
  432. }
  433. }