PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/libraries/Zend/Service/Technorati.php

https://bitbucket.org/zhemel/cloudengine
PHP | 1028 lines | 368 code | 87 blank | 573 comment | 27 complexity | d6300d4d0d8327efe07ef96fe4b981bd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  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_Service
  17. * @subpackage Technorati
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Technorati.php 8064 2008-02-16 10:58:39Z thomas $
  21. */
  22. /**
  23. * Zend_Service_Technorati provides an easy, intuitive and object-oriented interface
  24. * for using the Technorati API.
  25. *
  26. * It provides access to all available Technorati API queries
  27. * and returns the original XML response as a friendly PHP object.
  28. *
  29. * @category Zend
  30. * @package Zend_Service
  31. * @subpackage Technorati
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_Technorati
  36. {
  37. /** Base Technorati API URI */
  38. const API_URI_BASE = 'http://api.technorati.com';
  39. /** Query paths */
  40. const API_PATH_COSMOS = '/cosmos';
  41. const API_PATH_SEARCH = '/search';
  42. const API_PATH_TAG = '/tag';
  43. const API_PATH_DAILYCOUNTS = '/dailycounts';
  44. const API_PATH_TOPTAGS = '/toptags';
  45. const API_PATH_BLOGINFO = '/bloginfo';
  46. const API_PATH_BLOGPOSTTAGS = '/blogposttags';
  47. const API_PATH_GETINFO = '/getinfo';
  48. const API_PATH_KEYINFO = '/keyinfo';
  49. /** Prevent magic numbers */
  50. const PARAM_LIMIT_MIN_VALUE = 1;
  51. const PARAM_LIMIT_MAX_VALUE = 100;
  52. const PARAM_DAYS_MIN_VALUE = 1;
  53. const PARAM_DAYS_MAX_VALUE = 180;
  54. const PARAM_START_MIN_VALUE = 1;
  55. /**
  56. * Technorati API key
  57. *
  58. * @var string
  59. * @access protected
  60. */
  61. protected $_apiKey;
  62. /**
  63. * Zend_Rest_Client instance
  64. *
  65. * @var Zend_Rest_Client
  66. * @access protected
  67. */
  68. protected $_restClient;
  69. /**
  70. * Constructs a new Zend_Service_Technorati instance
  71. * and setup character encoding.
  72. *
  73. * @param string $apiKey Your Technorati API key
  74. */
  75. public function __construct($apiKey)
  76. {
  77. iconv_set_encoding('output_encoding', 'UTF-8');
  78. iconv_set_encoding('input_encoding', 'UTF-8');
  79. iconv_set_encoding('internal_encoding', 'UTF-8');
  80. $this->_apiKey = $apiKey;
  81. }
  82. /**
  83. * Cosmos query lets you see what blogs are linking to a given URL.
  84. *
  85. * On the Technorati site, you can enter a URL in the searchbox and
  86. * it will return a list of blogs linking to it.
  87. * The API version allows more features and gives you a way
  88. * to use the cosmos on your own site.
  89. *
  90. * Query options include:
  91. *
  92. * 'type' => (link|weblog)
  93. * optional - A value of link returns the freshest links referencing your target URL.
  94. * A value of weblog returns the last set of unique weblogs referencing your target URL.
  95. * 'limit' => (int)
  96. * optional - adjust the size of your result from the default value of 20
  97. * to between 1 and 100 results.
  98. * 'start' => (int)
  99. * optional - adjust the range of your result set.
  100. * Set this number to larger than zero and you will receive
  101. * the portion of Technorati's total result set ranging from start to start+limit.
  102. * The default start value is 1.
  103. * 'current' => (true|false)
  104. * optional - the default setting of true
  105. * Technorati returns links that are currently on a weblog's homepage.
  106. * Set this parameter to false if you would like to receive all links
  107. * to the given URL regardless of their current placement on the source blog.
  108. * Internally the value is converted in (yes|no).
  109. * 'claim' => (true|false)
  110. * optional - the default setting of FALSE returns no user information
  111. * about each weblog included in the result set when available.
  112. * Set this parameter to FALSE to include Technorati member data
  113. * in the result set when a weblog in your result set
  114. * has been successfully claimed by a member of Technorati.
  115. * Internally the value is converted in (int).
  116. * 'highlight' => (true|false)
  117. * optional - the default setting of TRUE
  118. * highlights the citation of the given URL within the weblog excerpt.
  119. * Set this parameter to FALSE to apply no special markup to the blog excerpt.
  120. * Internally the value is converted in (int).
  121. *
  122. * @param string $url the URL you are searching for. Prefixes http:// and www. are optional.
  123. * @param array $options additional parameters to refine your query
  124. * @return Zend_Service_Technorati_CosmosResultSet
  125. * @throws Zend_Service_Technorati_Exception
  126. * @link http://technorati.com/developers/api/cosmos.html Technorati API: Cosmos Query reference
  127. */
  128. public function cosmos($url, $options = null)
  129. {
  130. static $defaultOptions = array( 'type' => 'link',
  131. 'start' => 1,
  132. 'limit' => 20,
  133. 'current' => 'yes',
  134. 'format' => 'xml',
  135. 'claim' => 0,
  136. 'highlight' => 1,
  137. );
  138. $options['url'] = $url;
  139. $options = $this->_prepareOptions($options, $defaultOptions);
  140. $this->_validateCosmos($options);
  141. $response = $this->_makeRequest(self::API_PATH_COSMOS, $options);
  142. $dom = $this->_convertResponseAndCheckContent($response);
  143. /**
  144. * @see Zend_Service_Technorati_CosmosResultSet
  145. */
  146. require_once 'Zend/Service/Technorati/CosmosResultSet.php';
  147. return new Zend_Service_Technorati_CosmosResultSet($dom, $options);
  148. }
  149. /**
  150. * Search lets you see what blogs contain a given search string.
  151. *
  152. * Query options include:
  153. *
  154. * 'language' => (string)
  155. * optional - a ISO 639-1 two character language code
  156. * to retrieve results specific to that language.
  157. * This feature is currently beta and may not work for all languages.
  158. * 'authority' => (n|a1|a4|a7)
  159. * optional - filter results to those from blogs with at least
  160. * the Technorati Authority specified.
  161. * Technorati calculates a blog's authority by how many people link to it.
  162. * Filtering by authority is a good way to refine your search results.
  163. * There are four settings:
  164. * - n => Any authority: All results.
  165. * - a1 => A little authority: Results from blogs with at least one link.
  166. * - a4 => Some authority: Results from blogs with a handful of links.
  167. * - a7 => A lot of authority: Results from blogs with hundreds of links.
  168. * 'limit' => (int)
  169. * optional - adjust the size of your result from the default value of 20
  170. * to between 1 and 100 results.
  171. * 'start' => (int)
  172. * optional - adjust the range of your result set.
  173. * Set this number to larger than zero and you will receive
  174. * the portion of Technorati's total result set ranging from start to start+limit.
  175. * The default start value is 1.
  176. * 'claim' => (true|false)
  177. * optional - the default setting of FALSE returns no user information
  178. * about each weblog included in the result set when available.
  179. * Set this parameter to FALSE to include Technorati member data
  180. * in the result set when a weblog in your result set
  181. * has been successfully claimed by a member of Technorati.
  182. * Internally the value is converted in (int).
  183. *
  184. * @param string $query the words you are searching for.
  185. * @param array $options additional parameters to refine your query
  186. * @return Zend_Service_Technorati_SearchResultSet
  187. * @throws Zend_Service_Technorati_Exception
  188. * @link http://technorati.com/developers/api/search.html Technorati API: Search Query reference
  189. */
  190. public function search($query, $options = null)
  191. {
  192. static $defaultOptions = array( 'start' => 1,
  193. 'limit' => 20,
  194. 'format' => 'xml',
  195. 'claim' => 0);
  196. $options['query'] = $query;
  197. $options = $this->_prepareOptions($options, $defaultOptions);
  198. $this->_validateSearch($options);
  199. $response = $this->_makeRequest(self::API_PATH_SEARCH, $options);
  200. $dom = $this->_convertResponseAndCheckContent($response);
  201. /**
  202. * @see Zend_Service_Technorati_SearchResultSet
  203. */
  204. require_once 'Zend/Service/Technorati/SearchResultSet.php';
  205. return new Zend_Service_Technorati_SearchResultSet($dom, $options);
  206. }
  207. /**
  208. * Tag lets you see what posts are associated with a given tag.
  209. *
  210. * Query options include:
  211. *
  212. * 'limit' => (int)
  213. * optional - adjust the size of your result from the default value of 20
  214. * to between 1 and 100 results.
  215. * 'start' => (int)
  216. * optional - adjust the range of your result set.
  217. * Set this number to larger than zero and you will receive
  218. * the portion of Technorati's total result set ranging from start to start+limit.
  219. * The default start value is 1.
  220. * 'excerptsize' => (int)
  221. * optional - number of word characters to include in the post excerpts.
  222. * By default 100 word characters are returned.
  223. * 'topexcerptsize' => (int)
  224. * optional - number of word characters to include in the first post excerpt.
  225. * By default 150 word characters are returned.
  226. *
  227. * @param string $tag the tag term you are searching posts for.
  228. * @param array $options additional parameters to refine your query
  229. * @return Zend_Service_Technorati_TagResultSet
  230. * @throws Zend_Service_Technorati_Exception
  231. * @link http://technorati.com/developers/api/tag.html Technorati API: Tag Query reference
  232. */
  233. public function tag($tag, $options = null)
  234. {
  235. static $defaultOptions = array( 'start' => 1,
  236. 'limit' => 20,
  237. 'format' => 'xml',
  238. 'excerptsize' => 100,
  239. 'topexcerptsize' => 150);
  240. $options['tag'] = $tag;
  241. $options = $this->_prepareOptions($options, $defaultOptions);
  242. $this->_validateTag($options);
  243. $response = $this->_makeRequest(self::API_PATH_TAG, $options);
  244. $dom = $this->_convertResponseAndCheckContent($response);
  245. /**
  246. * @see Zend_Service_Technorati_TagResultSet
  247. */
  248. require_once 'Zend/Service/Technorati/TagResultSet.php';
  249. return new Zend_Service_Technorati_TagResultSet($dom, $options);
  250. }
  251. /**
  252. * TopTags provides daily counts of posts containing the queried keyword.
  253. *
  254. * Query options include:
  255. *
  256. * 'days' => (int)
  257. * optional - Used to specify the number of days in the past
  258. * to request daily count data for.
  259. * Can be any integer between 1 and 180, default is 180
  260. *
  261. * @param string $q the keyword query
  262. * @param array $options additional parameters to refine your query
  263. * @return Zend_Service_Technorati_DailyCountsResultSet
  264. * @throws Zend_Service_Technorati_Exception
  265. * @link http://technorati.com/developers/api/dailycounts.html Technorati API: DailyCounts Query reference
  266. */
  267. public function dailyCounts($query, $options = null)
  268. {
  269. static $defaultOptions = array( 'days' => 180,
  270. 'format' => 'xml'
  271. );
  272. $options['q'] = $query;
  273. $options = $this->_prepareOptions($options, $defaultOptions);
  274. $this->_validateDailyCounts($options);
  275. $response = $this->_makeRequest(self::API_PATH_DAILYCOUNTS, $options);
  276. $dom = $this->_convertResponseAndCheckContent($response);
  277. /**
  278. * @see Zend_Service_Technorati_DailyCountsResultSet
  279. */
  280. require_once 'Zend/Service/Technorati/DailyCountsResultSet.php';
  281. return new Zend_Service_Technorati_DailyCountsResultSet($dom);
  282. }
  283. /**
  284. * TopTags provides information on top tags indexed by Technorati.
  285. *
  286. * Query options include:
  287. *
  288. * 'limit' => (int)
  289. * optional - adjust the size of your result from the default value of 20
  290. * to between 1 and 100 results.
  291. * 'start' => (int)
  292. * optional - adjust the range of your result set.
  293. * Set this number to larger than zero and you will receive
  294. * the portion of Technorati's total result set ranging from start to start+limit.
  295. * The default start value is 1.
  296. *
  297. * @param array $options additional parameters to refine your query
  298. * @return Zend_Service_Technorati_TagsResultSet
  299. * @throws Zend_Service_Technorati_Exception
  300. * @link http://technorati.com/developers/api/toptags.html Technorati API: TopTags Query reference
  301. */
  302. public function topTags($options = null)
  303. {
  304. static $defaultOptions = array( 'start' => 1,
  305. 'limit' => 20,
  306. 'format' => 'xml'
  307. );
  308. $options = $this->_prepareOptions($options, $defaultOptions);
  309. $this->_validateTopTags($options);
  310. $response = $this->_makeRequest(self::API_PATH_TOPTAGS, $options);
  311. $dom = $this->_convertResponseAndCheckContent($response);
  312. /**
  313. * @see Zend_Service_Technorati_TagsResultSet
  314. */
  315. require_once 'Zend/Service/Technorati/TagsResultSet.php';
  316. return new Zend_Service_Technorati_TagsResultSet($dom);
  317. }
  318. /**
  319. * BlogInfo provides information on what blog, if any, is associated with a given URL.
  320. *
  321. * @param string $url the URL you are searching for. Prefixes http:// and www. are optional.
  322. * The URL must be recognized by Technorati as a blog.
  323. * @param array $options additional parameters to refine your query
  324. * @return Zend_Service_Technorati_BlogInfoResult
  325. * @throws Zend_Service_Technorati_Exception
  326. * @link http://technorati.com/developers/api/bloginfo.html Technorati API: BlogInfo Query reference
  327. */
  328. public function blogInfo($url, $options = null)
  329. {
  330. static $defaultOptions = array( 'format' => 'xml'
  331. );
  332. $options['url'] = $url;
  333. $options = $this->_prepareOptions($options, $defaultOptions);
  334. $this->_validateBlogInfo($options);
  335. $response = $this->_makeRequest(self::API_PATH_BLOGINFO, $options);
  336. $dom = $this->_convertResponseAndCheckContent($response);
  337. /**
  338. * @see Zend_Service_Technorati_BlogInfoResult
  339. */
  340. require_once 'Zend/Service/Technorati/BlogInfoResult.php';
  341. return new Zend_Service_Technorati_BlogInfoResult($dom);
  342. }
  343. /**
  344. * BlogPostTags provides information on the top tags used by a specific blog.
  345. *
  346. * Query options include:
  347. *
  348. * 'limit' => (int)
  349. * optional - adjust the size of your result from the default value of 20
  350. * to between 1 and 100 results.
  351. * 'start' => (int)
  352. * optional - adjust the range of your result set.
  353. * Set this number to larger than zero and you will receive
  354. * the portion of Technorati's total result set ranging from start to start+limit.
  355. * The default start value is 1.
  356. * Note. This property is not documented.
  357. *
  358. * @param string $url the URL you are searching for. Prefixes http:// and www. are optional.
  359. * The URL must be recognized by Technorati as a blog.
  360. * @param array $options additional parameters to refine your query
  361. * @return Zend_Service_Technorati_TagsResultSet
  362. * @throws Zend_Service_Technorati_Exception
  363. * @link http://technorati.com/developers/api/blogposttags.html Technorati API: BlogPostTags Query reference
  364. */
  365. public function blogPostTags($url, $options = null)
  366. {
  367. static $defaultOptions = array( 'start' => 1,
  368. 'limit' => 20,
  369. 'format' => 'xml'
  370. );
  371. $options['url'] = $url;
  372. $options = $this->_prepareOptions($options, $defaultOptions);
  373. $this->_validateBlogPostTags($options);
  374. $response = $this->_makeRequest(self::API_PATH_BLOGPOSTTAGS, $options);
  375. $dom = $this->_convertResponseAndCheckContent($response);
  376. /**
  377. * @see Zend_Service_Technorati_TagsResultSet
  378. */
  379. require_once 'Zend/Service/Technorati/TagsResultSet.php';
  380. return new Zend_Service_Technorati_TagsResultSet($dom);
  381. }
  382. /**
  383. * GetInfo query tells you things that Technorati knows about a member.
  384. *
  385. * The returned info is broken up into two sections:
  386. * The first part describes some information that the user wants
  387. * to allow people to know about him- or herself.
  388. * The second part of the document is a listing of the weblogs
  389. * that the user has successfully claimed and the information
  390. * that Technorati knows about these weblogs.
  391. *
  392. * @param string $username the Technorati user name you are searching for
  393. * @param array $options additional parameters to refine your query
  394. * @return Zend_Service_Technorati_GetInfoResult
  395. * @throws Zend_Service_Technorati_Exception
  396. * @link http://technorati.com/developers/api/getinfo.html Technorati API: GetInfo reference
  397. */
  398. public function getInfo($username, $options = null)
  399. {
  400. static $defaultOptions = array('format' => 'xml');
  401. $options['username'] = $username;
  402. $options = $this->_prepareOptions($options, $defaultOptions);
  403. $this->_validateGetInfo($options);
  404. $response = $this->_makeRequest(self::API_PATH_GETINFO, $options);
  405. $dom = $this->_convertResponseAndCheckContent($response);
  406. /**
  407. * @see Zend_Service_Technorati_GetInfoResult
  408. */
  409. require_once 'Zend/Service/Technorati/GetInfoResult.php';
  410. return new Zend_Service_Technorati_GetInfoResult($dom);
  411. }
  412. /**
  413. * KeyInfo query provides information on daily usage of an API key.
  414. * Key Info Queries do not count against a key's daily query limit.
  415. *
  416. * A day is defined as 00:00-23:59 Pacific time.
  417. *
  418. * @return Zend_Service_Technorati_KeyInfoResult
  419. * @throws Zend_Service_Technorati_Exception
  420. * @link http://developers.technorati.com/wiki/KeyInfo Technorati API: Key Info reference
  421. */
  422. public function keyInfo()
  423. {
  424. static $defaultOptions = array();
  425. $options = $this->_prepareOptions(array(), $defaultOptions);
  426. // you don't need to validate this request
  427. // because key is the only mandatory element
  428. // and it's already set in #_prepareOptions
  429. $response = $this->_makeRequest(self::API_PATH_KEYINFO, $options);
  430. $dom = $this->_convertResponseAndCheckContent($response);
  431. /**
  432. * @see Zend_Service_Technorati_KeyInfoResult
  433. */
  434. require_once 'Zend/Service/Technorati/KeyInfoResult.php';
  435. return new Zend_Service_Technorati_KeyInfoResult($dom, $this->_apiKey);
  436. }
  437. /**
  438. * Returns Technorati API key.
  439. *
  440. * @return string Technorati API key
  441. */
  442. public function getApiKey()
  443. {
  444. return $this->_apiKey;
  445. }
  446. /**
  447. * Returns a reference to the REST client object in use.
  448. *
  449. * If the reference hasn't being inizialized yet,
  450. * then a new Zend_Rest_Client instance is created.
  451. *
  452. * @return Zend_Rest_Client
  453. */
  454. public function getRestClient()
  455. {
  456. if (is_null($this->_restClient)) {
  457. /**
  458. * @see Zend_Rest_Client
  459. */
  460. require_once 'Zend/Rest/Client.php';
  461. $this->_restClient = new Zend_Rest_Client(self::API_URI_BASE);
  462. }
  463. return $this->_restClient;
  464. }
  465. /**
  466. * Sets Technorati API key.
  467. *
  468. * Be aware that this function doesn't validate the key.
  469. * The key is validated as soon as the first API request is sent.
  470. * If the key is invalid, the API request method will throw
  471. * a Zend_Service_Technorati_Exception exception with Invalid Key message.
  472. *
  473. * @param string $key Technorati API Key
  474. * @return void
  475. * @link http://technorati.com/developers/apikey.html How to get your Technorati API Key
  476. */
  477. public function setApiKey($key)
  478. {
  479. $this->_apiKey = $key;
  480. return $this;
  481. }
  482. /**
  483. * Validates Cosmos query options.
  484. *
  485. * @param array $options
  486. * @return void
  487. * @throws Zend_Service_Technorati_Exception
  488. * @access protected
  489. */
  490. protected function _validateCosmos(array $options)
  491. {
  492. static $validOptions = array('key', 'url',
  493. 'type', 'limit', 'start', 'current', 'claim', 'highlight', 'format');
  494. // Validate keys in the $options array
  495. $this->_compareOptions($options, $validOptions);
  496. // Validate url (required)
  497. $this->_validateOptionUrl($options);
  498. // Validate limit (optional)
  499. $this->_validateOptionLimit($options);
  500. // Validate start (optional)
  501. $this->_validateOptionStart($options);
  502. // Validate format (optional)
  503. $this->_validateOptionFormat($options);
  504. // Validate type (optional)
  505. $this->_validateInArrayOption('type', $options, array('link', 'weblog'));
  506. // Validate claim (optional)
  507. $this->_validateOptionClaim($options);
  508. // Validate highlight (optional)
  509. $this->_validateIntegerOption('highlight', $options);
  510. // Validate current (optional)
  511. if (isset($options['current'])) {
  512. $tmp = (int) $options['current'];
  513. $options['current'] = $tmp ? 'yes' : 'no';
  514. }
  515. }
  516. /**
  517. * Validates Search query options.
  518. *
  519. * @param array $options
  520. * @return void
  521. * @throws Zend_Service_Technorati_Exception
  522. * @access protected
  523. */
  524. protected function _validateSearch(array $options)
  525. {
  526. static $validOptions = array('key', 'query',
  527. 'language', 'authority', 'limit', 'start', 'claim', 'format');
  528. // Validate keys in the $options array
  529. $this->_compareOptions($options, $validOptions);
  530. // Validate query (required)
  531. $this->_validateMandatoryOption('query', $options);
  532. // Validate authority (optional)
  533. $this->_validateInArrayOption('authority', $options, array('n', 'a1', 'a4', 'a7'));
  534. // Validate limit (optional)
  535. $this->_validateOptionLimit($options);
  536. // Validate start (optional)
  537. $this->_validateOptionStart($options);
  538. // Validate claim (optional)
  539. $this->_validateOptionClaim($options);
  540. // Validate format (optional)
  541. $this->_validateOptionFormat($options);
  542. }
  543. /**
  544. * Validates Tag query options.
  545. *
  546. * @param array $options
  547. * @return void
  548. * @throws Zend_Service_Technorati_Exception
  549. * @access protected
  550. */
  551. protected function _validateTag(array $options)
  552. {
  553. static $validOptions = array('key', 'tag',
  554. 'limit', 'start', 'excerptsize', 'topexcerptsize', 'format');
  555. // Validate keys in the $options array
  556. $this->_compareOptions($options, $validOptions);
  557. // Validate query (required)
  558. $this->_validateMandatoryOption('tag', $options);
  559. // Validate limit (optional)
  560. $this->_validateOptionLimit($options);
  561. // Validate start (optional)
  562. $this->_validateOptionStart($options);
  563. // Validate excerptsize (optional)
  564. $this->_validateIntegerOption('excerptsize', $options);
  565. // Validate excerptsize (optional)
  566. $this->_validateIntegerOption('topexcerptsize', $options);
  567. // Validate format (optional)
  568. $this->_validateOptionFormat($options);
  569. }
  570. /**
  571. * Validates DailyCounts query options.
  572. *
  573. * @param array $options
  574. * @return void
  575. * @throws Zend_Service_Technorati_Exception
  576. * @access protected
  577. */
  578. protected function _validateDailyCounts(array $options)
  579. {
  580. static $validOptions = array('key', 'q',
  581. 'days', 'format');
  582. // Validate keys in the $options array
  583. $this->_compareOptions($options, $validOptions);
  584. // Validate q (required)
  585. $this->_validateMandatoryOption('q', $options);
  586. // Validate format (optional)
  587. $this->_validateOptionFormat($options);
  588. // Validate days (optional)
  589. if (isset($options['days'])) {
  590. $options['days'] = (int) $options['days'];
  591. if ($options['days'] < self::PARAM_DAYS_MIN_VALUE ||
  592. $options['days'] > self::PARAM_DAYS_MAX_VALUE) {
  593. /**
  594. * @see Zend_Service_Technorati_Exception
  595. */
  596. require_once 'Zend/Service/Technorati/Exception.php';
  597. throw new Zend_Service_Technorati_Exception(
  598. "Invalid value '" . $options['days'] . "' for 'days' option");
  599. }
  600. }
  601. }
  602. /**
  603. * Validates GetInfo query options.
  604. *
  605. * @param array $options
  606. * @return void
  607. * @throws Zend_Service_Technorati_Exception
  608. * @access protected
  609. */
  610. protected function _validateGetInfo(array $options)
  611. {
  612. static $validOptions = array('key', 'username',
  613. 'format');
  614. // Validate keys in the $options array
  615. $this->_compareOptions($options, $validOptions);
  616. // Validate username (required)
  617. $this->_validateMandatoryOption('username', $options);
  618. // Validate format (optional)
  619. $this->_validateOptionFormat($options);
  620. }
  621. /**
  622. * Validates TopTags query options.
  623. *
  624. * @param array $options
  625. * @return void
  626. * @throws Zend_Service_Technorati_Exception
  627. * @access protected
  628. */
  629. protected function _validateTopTags(array $options)
  630. {
  631. static $validOptions = array('key',
  632. 'limit', 'start', 'format');
  633. // Validate keys in the $options array
  634. $this->_compareOptions($options, $validOptions);
  635. // Validate limit (optional)
  636. $this->_validateOptionLimit($options);
  637. // Validate start (optional)
  638. $this->_validateOptionStart($options);
  639. // Validate format (optional)
  640. $this->_validateOptionFormat($options);
  641. }
  642. /**
  643. * Validates BlogInfo query options.
  644. *
  645. * @param array $options
  646. * @return void
  647. * @throws Zend_Service_Technorati_Exception
  648. * @access protected
  649. */
  650. protected function _validateBlogInfo(array $options)
  651. {
  652. static $validOptions = array('key', 'url',
  653. 'format');
  654. // Validate keys in the $options array
  655. $this->_compareOptions($options, $validOptions);
  656. // Validate url (required)
  657. $this->_validateOptionUrl($options);
  658. // Validate format (optional)
  659. $this->_validateOptionFormat($options);
  660. }
  661. /**
  662. * Validates TopTags query options.
  663. *
  664. * @param array $options
  665. * @return void
  666. * @throws Zend_Service_Technorati_Exception
  667. * @access protected
  668. */
  669. protected function _validateBlogPostTags(array $options)
  670. {
  671. static $validOptions = array('key', 'url',
  672. 'limit', 'start', 'format');
  673. // Validate keys in the $options array
  674. $this->_compareOptions($options, $validOptions);
  675. // Validate url (required)
  676. $this->_validateOptionUrl($options);
  677. // Validate limit (optional)
  678. $this->_validateOptionLimit($options);
  679. // Validate start (optional)
  680. $this->_validateOptionStart($options);
  681. // Validate format (optional)
  682. $this->_validateOptionFormat($options);
  683. }
  684. /**
  685. * Checks whether an option is in a given array.
  686. *
  687. * @param string $name option name
  688. * @param array $options
  689. * @param array $array array of valid options
  690. * @return void
  691. * @throws Zend_Service_Technorati_Exception
  692. * @access protected
  693. */
  694. protected function _validateInArrayOption($name, $options, array $array)
  695. {
  696. if (isset($options[$name]) && !in_array($options[$name], $array)) {
  697. /**
  698. * @see Zend_Service_Technorati_Exception
  699. */
  700. require_once 'Zend/Service/Technorati/Exception.php';
  701. throw new Zend_Service_Technorati_Exception(
  702. "Invalid value '{$options[$name]}' for '$name' option");
  703. }
  704. }
  705. /**
  706. * Checks whether mandatory $name option exists and it's valid.
  707. *
  708. * @param array $options
  709. * @return void
  710. * @throws Zend_Service_Technorati_Exception
  711. * @access protected
  712. */
  713. protected function _validateMandatoryOption($name, $options)
  714. {
  715. if (!isset($options[$name]) || !trim($options[$name])) {
  716. /**
  717. * @see Zend_Service_Technorati_Exception
  718. */
  719. require_once 'Zend/Service/Technorati/Exception.php';
  720. throw new Zend_Service_Technorati_Exception(
  721. "Empty value for '$name' option");
  722. }
  723. }
  724. /**
  725. * Checks whether $name option is a valid integer and casts it.
  726. *
  727. * @param array $options
  728. * @return void
  729. * @access protected
  730. */
  731. protected function _validateIntegerOption($name, $options)
  732. {
  733. if (isset($options[$name])) {
  734. $options[$name] = (int) $options[$name];
  735. }
  736. }
  737. /**
  738. * Makes and HTTP GET request to given $path with $options.
  739. * HTTP Response is first validated, then returned.
  740. *
  741. * @param string $path
  742. * @param array $options
  743. * @return Zend_Http_Response
  744. * @throws Zend_Service_Technorati_Exception on failure
  745. * @access protected
  746. */
  747. protected function _makeRequest($path, $options = array())
  748. {
  749. $restClient = $this->getRestClient();
  750. $restClient->getHttpClient()->resetParameters();
  751. $response = $restClient->restGet($path, $options);
  752. self::_checkResponse($response);
  753. return $response;
  754. }
  755. /**
  756. * Checks whether 'claim' option value is valid.
  757. *
  758. * @param array $options
  759. * @return void
  760. * @access protected
  761. */
  762. protected function _validateOptionClaim(array $options)
  763. {
  764. $this->_validateIntegerOption('claim', $options);
  765. }
  766. /**
  767. * Checks whether 'format' option value is valid.
  768. * Be aware that Zend_Service_Technorati supports only XML as format value.
  769. *
  770. * @param array $options
  771. * @return void
  772. * @throws Zend_Service_Technorati_Exception if 'format' value != XML
  773. * @access protected
  774. */
  775. protected function _validateOptionFormat(array $options)
  776. {
  777. if (isset($options['format']) && $options['format'] != 'xml') {
  778. /**
  779. * @see Zend_Service_Technorati_Exception
  780. */
  781. require_once 'Zend/Service/Technorati/Exception.php';
  782. throw new Zend_Service_Technorati_Exception(
  783. "Invalid value '" . $options['format'] . "' for 'format' option. " .
  784. "Zend_Service_Technorati supports only 'xml'");
  785. }
  786. }
  787. /**
  788. * Checks whether 'limit' option value is valid.
  789. * Value must be an integer greater than PARAM_LIMIT_MIN_VALUE
  790. * and lower than PARAM_LIMIT_MAX_VALUE.
  791. *
  792. * @param array $options
  793. * @return void
  794. * @throws Zend_Service_Technorati_Exception if 'limit' value is invalid
  795. * @access protected
  796. */
  797. protected function _validateOptionLimit(array $options)
  798. {
  799. if (!isset($options['limit'])) return;
  800. $options['limit'] = (int) $options['limit'];
  801. if ($options['limit'] < self::PARAM_LIMIT_MIN_VALUE ||
  802. $options['limit'] > self::PARAM_LIMIT_MAX_VALUE) {
  803. /**
  804. * @see Zend_Service_Technorati_Exception
  805. */
  806. require_once 'Zend/Service/Technorati/Exception.php';
  807. throw new Zend_Service_Technorati_Exception(
  808. "Invalid value '" . $options['limit'] . "' for 'limit' option");
  809. }
  810. }
  811. /**
  812. * Checks whether 'start' option value is valid.
  813. * Value must be an integer greater than 0.
  814. *
  815. * @param array $options
  816. * @return void
  817. * @throws Zend_Service_Technorati_Exception if 'start' value is invalid
  818. * @access protected
  819. */
  820. protected function _validateOptionStart(array $options)
  821. {
  822. if (!isset($options['start'])) return;
  823. $options['start'] = (int) $options['start'];
  824. if ($options['start'] < self::PARAM_START_MIN_VALUE) {
  825. /**
  826. * @see Zend_Service_Technorati_Exception
  827. */
  828. require_once 'Zend/Service/Technorati/Exception.php';
  829. throw new Zend_Service_Technorati_Exception(
  830. "Invalid value '" . $options['start'] . "' for 'start' option");
  831. }
  832. }
  833. /**
  834. * Checks whether 'url' option value exists and is valid.
  835. * 'url' must be a valid HTTP(s) URL.
  836. *
  837. * @param array $options
  838. * @return void
  839. * @throws Zend_Service_Technorati_Exception if 'url' value is invalid
  840. * @access protected
  841. * @todo support for Zend_Uri_Http
  842. */
  843. protected function _validateOptionUrl(array $options)
  844. {
  845. $this->_validateMandatoryOption('url', $options);
  846. }
  847. /**
  848. * Checks XML response content for errors.
  849. *
  850. * @param DomDocument $dom the XML response as a DOM document
  851. * @return void
  852. * @throws Zend_Service_Technorati_Exception
  853. * @link http://technorati.com/developers/api/error.html Technorati API: Error response
  854. * @access protected
  855. */
  856. protected static function _checkErrors(DomDocument $dom)
  857. {
  858. $xpath = new DOMXPath($dom);
  859. $result = $xpath->query("/tapi/document/result/error");
  860. if ($result->length >= 1) {
  861. $error = $result->item(0)->nodeValue;
  862. /**
  863. * @see Zend_Service_Technorati_Exception
  864. */
  865. require_once 'Zend/Service/Technorati/Exception.php';
  866. throw new Zend_Service_Technorati_Exception($error);
  867. }
  868. }
  869. /**
  870. * Converts $response body to a DOM object and checks it.
  871. *
  872. * @param Zend_Http_Response $response
  873. * @return DOMDocument
  874. * @throws Zend_Service_Technorati_Exception if response content contains an error message
  875. * @access protected
  876. */
  877. protected function _convertResponseAndCheckContent(Zend_Http_Response $response)
  878. {
  879. $dom = new DOMDocument();
  880. $dom->loadXML($response->getBody());
  881. self::_checkErrors($dom);
  882. return $dom;
  883. }
  884. /**
  885. * Checks ReST response for errors.
  886. *
  887. * @param Zend_Http_Response $response the ReST response
  888. * @return void
  889. * @throws Zend_Service_Technorati_Exception
  890. * @access protected
  891. */
  892. protected static function _checkResponse(Zend_Http_Response $response)
  893. {
  894. if ($response->isError()) {
  895. /**
  896. * @see Zend_Service_Technorati_Exception
  897. */
  898. require_once 'Zend/Service/Technorati/Exception.php';
  899. throw new Zend_Service_Technorati_Exception(sprintf(
  900. 'Invalid response status code (HTTP/%s %s %s)',
  901. $response->getVersion(), $response->getStatus(), $response->getMessage()));
  902. }
  903. }
  904. /**
  905. * Checks whether user given options are valid.
  906. *
  907. * @param array $options user options
  908. * @param array $validOptions valid options
  909. * @return void
  910. * @throws Zend_Service_Technorati_Exception
  911. * @access protected
  912. */
  913. protected function _compareOptions(array $options, array $validOptions)
  914. {
  915. $difference = array_diff(array_keys($options), $validOptions);
  916. if ($difference) {
  917. /**
  918. * @see Zend_Service_Technorati_Exception
  919. */
  920. require_once 'Zend/Service/Technorati/Exception.php';
  921. throw new Zend_Service_Technorati_Exception(
  922. "The following parameters are invalid: '" .
  923. implode("', '", $difference) . "'");
  924. }
  925. }
  926. /**
  927. * Prepares options for the request
  928. *
  929. * @param array $options user options
  930. * @param array $defaultOptions default options
  931. * @return array Merged array of user and default/required options.
  932. * @access protected
  933. */
  934. protected function _prepareOptions($options, array $defaultOptions)
  935. {
  936. $options = (array) $options; // force cast to convert null to array()
  937. $options['key'] = $this->_apiKey;
  938. $options = array_merge($defaultOptions, $options);
  939. return $options;
  940. }
  941. }