Skip to content

Commit affd488

Browse files
dependabot[bot]KaiSchwarz-cnic
authored andcommitted
feat(CommandFormatter.php): enhance command sorting and formatting logic
1 parent 8fdfa7e commit affd488

File tree

10 files changed

+345
-132
lines changed

10 files changed

+345
-132
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"squizlabs/php_codesniffer": "^3.7",
4444
"phpunit/phpunit": "^10.5",
4545
"phpstan/phpstan": "^2.0",
46-
"phpstan/phpstan-deprecation-rules": "^2.0",
4746
"phpstan/extension-installer": "^1.4"
4847
},
4948
"autoload": {

composer.lock

Lines changed: 27 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CNR/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use CNIC\CNR\Logger as L;
1313
use CNIC\CNR\SocketConfig;
1414
use CNIC\CNR\Response;
15+
use CNIC\CommandFormatter;
1516

1617
/**
1718
* CNR API Client
@@ -55,8 +56,8 @@ public function request($cmd = [])
5556
{
5657
$mycmd = [];
5758
if (!empty($cmd)) {
58-
// flatten nested api command bulk parameters
59-
$mycmd = $this->flattenCommand($cmd);
59+
// flatten nested api command bulk parameters and sort them
60+
$mycmd = CommandFormatter::flattenCommand($cmd);
6061
// auto convert umlaut names to punycode
6162
$mycmd = $this->autoIDNConvert($mycmd);
6263
}

src/CommandFormatter.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace CNIC;
4+
5+
class CommandFormatter
6+
{
7+
/**
8+
* Get the sorted command array based on priority
9+
*
10+
* @param array<string,string> $command The command array to be sorted
11+
* @return array<string,string> The sorted command array
12+
*/
13+
public static function getSortedCommand(array $command): array
14+
{
15+
$priority = self::getPriorityArray();
16+
17+
// Sort the command array based on priority
18+
uksort($command, function ($a, $b) use ($priority) {
19+
$priorityA = self::findPriority($a, $priority);
20+
$priorityB = self::findPriority($b, $priority);
21+
22+
return $priorityA === $priorityB ? strcmp($a, $b) : $priorityA - $priorityB;
23+
});
24+
25+
return $command;
26+
}
27+
28+
/**
29+
* Flatten API command's nested arrays for easier handling
30+
* @param array<mixed> $cmd API Command
31+
* @return array<mixed>
32+
*/
33+
public static function flattenCommand($cmd)
34+
{
35+
$newcmd = [];
36+
foreach ($cmd as $key => $val) {
37+
if (isset($val)) {
38+
$val = preg_replace("/\r|\n/", "", $val);
39+
$newKey = \strtoupper($key);
40+
if (is_array($val)) {
41+
foreach ($cmd[$key] as $idx => $v) {
42+
$newcmd[$newKey . $idx] = $v;
43+
}
44+
} else {
45+
$newcmd[$newKey] = $val;
46+
}
47+
}
48+
}
49+
50+
// Sort the command array based on priority
51+
return self::getSortedCommand($newcmd);
52+
}
53+
54+
/**
55+
* Format the command array into a plain text string
56+
*
57+
* @param array<string,string> $command The command array to be formatted
58+
* @return string The formatted command string
59+
*/
60+
public static function formatCommand(array $command): string
61+
{
62+
$tmp = "";
63+
64+
foreach ($command as $key => $val) {
65+
$tmp .= "$key = $val\n";
66+
}
67+
return $tmp;
68+
}
69+
70+
/**
71+
* Get the priority array with regex patterns
72+
*
73+
* @return array<string,int> The priority array
74+
*/
75+
private static function getPriorityArray(): array
76+
{
77+
$priority = [
78+
"COMMAND" => 1,
79+
"/^(DOMAIN|DNSZONE|NAMESERVER|ZONE|SUBUSER)[0-9]*$/" => 2,
80+
"/^(PERIOD|ACTION|AUTH|TARGET|X-FEE-COMMAND|RENEWALMODE|LIMIT|WIDE)$/" => 3,
81+
"/^(TRANSFERLOCK|DNSSEC0|X-FEE-AMOUNT|LOG|TYPE|OBJECT|INACTIVE|OBJECTID|OBJECTCLASS|ORDER|ORDERBY|CURRENCYFROM|CURRENCYTO)$/" => 4,
82+
];
83+
84+
$contactTypes = [
85+
"OWNERCONTACT" => 5,
86+
"ADMINCONTACT" => 6,
87+
"TECHCONTACT" => 7,
88+
"BILLINGCONTACT" => 8
89+
];
90+
$contactFields = [
91+
"FIRSTNAME" => 1,
92+
"LASTNAME" => 2,
93+
"ORGANIZATION" => 3,
94+
"STREET" => 4,
95+
"ZIP" => 5,
96+
"CITY" => 6,
97+
"STATE" => 7,
98+
"COUNTRY" => 8,
99+
"PHONE" => 9,
100+
"EMAIL" => 10,
101+
"CONTACT" => 11
102+
];
103+
104+
foreach ($contactTypes as $type => $typePriority) {
105+
foreach ($contactFields as $field => $fieldPriority) {
106+
$priority["/^{$type}[0-9]+{$field}$/"] = ($typePriority * 10) + $fieldPriority;
107+
}
108+
}
109+
110+
return $priority;
111+
}
112+
113+
/**
114+
* Find the priority of a given key
115+
*
116+
* @param string $key The key to find the priority for
117+
* @param array<string,int> $priority The priority array
118+
* @return int The priority value
119+
*/
120+
private static function findPriority(string $key, array $priority): int
121+
{
122+
foreach ($priority as $pattern => $priorityValue) {
123+
// Check if the pattern is a regex or a plain string
124+
if ((substr($pattern, 0, 1) === '/' && preg_match($pattern, $key)) || $pattern === $key) {
125+
return $priorityValue;
126+
}
127+
}
128+
return PHP_INT_MAX;
129+
}
130+
}

0 commit comments

Comments
 (0)