From 707160d8fc8b1475b3482cf03c1861f448133675 Mon Sep 17 00:00:00 2001 From: Caleb Mazalevskis Date: Wed, 16 Oct 2024 22:46:29 +0800 Subject: [PATCH] Returning aggregate output as an array (#11). Changelog excerpt: - Refactored and added the ability to return aggregated data as an array. --- Changelog.txt | 3 ++ src/Aggregator.php | 93 ++++++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index ed5fd69..2ced0bf 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -13,6 +13,9 @@ Versioning guidelines for SemVer can be found at: https://semver.org/ - [2024.10.14]: Strictened typing for the aggregator. +- [2024.10.16]: Refactored and added the ability to return aggregated data + as an array. + === Version/Release 1.3.4 === PATCH RELEASE. diff --git a/src/Aggregator.php b/src/Aggregator.php index 99d769b..2eef110 100644 --- a/src/Aggregator.php +++ b/src/Aggregator.php @@ -1,6 +1,6 @@ Output = $In; - $this->stripInvalidCharactersAndSort($this->Output); - $this->stripInvalidRangesAndSubs($this->Output); - $this->mergeRanges($this->Output); - if ($this->Mode === 1) { - $this->convertToNetmasks($this->Output); + $this->stripInvalidCharactersAndSort(); + $this->stripInvalidRangesAndSubs(); + $this->mergeRanges(); + if ($this->Mode === 1 || $this->Mode === 3) { + $this->convertToNetmasks(); + } + if ($this->Mode === 2 || $this->Mode === 3) { + $this->Output = explode("\n", $this->Output); } $this->ProcessingTime += microtime(true) - $Begin; return $this->Output; @@ -176,28 +181,29 @@ private function constructTables() /** * Strips invalid characters from lines and sorts entries. * - * @param string * @return void */ - private function stripInvalidCharactersAndSort(&$In) + private function stripInvalidCharactersAndSort() { - $In = explode("\n", strtolower(trim(str_replace("\r", '', $In)))); - $InCount = count($In); + if (!is_array($this->Output)) { + $this->Output = explode("\n", strtolower(trim(str_replace("\r", '', $this->Output)))); + } + $Count = count($this->Output); if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) { - $this->callbacks['newParse']($InCount); + $this->callbacks['newParse']($Count); } if (!empty($this->Results)) { - $this->NumberEntered += $InCount; + $this->NumberEntered += $Count; } - unset($InCount); - $In = array_filter(array_unique(array_map(function ($Line) { + unset($Count); + $this->Output = array_filter(array_unique(array_map(function ($Line) { $Line = preg_replace('~^(?:(?:#| \*|/\*).*|[^\dA-Fa-f:./]*)|(?:[ \t].*|[^\dA-Fa-f:./]*)$~', '', $Line); if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) { $this->callbacks['newTick'](); } return ($Line === '' || preg_match('~[^\da-f:./]+~i', $Line)) ? '' : $Line; - }, $In))); - usort($In, function ($A, $B) { + }, $this->Output))); + usort($this->Output, function ($A, $B) { if (($Pos = strpos($A, '/')) !== false) { $ASize = substr($A, $Pos + 1); $A = substr($A, 0, $Pos); @@ -262,21 +268,20 @@ private function stripInvalidCharactersAndSort(&$In) } return $Compare < 0 ? -1 : 1; }); - $In = implode("\n", $In); + $this->Output = implode("\n", $this->Output); } /** * Strips invalid ranges and subordinates. * - * @param string * @return void */ - private function stripInvalidRangesAndSubs(&$In) + private function stripInvalidRangesAndSubs() { if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) { - $this->callbacks['newParse'](substr_count($In, "\n")); + $this->callbacks['newParse'](substr_count($this->Output, "\n")); } - $In = $Out = "\n" . $In . "\n"; + $this->Output = $Out = "\n" . $this->Output . "\n"; $Offset = 0; $Low = [4 => 1, 6 => 1]; foreach ([ @@ -293,17 +298,17 @@ private function stripInvalidRangesAndSubs(&$In) ] as $Lows) { for ($Iterant = 1; $Iterant < $Lows[2]; $Iterant++) { $Low[$Lows[0]] = $Iterant; - if (preg_match('~\n' . $Lows[1] . '/' . $Iterant . '(?:$|\D)~i', $In)) { + if (preg_match('~\n' . $Lows[1] . '/' . $Iterant . '(?:$|\D)~i', $this->Output)) { break; } } } unset($Lows); - while (($NewLine = strpos($In, "\n", $Offset)) !== false) { + while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) { if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) { $this->callbacks['newTick'](); } - $Line = substr($In, $Offset, $NewLine - $Offset); + $Line = substr($this->Output, $Offset, $NewLine - $Offset); $Offset = $NewLine + 1; if (!$Line) { continue; @@ -352,9 +357,9 @@ private function stripInvalidRangesAndSubs(&$In) } } } - $In = trim($Out); + $this->Output = trim($Out); if (!empty($this->Results)) { - $this->NumberReturned += empty($In) ? 0 : substr_count($In, "\n") + 1; + $this->NumberReturned += empty($this->Output) ? 0 : substr_count($this->Output, "\n") + 1; $this->NumberRejected = $this->NumberEntered - $this->NumberReturned - $this->NumberMerged; $this->NumberAccepted = $this->NumberEntered - $this->NumberRejected; } @@ -363,28 +368,27 @@ private function stripInvalidRangesAndSubs(&$In) /** * Merges ranges. * - * @param string * @return void */ - private function mergeRanges(&$In) + private function mergeRanges() { while (true) { - $Step = $In; + $Step = $this->Output; if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) { $this->callbacks['newParse'](substr_count($Step, "\n")); } - $In = $Out = "\n" . $In . "\n"; + $this->Output = $Out = "\n" . $this->Output . "\n"; $Size = $Offset = 0; $Line = ''; $CIDRs = false; - while (($NewLine = strpos($In, "\n", $Offset)) !== false) { + while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) { if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) { $this->callbacks['newTick'](); } $PrevLine = $Line; $PrevSize = $Size; $PrevCIDRs = $CIDRs; - $Line = substr($In, $Offset, $NewLine - $Offset); + $Line = substr($this->Output, $Offset, $NewLine - $Offset); $Offset = $NewLine + 1; $RangeSep = strpos($Line, '/'); $Size = (int)substr($Line, $RangeSep + 1); @@ -410,8 +414,8 @@ private function mergeRanges(&$In) } } } - $In = trim($Out); - if ($Step === $In) { + $this->Output = trim($Out); + if ($Step === $this->Output) { break; } } @@ -420,21 +424,20 @@ private function mergeRanges(&$In) /** * Optionally converts output to netmask notation. * - * @param string * @return void */ - private function convertToNetmasks(&$In) + private function convertToNetmasks() { if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) { - $this->callbacks['newParse'](substr_count($In, "\n")); + $this->callbacks['newParse'](substr_count($this->Output, "\n")); } - $In = $Out = "\n" . $In . "\n"; + $this->Output = $Out = "\n" . $this->Output . "\n"; $Offset = 0; - while (($NewLine = strpos($In, "\n", $Offset)) !== false) { + while (($NewLine = strpos($this->Output, "\n", $Offset)) !== false) { if (isset($this->callbacks['newTick']) && is_callable($this->callbacks['newTick'])) { $this->callbacks['newTick'](); } - $Line = substr($In, $Offset, $NewLine - $Offset); + $Line = substr($this->Output, $Offset, $NewLine - $Offset); $Offset = $NewLine + 1; if (!$Line || ($RangeSep = strpos($Line, '/')) === false) { continue; @@ -449,6 +452,6 @@ private function convertToNetmasks(&$In) } $Out = str_replace("\n" . $Line . "\n", "\n" . $CIDR . '/' . $Size . "\n", $Out); } - $In = trim($Out); + $this->Output = trim($Out); } }