PluginProbe ʕ •ᴥ•ʔ
Search Regex / trunk
Search Regex vtrunk
trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / includes / context / type / class-value.php
search-regex / includes / context / type Last commit date
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