AbstractShortener.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_ShortUrl
  17. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  20. */
  21. /**
  22. * @see Zend_Service_Abstract
  23. */
  24. require_once 'Zend/Service/Abstract.php';
  25. /**
  26. * @see Zend_Service_ShortUrl_Shortener
  27. */
  28. require_once 'Zend/Service/ShortUrl/Shortener.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Service_ShortUrl
  32. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Service_ShortUrl_AbstractShortener
  36. extends Zend_Service_Abstract
  37. implements Zend_Service_ShortUrl_Shortener
  38. {
  39. /**
  40. * Base URI of the service
  41. *
  42. * @var string
  43. */
  44. protected $_baseUri = null;
  45. /**
  46. * Checks whether URL to be shortened is valid
  47. *
  48. * @param string $url
  49. * @throws Zend_Service_ShortUrl_Exception When URL is not valid
  50. */
  51. protected function _validateUri($url)
  52. {
  53. require_once 'Zend/Uri.php';
  54. if (!Zend_Uri::check($url)) {
  55. require_once 'Zend/Service/ShortUrl/Exception.php';
  56. throw new Zend_Service_ShortUrl_Exception(sprintf(
  57. 'The url "%s" is not valid and cannot be shortened', $url
  58. ));
  59. }
  60. }
  61. /**
  62. * Verifies that the URL has been shortened by this service
  63. *
  64. * @throws Zend_Service_ShortUrl_Exception If the URL hasn't been shortened by this service
  65. * @param string $shortenedUrl
  66. */
  67. protected function _verifyBaseUri($shortenedUrl)
  68. {
  69. if (strpos($shortenedUrl, $this->_baseUri) !== 0) {
  70. require_once 'Zend/Service/ShortUrl/Exception.php';
  71. throw new Zend_Service_ShortUrl_Exception(sprintf(
  72. 'The url "%s" is not valid for this service and the target cannot be resolved',
  73. $shortenedUrl
  74. ));
  75. }
  76. }
  77. }