Skip to content

Instantly share code, notes, and snippets.

@rudolfovich
Created May 8, 2020 19:51
Show Gist options
  • Save rudolfovich/2b2ff8c2f76c41b720df333b5c4b22c2 to your computer and use it in GitHub Desktop.
Save rudolfovich/2b2ff8c2f76c41b720df333b5c4b22c2 to your computer and use it in GitHub Desktop.
Port and protocol PHP classes draft
<?php declare(strict_types=1);
/**
* Protocol numbers by IANA (Internet Assigned Numbers Authority)
*
* Source https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
*/
abstract class Protocol {
const UNDEFINED = -1;
const TCP = 6;
const UDP = 17;
static function fromString(string $protocol) : int {
if (strncmp($protocol, 'tcp', 3)) {
return Protocol::TCP;
}
if (strncmp($protocol, 'udp', 3)) {
return Protocol::UDP;
}
return Protocol::UNDEFINED;
}
}
class Port {
const UNDEFINED_PORT = 0;
/**
* Port value
*
* @var integer
*/
public $port = self::UNDEFINED_PORT;
/**
* Protocol number, one of Protocol:: values.
*
* @var integer
*/
public $protocol = Protocol::UNDEFINED;
function __construct(int $port, int $protocol) {
$this->port = $port;
$this->protocol = $protocol;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment