Merge pull request #3 from sikofitt/1.x

Full php 8 support
This commit is contained in:
Eric 2021-01-21 09:18:22 -08:00 committed by GitHub
commit 42b2840aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 49 deletions

View File

@ -12,9 +12,7 @@ return PhpCsFixer\Config::create()
[
'@PSR2' => true,
'@PHP70Migration' => true,
'@PHP70Migration:risky' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'header_comment' => ['header' => $header],
'ordered_class_elements' => true,
'ordered_imports' => true,

View File

@ -3,7 +3,7 @@
"description": "Generates dummy mac addresses",
"type": "library",
"require": {
"php": ">=7.3",
"php": "^8.0",
"ext-json": "*",
"ext-sodium": "*"
},
@ -16,7 +16,7 @@
"friendsofphp/php-cs-fixer": "^2.18",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5",
"symfony/console": "^4.4|^5.0"
"symfony/console": "^5.2"
},
"autoload-dev": {
"psr-4": {

View File

@ -34,16 +34,20 @@ use Symfony\Component\Console\Style\SymfonyStyle;
class GenerateMacCommand extends Command
{
private const SEPARATOR_NAMES = [
'colon',
'dash',
'none',
];
public function configure(): void
{
$this
->setName('generate-mac')
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Generate {count} mac addresses.')
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output in this format instead of a string. [json,plain,string]')
->addOption('separator', 's', InputOption::VALUE_REQUIRED, 'The separator to use for mac addresses.')
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output in this format instead of a string. [json, plain, string]')
->addOption('separator', 's', InputOption::VALUE_REQUIRED, 'The separator to use for mac addresses. [colon, dash, none]')
;
parent::configure();
}
/**
@ -61,9 +65,9 @@ class GenerateMacCommand extends Command
throw new RuntimeException('$count should be a positive number greater than zero.');
}
$separator = strtolower($input->getOption('separator') ?? 'colon');
$separatorName = strtolower($input->getOption('separator') ?? 'colon');
if (!\in_array($separator, ['colon','none','dash'], true)) {
if (!\in_array($separatorName, self::SEPARATOR_NAMES, true)) {
throw new InvalidArgumentException('Separator must be one of "colon", "none", or "dash"');
}
@ -71,23 +75,21 @@ class GenerateMacCommand extends Command
$io = new SymfonyStyle($input, $output);
switch ($separator) {
case 'colon':
default:
$separator = Mac::SEPARATOR_COLON;
break;
case 'none':
$separator = Mac::SEPARATOR_NONE;
break;
case 'dash':
$separator = Mac::SEPARATOR_DASH;
break;
}
$separator = match($separatorName) {
'colon' => Mac::SEPARATOR_COLON,
'dash' => Mac::SEPARATOR_DASH,
'none' => Mac::SEPARATOR_NONE,
default => Mac::SEPARATOR_COLON,
};
$mac = new Mac($separator);
$macAddresses = $mac->getMacAddresses($count);
if(empty($macAddresses)) {
return Command::FAILURE;
}
switch ($outputFormat) {
case 'string':
default:
@ -102,6 +104,6 @@ class GenerateMacCommand extends Command
break;
}
return 0;
return Command::SUCCESS;
}
}

View File

@ -24,6 +24,7 @@ class Mac
public const SEPARATOR_COLON = 0;
public const SEPARATOR_DASH = 1;
public const SEPARATOR_NONE = 2;
/**
* Private mac address prefixes that are used
* internally or with virtual machines and containers.
@ -68,22 +69,10 @@ class Mac
protected $isTest = false;
/**
* @var int The mac address separator, can be self::SEPARATOR_*
*/
private $separator;
/**
* @var bool If we care if we get an already used prefix or not.
*/
private $unique;
/**
* Mac constructor.
*
* @param int $separator The mac address separator, one of self::SEPARATOR_*
* @param bool $unique Whether or not we care if we get a non unique prefix.
*/
public function __construct(int $separator = self::SEPARATOR_COLON, bool $unique = true)
public function __construct(private int $separator = self::SEPARATOR_COLON, private bool $unique = true)
{
$this->setUnique($unique);
$this->setSeparator($separator);
@ -196,16 +185,12 @@ class Mac
*/
public function getSeparatorAsString(): string
{
switch ($this->getSeparator()) {
default:
case self::SEPARATOR_COLON:
return ':';
case self::SEPARATOR_DASH:
return '-';
case self::SEPARATOR_NONE:
return '';
}
return match ( $this->getSeparator() ) {
self::SEPARATOR_COLON => ':',
self::SEPARATOR_DASH => '-',
self::SEPARATOR_NONE => '',
default => ':',
};
}
/**
@ -230,11 +215,11 @@ class Mac
*/
private function generateString(string $template): string
{
$bytes = sodium_bin2hex(\random_bytes(32));
$bytes = \sodium_bin2hex(\random_bytes(32));
while (false !== $pos = \strpos($template, 'x')) {
$replacement = $bytes[\random_int(0, \strlen($bytes) -1)];
$template = substr_replace($template, $replacement, $pos, 1);
$template = \substr_replace($template, $replacement, $pos, 1);
}
return $template;
@ -259,6 +244,6 @@ class Mac
*/
private function insertSeparator(string $macAddress): string
{
return implode($this->getSeparatorAsString(), str_split($macAddress, 2));
return \implode($this->getSeparatorAsString(), \str_split($macAddress, 2));
}
}