PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Ebozavrik/test-application
PHP | 494 lines | 252 code | 50 blank | 192 comment | 27 complexity | 398ba76ecc5f735a8c776aae2bf081dd 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  30. * @copyright Copyright (c) 2005-2012 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. *
  113. * @return void
  114. * @throws Zend_Json_Server_Exception if no name provided
  115. */
  116. public function __construct ($spec)
  117. {
  118. if (is_string($spec)) {
  119. $this->setName($spec);
  120. } elseif (is_array($spec)) {
  121. $this->setOptions($spec);
  122. }
  123. if (null == $this->getName()) {
  124. require_once 'Zend/Json/Server/Exception.php';
  125. throw new Zend_Json_Server_Exception( 'SMD service description requires a name; none provided' );
  126. }
  127. }
  128. /**
  129. * Set object state
  130. *
  131. * @param array $options
  132. *
  133. * @return Zend_Json_Server_Smd_Service
  134. */
  135. public function setOptions (array $options)
  136. {
  137. $methods = get_class_methods($this);
  138. foreach ($options as $key => $value) {
  139. if ('options' == strtolower($key)) {
  140. continue;
  141. }
  142. $method = 'set' . ucfirst($key);
  143. if (in_array($method, $methods)) {
  144. $this->$method($value);
  145. }
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Set service name
  151. *
  152. * @param string $name
  153. *
  154. * @return Zend_Json_Server_Smd_Service
  155. * @throws Zend_Json_Server_Exception
  156. */
  157. public function setName ($name)
  158. {
  159. $name = (string)$name;
  160. if (!preg_match($this->_nameRegex, $name)) {
  161. require_once 'Zend/Json/Server/Exception.php';
  162. throw new Zend_Json_Server_Exception( sprintf('Invalid name "%s" provided for service; must follow PHP method naming conventions', $name) );
  163. }
  164. $this->_name = $name;
  165. return $this;
  166. }
  167. /**
  168. * Retrieve name
  169. *
  170. * @return string
  171. */
  172. public function getName ()
  173. {
  174. return $this->_name;
  175. }
  176. /**
  177. * Set Transport
  178. *
  179. * Currently limited to POST
  180. *
  181. * @param string $transport
  182. *
  183. * @return Zend_Json_Server_Smd_Service
  184. */
  185. public function setTransport ($transport)
  186. {
  187. if (!in_array($transport, $this->_transportTypes)) {
  188. require_once 'Zend/Json/Server/Exception.php';
  189. throw new Zend_Json_Server_Exception( sprintf('Invalid transport "%s"; please select one of (%s)', $transport, implode(', ', $this->_transportTypes)) );
  190. }
  191. $this->_transport = $transport;
  192. return $this;
  193. }
  194. /**
  195. * Get transport
  196. *
  197. * @return string
  198. */
  199. public function getTransport ()
  200. {
  201. return $this->_transport;
  202. }
  203. /**
  204. * Set service target
  205. *
  206. * @param string $target
  207. *
  208. * @return Zend_Json_Server_Smd_Service
  209. */
  210. public function setTarget ($target)
  211. {
  212. $this->_target = (string)$target;
  213. return $this;
  214. }
  215. /**
  216. * Get service target
  217. *
  218. * @return string
  219. */
  220. public function getTarget ()
  221. {
  222. return $this->_target;
  223. }
  224. /**
  225. * Set envelope type
  226. *
  227. * @param string $envelopeType
  228. *
  229. * @return Zend_Json_Server_Smd_Service
  230. */
  231. public function setEnvelope ($envelopeType)
  232. {
  233. if (!in_array($envelopeType, $this->_envelopeTypes)) {
  234. require_once 'Zend/Json/Server/Exception.php';
  235. throw new Zend_Json_Server_Exception( sprintf('Invalid envelope type "%s"; please specify one of (%s)', $envelopeType, implode(', ', $this->_envelopeTypes)) );
  236. }
  237. $this->_envelope = $envelopeType;
  238. return $this;
  239. }
  240. /**
  241. * Get envelope type
  242. *
  243. * @return string
  244. */
  245. public function getEnvelope ()
  246. {
  247. return $this->_envelope;
  248. }
  249. /**
  250. * Add a parameter to the service
  251. *
  252. * @param string|array $type
  253. * @param array $options
  254. * @param int|null $order
  255. *
  256. * @return Zend_Json_Server_Smd_Service
  257. */
  258. public function addParam ($type, array $options = array(), $order = null)
  259. {
  260. if (is_string($type)) {
  261. $type = $this->_validateParamType($type);
  262. } elseif (is_array($type)) {
  263. foreach ($type as $key => $paramType) {
  264. $type[$key] = $this->_validateParamType($paramType);
  265. }
  266. } else {
  267. require_once 'Zend/Json/Server/Exception.php';
  268. throw new Zend_Json_Server_Exception( 'Invalid param type provided' );
  269. }
  270. $paramOptions = array(
  271. 'type' => $type,
  272. );
  273. foreach ($options as $key => $value) {
  274. if (in_array($key, array_keys($this->_paramOptionTypes))) {
  275. if (null !== ( $callback = $this->_paramOptionTypes[$key] )) {
  276. if (!$callback($value)) {
  277. continue;
  278. }
  279. }
  280. $paramOptions[$key] = $value;
  281. }
  282. }
  283. $this->_params[] = array(
  284. 'param' => $paramOptions,
  285. 'order' => $order,
  286. );
  287. return $this;
  288. }
  289. /**
  290. * Add params
  291. *
  292. * Each param should be an array, and should include the key 'type'.
  293. *
  294. * @param array $params
  295. *
  296. * @return Zend_Json_Server_Smd_Service
  297. */
  298. public function addParams (array $params)
  299. {
  300. ksort($params);
  301. foreach ($params as $options) {
  302. if (!is_array($options)) {
  303. continue;
  304. }
  305. if (!array_key_exists('type', $options)) {
  306. continue;
  307. }
  308. $type = $options['type'];
  309. $order = ( array_key_exists('order', $options) ) ? $options['order'] : null;
  310. $this->addParam($type, $options, $order);
  311. }
  312. return $this;
  313. }
  314. /**
  315. * Overwrite all parameters
  316. *
  317. * @param array $params
  318. *
  319. * @return Zend_Json_Server_Smd_Service
  320. */
  321. public function setParams (array $params)
  322. {
  323. $this->_params = array();
  324. return $this->addParams($params);
  325. }
  326. /**
  327. * Get all parameters
  328. *
  329. * Returns all params in specified order.
  330. *
  331. * @return array
  332. */
  333. public function getParams ()
  334. {
  335. $params = array();
  336. $index = 0;
  337. foreach ($this->_params as $param) {
  338. if (null === $param['order']) {
  339. if (array_search($index, array_keys($params), true)) {
  340. ++$index;
  341. }
  342. $params[$index] = $param['param'];
  343. ++$index;
  344. } else {
  345. $params[$param['order']] = $param['param'];
  346. }
  347. }
  348. ksort($params);
  349. return $params;
  350. }
  351. /**
  352. * Set return type
  353. *
  354. * @param string|array $type
  355. *
  356. * @return Zend_Json_Server_Smd_Service
  357. */
  358. public function setReturn ($type)
  359. {
  360. if (is_string($type)) {
  361. $type = $this->_validateParamType($type, true);
  362. } elseif (is_array($type)) {
  363. foreach ($type as $key => $returnType) {
  364. $type[$key] = $this->_validateParamType($returnType, true);
  365. }
  366. } else {
  367. require_once 'Zend/Json/Server/Exception.php';
  368. throw new Zend_Json_Server_Exception( 'Invalid param type provided ("' . gettype($type) . '")' );
  369. }
  370. $this->_return = $type;
  371. return $this;
  372. }
  373. /**
  374. * Get return type
  375. *
  376. * @return string|array
  377. */
  378. public function getReturn ()
  379. {
  380. return $this->_return;
  381. }
  382. /**
  383. * Cast service description to array
  384. *
  385. * @return array
  386. */
  387. public function toArray ()
  388. {
  389. $name = $this->getName();
  390. $envelope = $this->getEnvelope();
  391. $target = $this->getTarget();
  392. $transport = $this->getTransport();
  393. $parameters = $this->getParams();
  394. $returns = $this->getReturn();
  395. if (empty( $target )) {
  396. return compact('envelope', 'transport', 'parameters', 'returns');
  397. }
  398. return $paramInfo = compact('envelope', 'target', 'transport', 'parameters', 'returns');
  399. }
  400. /**
  401. * Return JSON encoding of service
  402. *
  403. * @return string
  404. */
  405. public function toJson ()
  406. {
  407. $service = array( $this->getName() => $this->toArray() );
  408. require_once 'Zend/Json.php';
  409. return Zend_Json::encode($service);
  410. }
  411. /**
  412. * Cast to string
  413. *
  414. * @return string
  415. */
  416. public function __toString ()
  417. {
  418. return $this->toJson();
  419. }
  420. /**
  421. * Validate parameter type
  422. *
  423. * @param string $type
  424. *
  425. * @return true
  426. * @throws Zend_Json_Server_Exception
  427. */
  428. protected function _validateParamType ($type, $isReturn = false)
  429. {
  430. if (!is_string($type)) {
  431. require_once 'Zend/Json/Server/Exception.php';
  432. throw new Zend_Json_Server_Exception( 'Invalid param type provided ("' . $type . '")' );
  433. }
  434. if (!array_key_exists($type, $this->_paramMap)) {
  435. $type = 'object';
  436. }
  437. $paramType = $this->_paramMap[$type];
  438. if (!$isReturn && ( 'null' == $paramType )) {
  439. require_once 'Zend/Json/Server/Exception.php';
  440. throw new Zend_Json_Server_Exception( 'Invalid param type provided ("' . $type . '")' );
  441. }
  442. return $paramType;
  443. }
  444. }