00001 <?php
00030 class Zend_Json_Server_Smd
00031 {
00032 const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
00033 const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
00034 const SMD_VERSION = '2.0';
00035
00040 protected $_contentType = 'application/json';
00041
00046 protected $_contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
00047
00052 protected $_description;
00053
00058 protected $_dojoCompatible = false;
00059
00064 protected $_envelope = self::ENV_JSONRPC_1;
00065
00070 protected $_envelopeTypes = array(
00071 self::ENV_JSONRPC_1,
00072 self::ENV_JSONRPC_2,
00073 );
00074
00079 protected $_id;
00080
00085 protected $_services = array();
00086
00091 protected $_target;
00092
00097 protected $_transport = 'POST';
00098
00103 protected $_transportTypes = array('POST');
00104
00111 public function setOptions(array $options)
00112 {
00113 $methods = get_class_methods($this);
00114 foreach ($options as $key => $value) {
00115 $method = 'set' . ucfirst($key);
00116 if (in_array($method, $methods)) {
00117 $this->$method($value);
00118 }
00119 }
00120 return $this;
00121 }
00122
00129 public function setTransport($transport)
00130 {
00131 if (!in_array($transport, $this->_transportTypes)) {
00132 require_once 'Zend/Json/Server/Exception.php';
00133 throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s" specified', $transport));
00134 }
00135 $this->_transport = $transport;
00136 return $this;
00137 }
00138
00144 public function getTransport()
00145 {
00146 return $this->_transport;
00147 }
00148
00155 public function setEnvelope($envelopeType)
00156 {
00157 if (!in_array($envelopeType, $this->_envelopeTypes)) {
00158 require_once 'Zend/Json/Server/Exception.php';
00159 throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"', $envelopeType));
00160 }
00161 $this->_envelope = $envelopeType;
00162 return $this;
00163 }
00164
00170 public function getEnvelope()
00171 {
00172 return $this->_envelope;
00173 }
00174
00175
00182 public function setContentType($type)
00183 {
00184 if (!preg_match($this->_contentTypeRegex, $type)) {
00185 require_once 'Zend/Json/Server/Exception.php';
00186 throw new Zend_Json_Server_Exception(sprintf('Invalid content type "%s" specified', $type));
00187 }
00188 $this->_contentType = $type;
00189 return $this;
00190 }
00191
00197 public function getContentType()
00198 {
00199 return $this->_contentType;
00200 }
00201
00208 public function setTarget($target)
00209 {
00210 $this->_target = (string) $target;
00211 return $this;
00212 }
00213
00219 public function getTarget()
00220 {
00221 return $this->_target;
00222 }
00223
00230 public function setId($id)
00231 {
00232 $this->_id = (string) $id;
00233 return $this->_id;
00234 }
00235
00241 public function getId()
00242 {
00243 return $this->_id;
00244 }
00245
00252 public function setDescription($description)
00253 {
00254 $this->_description = (string) $description;
00255 return $this->_description;
00256 }
00257
00263 public function getDescription()
00264 {
00265 return $this->_description;
00266 }
00267
00274 public function setDojoCompatible($flag)
00275 {
00276 $this->_dojoCompatible = (bool) $flag;
00277 return $this;
00278 }
00279
00285 public function isDojoCompatible()
00286 {
00287 return $this->_dojoCompatible;
00288 }
00289
00296 public function addService($service)
00297 {
00298 require_once 'Zend/Json/Server/Smd/Service.php';
00299
00300 if ($service instanceof Zend_Json_Server_Smd_Service) {
00301 $name = $service->getName();
00302 } elseif (is_array($service)) {
00303 $service = new Zend_Json_Server_Smd_Service($service);
00304 $name = $service->getName();
00305 } else {
00306 require_once 'Zend/Json/Server/Exception.php';
00307 throw new Zend_Json_Server_Exception('Invalid service passed to addService()');
00308 }
00309
00310 if (array_key_exists($name, $this->_services)) {
00311 require_once 'Zend/Json/Server/Exception.php';
00312 throw new Zend_Json_Server_Exception('Attempt to register a service already registered detected');
00313 }
00314 $this->_services[$name] = $service;
00315 return $this;
00316 }
00317
00324 public function addServices(array $services)
00325 {
00326 foreach ($services as $service) {
00327 $this->addService($service);
00328 }
00329 return $this;
00330 }
00331
00338 public function setServices(array $services)
00339 {
00340 $this->_services = array();
00341 return $this->addServices($services);
00342 }
00343
00350 public function getService($name)
00351 {
00352 if (array_key_exists($name, $this->_services)) {
00353 return $this->_services[$name];
00354 }
00355 return false;
00356 }
00357
00363 public function getServices()
00364 {
00365 return $this->_services;
00366 }
00367
00374 public function removeService($name)
00375 {
00376 if (array_key_exists($name, $this->_services)) {
00377 unset($this->_services[$name]);
00378 return true;
00379 }
00380 return false;
00381 }
00382
00388 public function toArray()
00389 {
00390 if ($this->isDojoCompatible()) {
00391 return $this->toDojoArray();
00392 }
00393
00394 $transport = $this->getTransport();
00395 $envelope = $this->getEnvelope();
00396 $contentType = $this->getContentType();
00397 $SMDVersion = self::SMD_VERSION;
00398 $service = compact('transport', 'envelope', 'contentType', 'SMDVersion');
00399
00400 if (null !== ($target = $this->getTarget())) {
00401 $service['target'] = $target;
00402 }
00403 if (null !== ($id = $this->getId())) {
00404 $service['id'] = $id;
00405 }
00406
00407 $services = $this->getServices();
00408 if (!empty($services)) {
00409 $service['services'] = array();
00410 foreach ($services as $name => $svc) {
00411 $svc->setEnvelope($envelope);
00412 $service['services'][$name] = $svc->toArray();
00413 }
00414 $service['methods'] = $service['services'];
00415 }
00416
00417 return $service;
00418 }
00419
00425 public function toDojoArray()
00426 {
00427 $SMDVersion = '.1';
00428 $serviceType = 'JSON-RPC';
00429 $service = compact('SMDVersion', 'serviceType');
00430
00431 $target = $this->getTarget();
00432
00433 $services = $this->getServices();
00434 if (!empty($services)) {
00435 $service['methods'] = array();
00436 foreach ($services as $name => $svc) {
00437 $method = array(
00438 'name' => $name,
00439 'serviceURL' => $target,
00440 );
00441 $params = array();
00442 foreach ($svc->getParams() as $param) {
00443 $paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
00444 $params[] = array(
00445 'name' => $paramName,
00446 'type' => $param['type'],
00447 );
00448 }
00449 if (!empty($params)) {
00450 $method['parameters'] = $params;
00451 }
00452 $service['methods'][] = $method;
00453 }
00454 }
00455
00456 return $service;
00457 }
00458
00464 public function toJson()
00465 {
00466 require_once 'Zend/Json.php';
00467 return Zend_Json::encode($this->toArray());
00468 }
00469
00475 public function __toString()
00476 {
00477 return $this->toJson();
00478 }
00479 }
00480