class-add.php
5 months ago
class-delete.php
5 months ago
class-empty-type.php
5 months ago
class-matched.php
5 months ago
class-pair.php
5 months ago
class-replace.php
5 months ago
class-text.php
5 months ago
class-value.php
5 months ago
class-pair.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Context\Type; |
| 4 | |
| 5 | use SearchRegex\Context; |
| 6 | |
| 7 | /** |
| 8 | * Context for a keyvalue pair |
| 9 | */ |
| 10 | class Pair extends Context\Context { |
| 11 | const TYPE_PAIR = 'keyvalue'; |
| 12 | |
| 13 | /** |
| 14 | * Key |
| 15 | * |
| 16 | * @readonly |
| 17 | */ |
| 18 | private Context\Context $key; |
| 19 | |
| 20 | /** |
| 21 | * Value |
| 22 | * |
| 23 | * @readonly |
| 24 | */ |
| 25 | private Context\Context $value; |
| 26 | |
| 27 | // @phpstan-ignore constructor.missingParentCall |
| 28 | public function __construct( Context\Context $key, Context\Context $value ) { |
| 29 | $this->key = $key; |
| 30 | $this->value = $value; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the key |
| 35 | * |
| 36 | * @return Context\Context |
| 37 | */ |
| 38 | public function get_key() { |
| 39 | return $this->key; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the value |
| 44 | * |
| 45 | * @return Context\Context |
| 46 | */ |
| 47 | public function get_value() { |
| 48 | return $this->value; |
| 49 | } |
| 50 | |
| 51 | public function get_type() { |
| 52 | return self::TYPE_PAIR; |
| 53 | } |
| 54 | |
| 55 | public function to_json() { |
| 56 | $key = $this->key->to_json(); |
| 57 | $value = $this->value->to_json(); |
| 58 | |
| 59 | unset( $key['context_id'] ); |
| 60 | unset( $value['context_id'] ); |
| 61 | |
| 62 | $parent_json = parent::to_json(); |
| 63 | |
| 64 | return [ |
| 65 | 'context_id' => $parent_json['context_id'], |
| 66 | 'type' => $parent_json['type'], |
| 67 | 'key' => $key, |
| 68 | 'value' => $value, |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | public function is_equal( Context\Context $context ) { |
| 73 | if ( parent::is_equal( $context ) && $context instanceof Context\Type\Pair ) { |
| 74 | return $this->key->is_equal( $context->key ) && $this->value->is_equal( $context->value ); |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | public function is_matched() { |
| 81 | return $this->key->is_matched() || $this->value->is_matched(); |
| 82 | } |
| 83 | |
| 84 | public function needs_saving() { |
| 85 | return $this->key->needs_saving() || $this->value->needs_saving(); |
| 86 | } |
| 87 | } |
| 88 |