match-column.php
6 years ago
match-context.php
6 years ago
match.php
6 years ago
replace.php
6 years ago
result.php
6 years ago
search-flags.php
6 years ago
search.php
6 years ago
source-flags.php
6 years ago
source-manager.php
6 years ago
source.php
6 years ago
result.php
97 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 | /** @var Int */ |
| 10 | private $row_id; |
| 11 | /** @var String */ |
| 12 | private $source_type; |
| 13 | /** @var String */ |
| 14 | private $source_name; |
| 15 | /** @var String */ |
| 16 | private $result_title; |
| 17 | /** @var Match_Column[] */ |
| 18 | private $columns; |
| 19 | /** @var String[] */ |
| 20 | private $raw; |
| 21 | /** @var String[] */ |
| 22 | private $actions = []; |
| 23 | |
| 24 | /** |
| 25 | * Create the result given a row ID, the Search_Source, a set of columns, and the raw database data. |
| 26 | * |
| 27 | * @param Int $row_id Database row ID. |
| 28 | * @param Search_Source $source The search source. |
| 29 | * @param Array $columns Array of Match_Column objects. |
| 30 | * @param Array $raw Raw row data. |
| 31 | */ |
| 32 | public function __construct( $row_id, Search_Source $source, array $columns, $raw ) { |
| 33 | global $wpdb; |
| 34 | |
| 35 | $this->row_id = $row_id; |
| 36 | $this->columns = $columns; |
| 37 | $this->raw = $raw; |
| 38 | $this->source_type = $source->get_type( $raw ); |
| 39 | $this->source_name = $source->get_name( $raw ); |
| 40 | $this->result_title = isset( $raw[ $source->get_title_column() ] ) ? $raw[ $source->get_title_column() ] : false; |
| 41 | /** @psalm-suppress TooManyArguments */ |
| 42 | $this->actions = \apply_filters( 'searchregex_result_actions', $source->get_actions( $this ), $this->source_type, $this ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Convert the Result to JSON |
| 47 | * |
| 48 | * @return Array JSON |
| 49 | */ |
| 50 | public function to_json() { |
| 51 | $columns = []; |
| 52 | |
| 53 | foreach ( $this->columns as $column ) { |
| 54 | $columns[] = $column->to_json(); |
| 55 | } |
| 56 | |
| 57 | return [ |
| 58 | 'row_id' => $this->row_id, |
| 59 | 'source_type' => $this->source_type, |
| 60 | 'source_name' => $this->source_name, |
| 61 | 'columns' => $columns, |
| 62 | 'actions' => $this->actions, |
| 63 | 'title' => $this->result_title, |
| 64 | 'match_count' => \array_reduce( $columns, function( $carry, $column ) { |
| 65 | return $carry + $column['match_count']; |
| 66 | }, 0 ), |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the Match_Column array |
| 72 | * |
| 73 | * @return Array Match_Column array |
| 74 | */ |
| 75 | public function get_columns() { |
| 76 | return $this->columns; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the raw data |
| 81 | * |
| 82 | * @return String[] Raw data |
| 83 | */ |
| 84 | public function get_raw() { |
| 85 | return $this->raw; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the row ID |
| 90 | * |
| 91 | * @return Int Row ID |
| 92 | */ |
| 93 | public function get_row_id() { |
| 94 | return $this->row_id; |
| 95 | } |
| 96 | } |
| 97 |