Wrench\Socket\Socket::getNamePart PHP Method

getNamePart() public static method

PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address).
public static getNamePart ( string $name, $part ) : string
$name string (from $this->getName() usually)
return string
    public static function getNamePart($name, $part)
    {
        if (!$name) {
            throw new InvalidArgumentException('Invalid name');
        }
        $parts = explode(':', $name);
        if (count($parts) < 2) {
            throw new SocketException('Could not parse name parts: ' . $name);
        }
        if ($part == self::NAME_PART_PORT) {
            return end($parts);
        } elseif ($part == self::NAME_PART_IP) {
            return implode(':', array_slice($parts, 0, -1));
        } else {
            throw new InvalidArgumentException('Invalid name part');
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * @dataProvider getValidNames
  * @param string $name
  */
 public function testGetNamePart($name, $ip, $port)
 {
     $this->assertEquals($ip, Socket::getNamePart($name, Socket::NAME_PART_IP), 'splits ip correctly');
     $this->assertEquals($port, Socket::getNamePart($name, Socket::NAME_PART_PORT), 'splits port correctly');
 }