| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
// allow-no-test-found: covered through public WP-CLI command entry points in tests/WPCLICommandsTest.php |
| 8 |
|
| 9 |
/** |
| 10 |
* Presents WP-CLI command results through the WP_CLI output API. |
| 11 |
*/ |
| 12 |
class ABJ_404_Solution_WPCLICommandPresenter { |
| 13 |
|
| 14 |
/** |
| 15 |
* @param array<string, mixed> $result |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function present(array $result): void { |
| 19 |
$this->presentMessages($result, 'warnings', 'warning'); |
| 20 |
$this->presentMessages($result, 'lines', 'line'); |
| 21 |
$type = isset($result['type']) && is_string($result['type']) ? $result['type'] : 'line'; |
| 22 |
if ($type === 'format') { |
| 23 |
$this->presentFormat($result); |
| 24 |
return; |
| 25 |
} |
| 26 |
if ($type === 'content') { |
| 27 |
echo $this->scalarString($result, 'content', ''); |
| 28 |
return; |
| 29 |
} |
| 30 |
if ($type === 'success') { |
| 31 |
\WP_CLI::success($this->scalarString($result, 'message', '')); |
| 32 |
return; |
| 33 |
} |
| 34 |
if ($type === 'error') { |
| 35 |
\WP_CLI::error($this->scalarString($result, 'message', '')); |
| 36 |
} else { |
| 37 |
\WP_CLI::line($this->scalarString($result, 'message', '')); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param array<string, mixed> $result |
| 43 |
* @param string $key |
| 44 |
* @param string $method |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
private function presentMessages(array $result, string $key, string $method): void { |
| 48 |
$messages = isset($result[$key]) && is_array($result[$key]) ? $result[$key] : array(); |
| 49 |
foreach ($messages as $message) { |
| 50 |
if (is_scalar($message)) { |
| 51 |
\WP_CLI::$method((string)$message); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** @param array<string, mixed> $result @return void */ |
| 57 |
private function presentFormat(array $result): void { |
| 58 |
$rows = isset($result['rows']) && is_array($result['rows']) ? $result['rows'] : array(); |
| 59 |
\WP_CLI\Utils\format_items( |
| 60 |
$this->scalarString($result, 'format', 'table'), |
| 61 |
$rows, |
| 62 |
$this->stringList($result, 'fields') |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param array<string, mixed> $result |
| 68 |
* @param string $key |
| 69 |
* @return array<int, string> |
| 70 |
*/ |
| 71 |
private function stringList(array $result, string $key): array { |
| 72 |
$values = isset($result[$key]) && is_array($result[$key]) ? $result[$key] : array(); |
| 73 |
$strings = array(); |
| 74 |
foreach ($values as $value) { |
| 75 |
if (is_scalar($value)) { |
| 76 |
$strings[] = (string)$value; |
| 77 |
} |
| 78 |
} |
| 79 |
return $strings; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param array<string, mixed> $result |
| 84 |
* @param string $key |
| 85 |
* @param string $default |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
private function scalarString(array $result, string $key, string $default): string { |
| 89 |
return isset($result[$key]) && is_scalar($result[$key]) ? (string)$result[$key] : $default; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param string $question |
| 94 |
* @param array<string, mixed> $assocArgs |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function confirm(string $question, array $assocArgs): void { |
| 98 |
\WP_CLI::confirm($question, $assocArgs); |
| 99 |
} |
| 100 |
} |
| 101 |
|