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.
16 * @package Zend_Service
17 * @subpackage LiveDocx
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * @package Zend_Service
26 * @subpackage LiveDocx
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
31 class Zend_Service_LiveDocx
34 * LiveDocx service version
37 const VERSION
= '1.2';
40 * SOAP client used to connect to LiveDocx service
41 * @var Zend_Soap_Client
44 protected $_soapClient;
47 * WSDL of LiveDocx web service
54 * Array of credentials (username and password) to log into backend server
58 protected $_credentials;
61 * Set to true, when session is logged into backend server
70 * Optionally, pass an array of options (or Zend_Config object).
72 * If an option with the key 'soapClient' is provided, that value will be
73 * used to set the internal SOAP client used to connect to the LiveDocx
76 * Use 'soapClient' in the case that you have a dedicated or (locally
77 * installed) licensed LiveDocx server. For example:
80 * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
82 * 'username' => 'myUsername',
83 * 'password' => 'myPassword',
84 * 'soapClient' => new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
89 * Replace the URI of the WSDL in the constructor of Zend_Soap_Client with
90 * that of your dedicated or licensed LiveDocx server.
92 * If you are using the public LiveDocx server, simply pass 'username' and
93 * 'password'. For example:
96 * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
98 * 'username' => 'myUsername',
99 * 'password' => 'myPassword'
104 * If you prefer to not pass the username and password through the
105 * constructor, you can also call the following methods:
108 * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
110 * $phpLiveDocx->setUsername('myUsername')
111 * ->setPassword('myPassword');
114 * Or, if you want to specify your own SoapClient:
117 * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
119 * $phpLiveDocx->setUsername('myUsername')
120 * ->setPassword('myPassword');
122 * $phpLiveDocx->setSoapClient(
123 * new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
127 * @param array|Zend_Config $options
129 * @throws Zend_Service_LiveDocx_Exception
130 * @since LiveDocx 1.0
132 public function __construct($options = null)
134 $this->_credentials
= array();
135 $this->_loggedIn
= false;
137 if ($options instanceof Zend_Config
) {
138 $options = $options->toArray();
141 if (is_array($options)) {
142 $this->setOptions($options);
148 * One or more of username, password, soapClient
151 * @return Zend_Service_LiveDocx
152 * @since LiveDocx 1.2
154 public function setOptions(array $options)
156 foreach ($options as $key => $value) {
157 $method = 'set' . $key;
158 if (method_exists($this, $method)) {
159 $this->$method($value);
167 * Clean up and log out of LiveDocx service
170 * @since LiveDocx 1.0
172 public function __destruct()
174 return $this->logOut();
178 * Init Soap client - connect to SOAP service
180 * @param string $endpoint
181 * @throws Zend_Service_LiveDocx_Exception
183 * @since LiveDocx 1.2
185 protected function _initSoapClient($endpoint)
188 require_once 'Zend/Soap/Client.php';
189 $this->_soapClient
= new Zend_Soap_Client();
190 $this->_soapClient
->setWsdl($endpoint);
191 } catch (Zend_Soap_Client_Exception
$e) {
192 require_once 'Zend/Service/LiveDocx/Exception.php';
193 throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e);
200 * @return Zend_Soap_Client
201 * @since LiveDocx 1.2
203 public function getSoapClient()
205 return $this->_soapClient
;
211 * @param Zend_Soap_Client $soapClient
212 * @return Zend_Service_LiveDocx
213 * @since LiveDocx 1.2
215 public function setSoapClient(Zend_Soap_Client
$soapClient)
217 $this->_soapClient
= $soapClient;
222 * Log in to LiveDocx service
224 * @param string $username
225 * @param string $password
227 * @throws Zend_Service_LiveDocx_Exception
229 * @since LiveDocx 1.2
231 public function logIn()
233 if (!$this->isLoggedIn()) {
234 if (null === $this->getUsername()) {
235 require_once 'Zend/Service/LiveDocx/Exception.php';
236 throw new Zend_Service_LiveDocx_Exception(
237 'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation'
241 if (null === $this->getPassword()) {
242 require_once 'Zend/Service/LiveDocx/Exception.php';
243 throw new Zend_Service_LiveDocx_Exception(
244 'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation'
248 if (null === $this->getSoapClient()) {
249 $this->_initSoapClient($this->_wsdl
);
253 $this->getSoapClient()->LogIn(array(
254 'username' => $this->getUsername(),
255 'password' => $this->getPassword(),
257 $this->_loggedIn
= true;
258 } catch (Exception
$e) {
259 require_once 'Zend/Service/LiveDocx/Exception.php';
260 throw new Zend_Service_LiveDocx_Exception(
261 'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e
266 return $this->_loggedIn
;
270 * Log out of the LiveDocx service
272 * @throws Zend_Service_LiveDocx_Exception
274 * @since LiveDocx 1.2
276 public function logOut()
278 if ($this->isLoggedIn()) {
280 $this->getSoapClient()->LogOut();
281 $this->_loggedIn
= false;
282 } catch (Exception
$e) {
283 require_once 'Zend/Service/LiveDocx/Exception.php';
284 throw new Zend_Service_LiveDocx_Exception(
285 'Cannot log out of LiveDocx service', 0, $e
290 return $this->_loggedIn
;
294 * Return true, if session is currently logged into the backend server
297 * @since LiveDocx 1.2
299 public function isLoggedIn()
301 return $this->_loggedIn
;
307 * @return Zend_Service_LiveDocx
308 * @since LiveDocx 1.0
310 public function setUsername($username)
312 $this->_credentials
['username'] = $username;
319 * @return Zend_Service_LiveDocx
320 * @since LiveDocx 1.0
322 public function setPassword($password)
324 $this->_credentials
['password'] = $password;
329 * Set WSDL of LiveDocx web service
331 * @return Zend_Service_LiveDocx
332 * @since LiveDocx 1.0
334 public function setWsdl($wsdl)
336 $this->_wsdl
= $wsdl;
341 * Return current username
343 * @return string|null
344 * @since LiveDocx 1.0
346 public function getUsername()
348 if (isset($this->_credentials
['username'])) {
349 return $this->_credentials
['username'];
356 * Return current password
358 * @return string|null
359 * @since LiveDocx 1.0
361 public function getPassword()
363 if (isset($this->_credentials
['password'])) {
364 return $this->_credentials
['password'];
371 * Return WSDL of LiveDocx web service
373 * @return Zend_Service_LiveDocx
374 * @since LiveDocx 1.0
376 public function getWsdl()
382 * Return the document format (extension) of a filename
384 * @param string $filename
386 * @since LiveDocx 1.0
388 public function getFormat($filename)
390 return strtolower(substr(strrchr($filename, '.'), 1));
394 * Return the current API version
397 * @since LiveDocx 1.0
399 public function getVersion()
401 return self
::VERSION
;
405 * Compare the current API version with another version
407 * @param string $version (STRING NOT FLOAT)
408 * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version)
409 * @since LiveDocx 1.0
411 public function compareVersion($version)
413 return version_compare($version, $this->getVersion());