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