IpAddress.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007-2008 by Vladimir A. Altuchov                       *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     class IpAddress implements Stringable
00016     {
00017         private $longIp = null;
00018         
00022         public static function create($ip)
00023         {
00024             return new self($ip);
00025         }
00026         
00027         public function __construct($ip)
00028         {
00029             $this->setIp($ip);
00030         }
00031         
00035         public function setIp($ip)
00036         {
00037             $long = ip2long($ip);
00038             
00039             if ($long === false)
00040                 throw new WrongArgumentException('wrong ip given');
00041             
00042             $this->longIp = $long;
00043             
00044             return $this;
00045         }
00046         
00047         public function getLongIp()
00048         {
00049             return $this->longIp;
00050         }
00051         
00052         public function toString()
00053         {
00054             return long2ip($this->longIp);
00055         }
00056         
00057         public function toSignedInt()
00058         {
00059             return TypesUtils::unsignedToSigned($this->longIp);
00060         }
00061     }
00062 ?>