PluginProbe ʕ •ᴥ•ʔ
Search Regex / 3.4.3
Search Regex v3.4.3
3.4.3 trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / includes / search / class-result.php
search-regex / includes / search Last commit date
class-column.php 6 months ago class-flags.php 6 months ago class-preset.php 1 month ago class-result.php 1 month ago class-search.php 6 months ago class-text.php 6 months ago class-totals.php 6 months ago
class-result.php
186 lines
1 <?php
2
3 namespace SearchRegex\Search;
4
5 use SearchRegex\Source;
6 use SearchRegex\Search;
7
8 /**
9 * Contains all information for a search result - a database row that contains matches
10 *
11 * @phpstan-type ResultJson array{
12 * row_id: int,
13 * source_type: string,
14 * source_name: string,
15 * columns: array<int, mixed>,
16 * actions: string[],
17 * title: string,
18 * match_count: int
19 * }
20 *
21 * @phpstan-type ResultUpdates array<string, array{
22 * change: mixed,
23 * same: mixed
24 * }>
25 */
26 class Result {
27 /**
28 * Row ID
29 */
30 private int $row_id;
31
32 /**
33 * Source type
34 */
35 private string $source_type;
36
37 /**
38 * Source name
39 */
40 private string $source_name;
41
42 /**
43 * A title for the result. e.g. post title
44 *
45 * @var string|false
46 */
47 private $result_title;
48
49 /**
50 * Array of columns with matches
51 *
52 * @var Search\Column[]
53 */
54 private array $columns;
55
56 /**
57 * Raw data for this result
58 *
59 * @var string[]
60 */
61 private array $raw;
62
63 /**
64 * Array of actions that can be performed on this result
65 *
66 * @var string[]
67 */
68 private array $actions = [];
69
70 /**
71 * Create the result given a row ID, the Source\Source, a set of columns, and the raw database data.
72 *
73 * @param int $row_id Database row ID.
74 * @param Source\Source $source The search source.
75 * @param Search\Column[] $columns Array of Search\Column objects.
76 * @param array<string, mixed> $raw Raw row data.
77 */
78 public function __construct( $row_id, Source\Source $source, array $columns, array $raw ) {
79 $this->row_id = $row_id;
80 $this->columns = $columns;
81 $this->raw = $raw;
82 $this->source_type = $source->get_type( $raw );
83 $this->source_name = $source->get_name( $raw );
84 $this->result_title = $raw[ $source->get_title_column() ] ?? false;
85 $this->actions = \apply_filters( 'searchregex_result_actions', $source->get_actions( $this ), $this->source_type, $this );
86
87 // Get columns as positional values
88 $schema = $source->get_schema_order();
89
90 usort(
91 $this->columns, function ( $a, $b ) use ( $schema ) {
92 $a = $schema[ $a->get_column_id() ];
93 $b = $schema[ $b->get_column_id() ];
94 return $a <=> $b;
95 }
96 );
97 }
98
99 /**
100 * Convert the Result to JSON
101 *
102 * @return ResultJson JSON
103 */
104 public function to_json() {
105 $columns = [];
106
107 foreach ( $this->columns as $column ) {
108 $columns[] = $column->to_json();
109 }
110
111 return [
112 'row_id' => $this->row_id,
113 'source_type' => $this->source_type,
114 'source_name' => $this->source_name,
115
116 'columns' => $columns,
117
118 'actions' => $this->actions,
119 'title' => $this->result_title !== false ? html_entity_decode( $this->result_title, ENT_COMPAT, 'UTF-8' ) : '',
120
121 'match_count' => \array_reduce(
122 $columns, fn( $carry, $column ) => $carry + $column['match_count'], 0
123 ),
124 ];
125 }
126
127 /**
128 * Get the Search\Column array
129 *
130 * @return Search\Column[] Array of Search\Column objects
131 */
132 public function get_columns() {
133 return $this->columns;
134 }
135
136 /**
137 * Get the raw data
138 *
139 * @return string[] Raw data
140 */
141 public function get_raw() {
142 return $this->raw;
143 }
144
145 /**
146 * Get the row ID
147 *
148 * @return int Row ID
149 */
150 public function get_row_id() {
151 return $this->row_id;
152 }
153
154 /**
155 * Get the result source type
156 *
157 * @return string
158 */
159 public function get_source_type() {
160 return $this->source_type;
161 }
162
163 /**
164 * Get array of changes for this result
165 *
166 * @return ResultUpdates
167 */
168 public function get_updates() {
169 $updates = [];
170
171 foreach ( $this->columns as $column ) {
172 $change = $column->get_changes( $this->raw );
173 $same = $column->get_same( $this->raw );
174
175 if ( count( $change ) > 0 ) {
176 $updates[ $column->get_column_id() ] = [
177 'change' => $change,
178 'same' => $same,
179 ];
180 }
181 }
182
183 return $updates;
184 }
185 }
186