IpAddress.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 DeveloperGarden
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Validate_Ip
  24. */
  25. require_once 'Zend/Validate/Ip.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage DeveloperGarden
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @author Marco Kaiser
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Service_DeveloperGarden_IpLocation_IpAddress
  35. {
  36. /**
  37. * the ip version
  38. * ip v4 = 4
  39. * ip v6 = 6
  40. *
  41. * @var integer
  42. */
  43. private $_version = 4;
  44. /**
  45. * currently supported versions
  46. *
  47. * @var array
  48. */
  49. private $_versionSupported = array(
  50. 4,
  51. //6, not supported yet
  52. );
  53. private $_address = null;
  54. /**
  55. * create ipaddress object
  56. *
  57. * @param string $ip
  58. * @param integer $version
  59. *
  60. * @return Zend_Service_Developergarde_IpLocation_IpAddress
  61. */
  62. public function __construct($ip, $version = 4)
  63. {
  64. $this->setIp($ip)
  65. ->setVersion($version);
  66. }
  67. /**
  68. * sets new ip address
  69. *
  70. * @param string $ip
  71. * @throws Zend_Service_DeveloperGarden_Exception
  72. * @return Zend_Service_DeveloperGarden_IpLocation_IpAddress
  73. */
  74. public function setIp($ip)
  75. {
  76. $validator = new Zend_Validate_Ip();
  77. if (!$validator->isValid($ip)) {
  78. $message = $validator->getMessages();
  79. require_once 'Zend/Service/DeveloperGarden/Exception.php';
  80. throw new Zend_Service_DeveloperGarden_Exception($message['notIpAddress']);
  81. }
  82. $this->_address = $ip;
  83. return $this;
  84. }
  85. /**
  86. * returns the current address
  87. *
  88. * @return string
  89. */
  90. public function getIp()
  91. {
  92. return $this->_address;
  93. }
  94. /**
  95. * sets new ip version
  96. *
  97. * @param integer $version
  98. * @throws Zend_Service_DeveloperGarden_Exception
  99. * @return Zend_Service_DeveloperGarden_IpLocation_IpAddress
  100. */
  101. public function setVersion($version)
  102. {
  103. if (!in_array($version, $this->_versionSupported)) {
  104. require_once 'Zend/Service/DeveloperGarden/Exception.php';
  105. throw new Zend_Service_DeveloperGarden_Exception('Ip Version ' . (int)$version . ' is not supported.');
  106. }
  107. $this->_version = $version;
  108. return $this;
  109. }
  110. /**
  111. * returns the ip version
  112. *
  113. * @return integer
  114. */
  115. public function getVersion()
  116. {
  117. return $this->_version;
  118. }
  119. }