match-column.php
6 years ago
match-context.php
6 years ago
match.php
6 years ago
preset.php
5 years ago
replace.php
6 years ago
result.php
6 years ago
search-flags.php
5 years ago
search.php
5 years ago
source-flags.php
5 years ago
source-manager.php
5 years ago
source.php
5 years ago
totals.php
6 years ago
result.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex; |
| 4 | |
| 5 | /** |
| 6 | * Contains all information for a search result - a database row that contains matches |
| 7 | */ |
| 8 | class Result { |
| 9 | /** |
| 10 | * Row ID |
| 11 | * |
| 12 | * @var Int |
| 13 | **/ |
| 14 | private $row_id; |
| 15 | |
| 16 | /** |
| 17 | * Source type |
| 18 | * |
| 19 | * @var String |
| 20 | **/ |
| 21 | private $source_type; |
| 22 | |
| 23 | /** |
| 24 | * Source name |
| 25 | * |
| 26 | * @var String |
| 27 | **/ |
| 28 | private $source_name; |
| 29 | |
| 30 | /** |
| 31 | * A title for the result. e.g. post title |
| 32 | * |
| 33 | * @var String |
| 34 | **/ |
| 35 | private $result_title; |
| 36 | |
| 37 | /** |
| 38 | * Array of columns with matches |
| 39 | * |
| 40 | * @var Match_Column[] |
| 41 | **/ |
| 42 | private $columns; |
| 43 | |
| 44 | /** |
| 45 | * Raw data for this result |
| 46 | * |
| 47 | * @var String[] |
| 48 | **/ |
| 49 | private $raw; |
| 50 | |
| 51 | /** |
| 52 | * Array of actions that can be performed on this result |
| 53 | * |
| 54 | * @var String[] |
| 55 | **/ |
| 56 | private $actions = []; |
| 57 | |
| 58 | /** |
| 59 | * Create the result given a row ID, the Search_Source, a set of columns, and the raw database data. |
| 60 | * |
| 61 | * @param Int $row_id Database row ID. |
| 62 | * @param Search_Source $source The search source. |
| 63 | * @param Array $columns Array of Match_Column objects. |
| 64 | * @param Array $raw Raw row data. |
| 65 | */ |
| 66 | public function __construct( $row_id, Search_Source $source, array $columns, $raw ) { |
| 67 | global $wpdb; |
| 68 | |
| 69 | $this->row_id = $row_id; |
| 70 | $this->columns = $columns; |
| 71 | $this->raw = $raw; |
| 72 | $this->source_type = $source->get_type( $raw ); |
| 73 | $this->source_name = $source->get_name( $raw ); |
| 74 | $this->result_title = isset( $raw[ $source->get_title_column() ] ) ? $raw[ $source->get_title_column() ] : false; |
| 75 | /** @psalm-suppress TooManyArguments */ |
| 76 | $this->actions = \apply_filters( 'searchregex_result_actions', $source->get_actions( $this ), $this->source_type, $this ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert the Result to JSON |
| 81 | * |
| 82 | * @return Array JSON |
| 83 | */ |
| 84 | public function to_json() { |
| 85 | $columns = []; |
| 86 | |
| 87 | foreach ( $this->columns as $column ) { |
| 88 | $columns[] = $column->to_json(); |
| 89 | } |
| 90 | |
| 91 | return [ |
| 92 | 'row_id' => $this->row_id, |
| 93 | 'source_type' => $this->source_type, |
| 94 | 'source_name' => $this->source_name, |
| 95 | 'columns' => $columns, |
| 96 | 'actions' => $this->actions, |
| 97 | 'title' => html_entity_decode( $this->result_title ), |
| 98 | 'match_count' => \array_reduce( $columns, function( $carry, $column ) { |
| 99 | return $carry + $column['match_count']; |
| 100 | }, 0 ), |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the Match_Column array |
| 106 | * |
| 107 | * @return Array Match_Column array |
| 108 | */ |
| 109 | public function get_columns() { |
| 110 | return $this->columns; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the raw data |
| 115 | * |
| 116 | * @return String[] Raw data |
| 117 | */ |
| 118 | public function get_raw() { |
| 119 | return $this->raw; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the row ID |
| 124 | * |
| 125 | * @return Int Row ID |
| 126 | */ |
| 127 | public function get_row_id() { |
| 128 | return $this->row_id; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the result source type |
| 133 | * |
| 134 | * @return String |
| 135 | */ |
| 136 | public function get_source_type() { |
| 137 | return $this->source_type; |
| 138 | } |
| 139 | } |
| 140 |