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-flags.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Search; |
| 4 | |
| 5 | /** |
| 6 | * Represents flags for a particular search |
| 7 | * |
| 8 | * @phpstan-type SearchFlag 'regex'|'case' |
| 9 | */ |
| 10 | class Flags { |
| 11 | /** |
| 12 | * @var SearchFlag[] |
| 13 | */ |
| 14 | private array $flags = []; |
| 15 | |
| 16 | /** |
| 17 | * Create a Flags object with an array of flag strings |
| 18 | * |
| 19 | * @param SearchFlag[] $flags Array of flag values. |
| 20 | */ |
| 21 | public function __construct( array $flags = [] ) { |
| 22 | $allowed = [ |
| 23 | 'regex', |
| 24 | 'case', |
| 25 | ]; |
| 26 | |
| 27 | $this->flags = array_filter( |
| 28 | $flags, fn( $flag ) => array_search( $flag, $allowed, true ) !== false |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Duplicate a search flag object |
| 34 | * |
| 35 | * @param Flags $flags Flags. |
| 36 | * @return Flags |
| 37 | */ |
| 38 | public static function copy( Flags $flags ) { |
| 39 | return new Flags( $flags->flags ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Is the flag set? |
| 44 | * |
| 45 | * @param string $flag Flag to check. |
| 46 | * @return boolean true if set, false otherwise |
| 47 | */ |
| 48 | public function has_flag( $flag ) { |
| 49 | return in_array( $flag, $this->flags, true ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Is the a regular expression search? |
| 54 | * |
| 55 | * @return boolean true if yes, false otherwise |
| 56 | */ |
| 57 | public function is_regex() { |
| 58 | return $this->has_flag( 'regex' ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Is the a case insensitive search? |
| 63 | * |
| 64 | * @return boolean true if yes, false otherwise |
| 65 | */ |
| 66 | public function is_case_insensitive() { |
| 67 | return $this->has_flag( 'case' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the regex flag |
| 72 | * |
| 73 | * @return void |
| 74 | */ |
| 75 | public function set_regex() { |
| 76 | if ( ! $this->is_regex() ) { |
| 77 | $this->flags[] = 'regex'; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get all the flags |
| 83 | * |
| 84 | * @return SearchFlag[] Array of flags |
| 85 | */ |
| 86 | public function get_flags() { |
| 87 | return $this->flags; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Convert the flags to JSON |
| 92 | * |
| 93 | * @return SearchFlag[] |
| 94 | */ |
| 95 | public function to_json() { |
| 96 | return $this->flags; |
| 97 | } |
| 98 | } |
| 99 |