class-context.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Context; |
| 4 | |
| 5 | /** |
| 6 | * A group of matches within the same area of a column |
| 7 | * |
| 8 | * @phpstan-type ContextJson array{ |
| 9 | * context_id: int, |
| 10 | * type: string |
| 11 | * } |
| 12 | */ |
| 13 | abstract class Context { |
| 14 | /** |
| 15 | * Context ID |
| 16 | * |
| 17 | * @var int |
| 18 | */ |
| 19 | protected int $context_id = 0; |
| 20 | |
| 21 | /** |
| 22 | * Create a Context_String with a given context ID |
| 23 | * |
| 24 | * @param int $context_id Context ID. |
| 25 | */ |
| 26 | public function __construct( int $context_id = 0 ) { |
| 27 | $this->context_id = $context_id; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Set the context ID |
| 32 | * |
| 33 | * @param integer $context_id New context ID. |
| 34 | * @return void |
| 35 | */ |
| 36 | public function set_context_id( $context_id ) { |
| 37 | $this->context_id = $context_id; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Is the context the same type as this context? |
| 42 | * |
| 43 | * @param Context $context Context to compare. |
| 44 | * @return boolean |
| 45 | */ |
| 46 | public function is_equal( Context $context ) { |
| 47 | return $this->get_type() === $context->get_type(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Has this been matched? |
| 52 | * |
| 53 | * @return boolean |
| 54 | */ |
| 55 | public function is_matched() { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get number of matches |
| 61 | * |
| 62 | * @return integer |
| 63 | */ |
| 64 | public function get_match_count() { |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Does this need saving? |
| 70 | * |
| 71 | * @return boolean |
| 72 | */ |
| 73 | public function needs_saving() { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Convert the Context_String to to_json |
| 79 | * |
| 80 | * @return ContextJson |
| 81 | */ |
| 82 | public function to_json() { |
| 83 | return [ |
| 84 | 'context_id' => $this->context_id, |
| 85 | 'type' => $this->get_type(), |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get context type |
| 91 | * |
| 92 | * @return string |
| 93 | */ |
| 94 | abstract public function get_type(); |
| 95 | } |
| 96 |