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 / modifier / value / class-integer-value.php
search-regex / includes / modifier / value Last commit date
class-date-value.php 5 months ago class-integer-value.php 5 months ago class-key-value.php 5 months ago class-member-value.php 5 months ago class-string-value.php 5 months ago
class-integer-value.php
83 lines
1 <?php
2
3 namespace SearchRegex\Modifier\Value;
4
5 use SearchRegex\Schema;
6 use SearchRegex\Search;
7 use SearchRegex\Source;
8 use SearchRegex\Modifier;
9 use SearchRegex\Context;
10
11 /**
12 * @phpstan-type IntegerModifierOption array{
13 * column?: string,
14 * operation?: 'set'|'increment'|'decrement',
15 * value?: mixed
16 * }
17 */
18 class Integer_Value extends Modifier\Modifier {
19 /**
20 * Modification Value
21 */
22 private ?int $value = null;
23
24 /**
25 * Constructor
26 *
27 * @param IntegerModifierOption $option Integer modification options.
28 * @param Schema\Column $schema Schema.
29 */
30 public function __construct( $option, Schema\Column $schema ) {
31 parent::__construct( $option, $schema );
32
33 if ( isset( $option['value'] ) ) {
34 $this->value = intval( $option['value'], 10 );
35 }
36
37 $this->operation = 'set';
38 if ( isset( $option['operation'] ) && in_array( $option['operation'], [ 'set', 'increment', 'decrement' ], true ) ) {
39 $this->operation = $option['operation'];
40 }
41 }
42
43 public function to_json() {
44 $parent_json = parent::to_json();
45
46 return [
47 'column' => $parent_json['column'],
48 'source' => $parent_json['source'],
49 'operation' => $this->operation,
50 'value' => $this->value,
51 ];
52 }
53
54 public function perform( $row_id, $row_value, Source\Source $source, Search\Column $column, array $raw, $save_mode ) {
55 // Go through contexts and find the matching action that modifies it
56 if ( count( $column->get_contexts() ) === 1 && $this->value !== null ) {
57 $context = $column->get_contexts()[0];
58 if ( ! $context instanceof Context\Type\Value ) {
59 return $column;
60 }
61
62 $value = $context->get_value();
63
64 if ( $this->operation === 'increment' ) {
65 $replacement = intval( $value, 10 ) + $this->value;
66 $context = new Context\Type\Replace( $value );
67 $context->set_replacement( "{$replacement}", $source->convert_result_value( $this->schema, (string) $replacement ) );
68 } elseif ( $this->operation === 'decrement' ) {
69 $replacement = max( 0, intval( $value, 10 ) - $this->value );
70 $context = new Context\Type\Replace( $value );
71 $context->set_replacement( "{$replacement}", $source->convert_result_value( $this->schema, (string) $replacement ) );
72 } elseif ( intval( $this->value, 10 ) !== intval( $value, 10 ) ) {
73 $context = new Context\Type\Replace( $value );
74 $context->set_replacement( "{$this->value}", $source->convert_result_value( $this->schema, (string) $this->value ) );
75 }
76
77 $column->set_contexts( [ $context ] );
78 }
79
80 return $column;
81 }
82 }
83