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-column.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-column.php
214 lines
1 <?php
2
3 namespace SearchRegex\Search;
4
5 use SearchRegex\Context;
6
7 class Column {
8 const CONTEXT_LIMIT = 20;
9
10 /**
11 * Column ID
12 *
13 * @readonly
14 */
15 private string $column_id;
16
17 /**
18 * Column label
19 *
20 * @readonly
21 */
22 private string $column_label;
23
24 /**
25 * Array of match contexts
26 *
27 * @var Context\Context[]
28 */
29 private array $contexts = [];
30
31 /**
32 * Total number of match contexts
33 */
34 private int $context_count;
35
36 /**
37 * Total number of matches
38 */
39 private int $match_count;
40
41 /**
42 * Raw data for this column
43 *
44 * @readonly
45 * @var array<string, mixed>
46 */
47 private array $raw;
48
49 /**
50 * Create a Match Column, which contains an array of Context\Type\Text items for a particular database column.
51 *
52 * @param string $column_id Column ID.
53 * @param string $column_label Descriptive column label, shown to the user.
54 * @param array<Context\Context> $contexts Contexts.
55 * @param array<string, mixed> $raw Raw data.
56 */
57 public function __construct( $column_id, $column_label, array $contexts, array $raw ) {
58 $this->match_count = 0;
59 $this->column_id = $column_id;
60 $this->column_label = $column_label;
61 $this->raw = $raw;
62 $this->context_count = 0;
63
64 $this->set_contexts( $contexts );
65 }
66
67 /**
68 * Get contexts
69 *
70 * @return array<Context\Context>
71 */
72 public function get_contexts() {
73 return $this->contexts;
74 }
75
76 /**
77 * Add contexts if:
78 * - there are no contexts and it is unmatched
79 * - the contexts match
80 *
81 * The aim is to ensure that we either have all matches, or one unmatched
82 *
83 * @param array<Context\Context> $contexts Contexts.
84 * @return void
85 */
86 public function add_contexts_if_matching( array $contexts ) {
87 $this->add_contexts( $contexts );
88
89 // Ensure if we have any matches then there are no unmatched
90 $matched = array_filter(
91 $this->contexts, fn( $context ) => $context->is_matched()
92 );
93
94 if ( count( $matched ) > 0 ) {
95 // Remove unmatched
96 $this->set_contexts( $matched );
97 }
98 }
99
100 /**
101 * Add contexts to the column, ensuring we don't have duplicates
102 *
103 * @param array<Context\Context> $contexts Contexts.
104 * @return void
105 */
106 public function add_contexts( array $contexts ) {
107 foreach ( $contexts as $context ) {
108 $found = false;
109
110 // Remove duplicates
111 foreach ( $this->contexts as $existing ) {
112 if ( $existing->is_equal( $context ) ) {
113 $found = true;
114 break;
115 }
116 }
117
118 if ( ! $found ) {
119 $context->set_context_id( count( $this->contexts ) );
120
121 $this->contexts[] = $context;
122 $this->match_count += $context->get_match_count();
123 }
124 }
125
126 $this->context_count = count( $this->contexts );
127 $this->contexts = array_slice( $this->contexts, 0, self::CONTEXT_LIMIT );
128 }
129
130 /**
131 * Set the context array
132 *
133 * @param Context\Context[] $contexts Array of contexts.
134 * @return void
135 */
136 public function set_contexts( array $contexts ) {
137 $this->contexts = [];
138 $this->add_contexts( $contexts );
139 }
140
141 /**
142 * Convert the Context\Type\Text to JSON
143 *
144 * @return array{column_id: string, column_label: string, contexts: array<int, mixed>, context_count: int, match_count: int} JSON data
145 */
146 public function to_json() {
147 return [
148 'column_id' => $this->column_id,
149 'column_label' => $this->column_label,
150 'contexts' => array_map(
151 fn( $item ) => $item->to_json(),
152 $this->contexts
153 ),
154 'context_count' => $this->context_count,
155 'match_count' => $this->match_count,
156 ];
157 }
158
159 /**
160 * Get column ID
161 *
162 * @return string Column ID
163 */
164 public function get_column_id() {
165 return $this->column_id;
166 }
167
168 /**
169 * Get match count
170 *
171 * @return int Match count
172 */
173 public function get_match_count() {
174 return $this->match_count;
175 }
176
177 /**
178 * Get contexts that have changed
179 *
180 * @param array<string, mixed> $_raw Raw data.
181 * @return array<Context\Context>
182 */
183 public function get_changes( array $_raw ) {
184 return array_values(
185 array_filter(
186 $this->contexts, fn( $context ) => $context->needs_saving()
187 )
188 );
189 }
190
191 /**
192 * Get contexts that have not changed
193 *
194 * @param array<string, mixed> $_raw Raw data.
195 * @return array<Context\Context>
196 */
197 public function get_same( array $_raw ) {
198 return array_values(
199 array_filter(
200 $this->contexts, fn( $context ) => ! $context->needs_saving()
201 )
202 );
203 }
204
205 /**
206 * Get the value for this column
207 *
208 * @return string
209 */
210 public function get_value() {
211 return array_values( $this->raw )[0];
212 }
213 }
214