|
| 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