PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / wpcli / WPCLICommandPresenter.php

WPCLICommandPresenter.php in 404 Solution trunk, at includes/wpcli/WPCLICommandPresenter.php

101 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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