Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Json / Server / Smd / Service.php
blob9cec38cc9160c1d543fc9f1c7e12780c5fb19565
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Json\Server\Smd;
12 use Zend\Json\Server;
13 use Zend\Json\Server\Exception\InvalidArgumentException;
14 use Zend\Json\Server\Smd;
16 /**
17 * Create Service Mapping Description for a method
19 * @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
21 class Service
23 /**#@+
24 * Service metadata
25 * @var string
27 protected $envelope = Smd::ENV_JSONRPC_1;
28 protected $name;
29 protected $return;
30 protected $target;
31 protected $transport = 'POST';
32 /**#@-*/
34 /**
35 * Allowed envelope types
36 * @var array
38 protected $envelopeTypes = array(
39 Smd::ENV_JSONRPC_1,
40 Smd::ENV_JSONRPC_2,
43 /**
44 * Regex for names
45 * @var string
47 protected $nameRegex = '/^[a-z][a-z0-9.\\\\_]+$/i';
49 /**
50 * Parameter option types
51 * @var array
53 protected $paramOptionTypes = array(
54 'name' => 'is_string',
55 'optional' => 'is_bool',
56 'default' => null,
57 'description' => 'is_string',
60 /**
61 * Service params
62 * @var array
64 protected $params = array();
66 /**
67 * Mapping of parameter types to JSON-RPC types
68 * @var array
70 protected $paramMap = array(
71 'any' => 'any',
72 'arr' => 'array',
73 'array' => 'array',
74 'assoc' => 'object',
75 'bool' => 'boolean',
76 'boolean' => 'boolean',
77 'dbl' => 'float',
78 'double' => 'float',
79 'false' => 'boolean',
80 'float' => 'float',
81 'hash' => 'object',
82 'integer' => 'integer',
83 'int' => 'integer',
84 'mixed' => 'any',
85 'nil' => 'null',
86 'null' => 'null',
87 'object' => 'object',
88 'string' => 'string',
89 'str' => 'string',
90 'struct' => 'object',
91 'true' => 'boolean',
92 'void' => 'null',
95 /**
96 * Allowed transport types
97 * @var array
99 protected $transportTypes = array(
100 'POST',
104 * Constructor
106 * @param string|array $spec
107 * @throws InvalidArgumentException if no name provided
109 public function __construct($spec)
111 if (is_string($spec)) {
112 $this->setName($spec);
113 } elseif (is_array($spec)) {
114 $this->setOptions($spec);
117 if (null == $this->getName()) {
118 throw new InvalidArgumentException('SMD service description requires a name; none provided');
123 * Set object state
125 * @param array $options
126 * @return Service
128 public function setOptions(array $options)
130 $methods = get_class_methods($this);
131 foreach ($options as $key => $value) {
132 if ('options' == strtolower($key)) {
133 continue;
135 $method = 'set' . ucfirst($key);
136 if (in_array($method, $methods)) {
137 $this->$method($value);
140 return $this;
144 * Set service name
146 * @param string $name
147 * @return Service
148 * @throws InvalidArgumentException
150 public function setName($name)
152 $name = (string) $name;
153 if (!preg_match($this->nameRegex, $name)) {
154 throw new InvalidArgumentException("Invalid name '{$name} provided for service; must follow PHP method naming conventions");
156 $this->name = $name;
157 return $this;
161 * Retrieve name
163 * @return string
165 public function getName()
167 return $this->name;
171 * Set Transport
173 * Currently limited to POST
175 * @param string $transport
176 * @throws InvalidArgumentException
177 * @return Service
179 public function setTransport($transport)
181 if (!in_array($transport, $this->transportTypes)) {
182 throw new InvalidArgumentException("Invalid transport '{$transport}'; please select one of (" . implode(', ', $this->transportTypes) . ')');
185 $this->transport = $transport;
186 return $this;
190 * Get transport
192 * @return string
194 public function getTransport()
196 return $this->transport;
200 * Set service target
202 * @param string $target
203 * @return Service
205 public function setTarget($target)
207 $this->target = (string) $target;
208 return $this;
212 * Get service target
214 * @return string
216 public function getTarget()
218 return $this->target;
222 * Set envelope type
224 * @param string $envelopeType
225 * @throws InvalidArgumentException
226 * @return Service
228 public function setEnvelope($envelopeType)
230 if (!in_array($envelopeType, $this->envelopeTypes)) {
231 throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'; please specify one of (" . implode(', ', $this->envelopeTypes) . ')');
234 $this->envelope = $envelopeType;
235 return $this;
239 * Get envelope type
241 * @return string
243 public function getEnvelope()
245 return $this->envelope;
249 * Add a parameter to the service
251 * @param string|array $type
252 * @param array $options
253 * @param int|null $order
254 * @throws InvalidArgumentException
255 * @return Service
257 public function addParam($type, array $options = array(), $order = null)
259 if (is_string($type)) {
260 $type = $this->_validateParamType($type);
261 } elseif (is_array($type)) {
262 foreach ($type as $key => $paramType) {
263 $type[$key] = $this->_validateParamType($paramType);
265 } else {
266 throw new InvalidArgumentException('Invalid param type provided');
269 $paramOptions = array(
270 'type' => $type,
272 foreach ($options as $key => $value) {
273 if (in_array($key, array_keys($this->paramOptionTypes))) {
274 if (null !== ($callback = $this->paramOptionTypes[$key])) {
275 if (!$callback($value)) {
276 continue;
279 $paramOptions[$key] = $value;
283 $this->params[] = array(
284 'param' => $paramOptions,
285 'order' => $order,
288 return $this;
292 * Add params
294 * Each param should be an array, and should include the key 'type'.
296 * @param array $params
297 * @return Service
299 public function addParams(array $params)
301 ksort($params);
302 foreach ($params as $options) {
303 if (!is_array($options)) {
304 continue;
306 if (!array_key_exists('type', $options)) {
307 continue;
309 $type = $options['type'];
310 $order = (array_key_exists('order', $options)) ? $options['order'] : null;
311 $this->addParam($type, $options, $order);
313 return $this;
317 * Overwrite all parameters
319 * @param array $params
320 * @return Service
322 public function setParams(array $params)
324 $this->params = array();
325 return $this->addParams($params);
329 * Get all parameters
331 * Returns all params in specified order.
333 * @return array
335 public function getParams()
337 $params = array();
338 $index = 0;
339 foreach ($this->params as $param) {
340 if (null === $param['order']) {
341 if (array_search($index, array_keys($params), true)) {
342 ++$index;
344 $params[$index] = $param['param'];
345 ++$index;
346 } else {
347 $params[$param['order']] = $param['param'];
350 ksort($params);
351 return $params;
355 * Set return type
357 * @param string|array $type
358 * @throws InvalidArgumentException
359 * @return Service
361 public function setReturn($type)
363 if (is_string($type)) {
364 $type = $this->_validateParamType($type, true);
365 } elseif (is_array($type)) {
366 foreach ($type as $key => $returnType) {
367 $type[$key] = $this->_validateParamType($returnType, true);
369 } else {
370 throw new InvalidArgumentException("Invalid param type provided ('" . gettype($type) . "')");
372 $this->return = $type;
373 return $this;
377 * Get return type
379 * @return string|array
381 public function getReturn()
383 return $this->return;
387 * Cast service description to array
389 * @return array
391 public function toArray()
393 $envelope = $this->getEnvelope();
394 $target = $this->getTarget();
395 $transport = $this->getTransport();
396 $parameters = $this->getParams();
397 $returns = $this->getReturn();
399 if (empty($target)) {
400 return compact('envelope', 'transport', 'parameters', 'returns');
403 return $paramInfo = compact('envelope', 'target', 'transport', 'parameters', 'returns');
407 * Return JSON encoding of service
409 * @return string
411 public function toJson()
413 $service = array($this->getName() => $this->toArray());
414 return \Zend\Json\Json::encode($service);
418 * Cast to string
420 * @return string
422 public function __toString()
424 return $this->toJson();
428 * Validate parameter type
430 * @param string $type
431 * @param bool $isReturn
432 * @return string
433 * @throws InvalidArgumentException
435 protected function _validateParamType($type, $isReturn = false)
437 if (!is_string($type)) {
438 throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
441 if (!array_key_exists($type, $this->paramMap)) {
442 $type = 'object';
445 $paramType = $this->paramMap[$type];
446 if (!$isReturn && ('null' == $paramType)) {
447 throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
450 return $paramType;