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-value.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Context\Type; |
| 4 | |
| 5 | use SearchRegex\Context; |
| 6 | |
| 7 | /** |
| 8 | * Context that contains a value. |
| 9 | * |
| 10 | * @phpstan-type ValueContextJson array{context_id: int, type: string, value: string, value_type: string, value_label: string, value_length: int} |
| 11 | */ |
| 12 | class Value extends Context\Context { |
| 13 | const TYPE_VALUE = 'value'; |
| 14 | const MAX_LENGTH = 200; |
| 15 | |
| 16 | /** |
| 17 | * The value |
| 18 | * |
| 19 | * @readonly |
| 20 | * @var string|integer |
| 21 | */ |
| 22 | protected $value; |
| 23 | |
| 24 | /** |
| 25 | * Type of the value |
| 26 | */ |
| 27 | protected string $value_type; |
| 28 | |
| 29 | /** |
| 30 | * Label for the value, if appropriate. |
| 31 | */ |
| 32 | protected string $value_label; |
| 33 | |
| 34 | /** |
| 35 | * Value length. If the value has been cropped this will be longer than the length in `value` |
| 36 | */ |
| 37 | protected int $value_length; |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * @param string|integer $value Value. |
| 43 | * @param string $label Label. |
| 44 | */ |
| 45 | public function __construct( $value, $label = null ) { |
| 46 | parent::__construct(); |
| 47 | |
| 48 | $this->value_type = Context\Value_Type::get( (string) $value ); |
| 49 | $this->value = "$value"; |
| 50 | $this->value_label = $label ?? (string) $value; |
| 51 | $this->value_length = strlen( (string) $value ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Restrict the value to the max length |
| 56 | * |
| 57 | * @param string $value Value to restrict. |
| 58 | * @return string |
| 59 | */ |
| 60 | protected function restrict_value( $value ) { |
| 61 | return mb_substr( $value, 0, self::MAX_LENGTH ); |
| 62 | } |
| 63 | |
| 64 | public function get_type() { |
| 65 | return self::TYPE_VALUE; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get value |
| 70 | * |
| 71 | * @return string|integer |
| 72 | */ |
| 73 | public function get_value() { |
| 74 | return $this->value; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get value type |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function get_value_type() { |
| 83 | return $this->value_type; |
| 84 | } |
| 85 | |
| 86 | public function is_equal( Context\Context $context ) { |
| 87 | if ( parent::is_equal( $context ) && $context instanceof Context\Type\Value ) { |
| 88 | return $this->value === $context->value; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Convert the Context\Type\Text to to_json |
| 96 | * |
| 97 | * @return ValueContextJson JSON |
| 98 | */ |
| 99 | public function to_json() { |
| 100 | $parent_json = parent::to_json(); |
| 101 | |
| 102 | return [ |
| 103 | 'context_id' => $parent_json['context_id'], |
| 104 | 'type' => $parent_json['type'], |
| 105 | 'value' => $this->restrict_value( (string) $this->value ), |
| 106 | 'value_type' => $this->value_type, |
| 107 | 'value_label' => $this->restrict_value( $this->value_label ), |
| 108 | 'value_length' => $this->value_length, |
| 109 | ]; |
| 110 | } |
| 111 | } |
| 112 |