/** * {@inheritdoc} */ public function __construct($value) { if (!self::validate($value)) { throw new \InvalidArgumentException(sprintf('The passed value "%s" does not look like an IP.', $value)); } parent::__construct($value); }
/** * Does IP Address Match? * * @param \Darsyn\IP\IP $clientIp * @return boolean */ protected function doesIpAddressMatch(IP $clientIp) { foreach ($this->ipAddresses as $ipAddress) { try { $cidr = 128; if (preg_match('#^(.+)/([1-9]\\d{0,2})$#', $ipAddress, $matches)) { $ipAddress = $matches[1]; $cidr = (int) $matches[2]; } $ipAddress = new IP($ipAddress); if ($ipAddress->inRange($clientIp, min($ipAddress->isVersion(IP::VERSION_4) ? $cidr + 96 : $cidr, 128))) { return true; } } catch (\InvalidArgumentException $e) { continue; } } return false; }
/** * Test: Is private use * * @test * @dataProvider privateUseIpAddresses * @param string $ip * @param bool $isPrivateUse */ public function isPrivateUse($ip, $isPrivateUse) { $ip = new IP($ip); $this->assertEquals($isPrivateUse, $ip->isPrivateUse()); }
/** * IP Converts to PHP Value * * @test * @covers \Darsyn\IP\Doctrine\IpType::convertToPHPValue * @access public * @return void */ public function ipConvertsToPHPValue() { $ip = new IP('12.34.56.78'); $dbIp = $this->type->convertToPHPValue($ip->getBinary(), $this->platform); $this->assertInstanceOf('Darsyn\\IP\\IP', $dbIp); $this->assertEquals('12.34.56.78', $dbIp->getShortAddress()); }