class-date-value.php
6 months ago
class-integer-value.php
6 months ago
class-key-value.php
6 months ago
class-member-value.php
6 months ago
class-string-value.php
6 months ago
class-date-value.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Modifier\Value; |
| 4 | |
| 5 | use SearchRegex\Schema; |
| 6 | use SearchRegex\Search; |
| 7 | use SearchRegex\Modifier; |
| 8 | use SearchRegex\Source; |
| 9 | use SearchRegex\Context; |
| 10 | |
| 11 | /** |
| 12 | * Modify a date column |
| 13 | * |
| 14 | * @phpstan-type DateModifierOption array{ |
| 15 | * column?: string, |
| 16 | * operation?: 'set'|'increment'|'decrement', |
| 17 | * value?: string|int, |
| 18 | * unit?: 'second'|'minute'|'hour'|'day'|'week'|'month'|'year' |
| 19 | * } |
| 20 | */ |
| 21 | class Date_Value extends Modifier\Modifier { |
| 22 | const UNITS = [ 'second', 'minute', 'hour', 'day', 'week', 'month', 'year' ]; |
| 23 | |
| 24 | /** |
| 25 | * Date value |
| 26 | */ |
| 27 | private ?int $value = null; |
| 28 | |
| 29 | /** |
| 30 | * Units of the value |
| 31 | * |
| 32 | * @var 'second'|'minute'|'hour'|'day'|'week'|'month'|'year' |
| 33 | */ |
| 34 | private string $unit = 'hour'; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param DateModifierOption $option Date modification options. |
| 40 | * @param Schema\Column $schema Schema. |
| 41 | */ |
| 42 | public function __construct( $option, Schema\Column $schema ) { |
| 43 | parent::__construct( $option, $schema ); |
| 44 | |
| 45 | $this->operation = 'set'; |
| 46 | if ( isset( $option['operation'] ) && in_array( $option['operation'], [ 'set', 'increment', 'decrement' ], true ) ) { |
| 47 | $this->operation = $option['operation']; |
| 48 | } |
| 49 | |
| 50 | if ( isset( $option['value'] ) ) { |
| 51 | if ( $this->operation === 'set' ) { |
| 52 | $timestamp = strtotime( (string) $option['value'] ); |
| 53 | $this->value = $timestamp !== false ? $timestamp : 0; |
| 54 | } else { |
| 55 | $this->value = intval( $option['value'], 10 ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if ( isset( $option['unit'] ) && in_array( $option['unit'], self::UNITS, true ) && $this->operation !== 'set' ) { |
| 60 | $this->unit = $option['unit']; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function to_json() { |
| 65 | $parent_json = parent::to_json(); |
| 66 | |
| 67 | return [ |
| 68 | 'column' => $parent_json['column'], |
| 69 | 'source' => $parent_json['source'], |
| 70 | 'operation' => $this->operation, |
| 71 | 'value' => $this->value, |
| 72 | 'unit' => $this->unit, |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Perform a date operation - add or subtract |
| 78 | * |
| 79 | * @param int $value1 Value 1. |
| 80 | * @param int $value2 Value 2. |
| 81 | * @return integer |
| 82 | */ |
| 83 | private function perform_operation( $value1, $value2 ) { |
| 84 | if ( $this->operation === 'increment' ) { |
| 85 | return $value1 + $value2; |
| 86 | } |
| 87 | |
| 88 | return $value1 - $value2; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Apply a unit calculation to a date |
| 93 | * |
| 94 | * @param string $unit Unit. |
| 95 | * @param integer $date Date. |
| 96 | * @return integer |
| 97 | */ |
| 98 | private function get_changed_date( $unit, $date ) { |
| 99 | $hour = intval( gmdate( 'G', $date ), 10 ); |
| 100 | $minute = intval( gmdate( 'i', $date ), 10 ); |
| 101 | $second = intval( gmdate( 's', $date ), 10 ); |
| 102 | $month = intval( gmdate( 'n', $date ), 10 ); |
| 103 | $day = intval( gmdate( 'j', $date ), 10 ); |
| 104 | $year = intval( gmdate( 'Y', $date ), 10 ); |
| 105 | |
| 106 | if ( $this->value === null ) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | if ( $unit === 'second' ) { |
| 111 | $second = $this->perform_operation( $second, $this->value ); |
| 112 | } elseif ( $unit === 'minute' ) { |
| 113 | $minute = $this->perform_operation( $minute, $this->value ); |
| 114 | } elseif ( $unit === 'hour' ) { |
| 115 | $hour = $this->perform_operation( $hour, $this->value ); |
| 116 | } elseif ( $unit === 'day' ) { |
| 117 | $day = $this->perform_operation( $day, $this->value ); |
| 118 | } elseif ( $unit === 'week' ) { |
| 119 | $day = $this->perform_operation( $day, $this->value * 7 ); |
| 120 | } elseif ( $unit === 'month' ) { |
| 121 | $month = $this->perform_operation( $month, $this->value ); |
| 122 | } elseif ( $unit === 'year' ) { |
| 123 | $year = $this->perform_operation( $year, $this->value ); |
| 124 | } |
| 125 | |
| 126 | return mktime( $hour, $minute, $second, $month, $day, $year ); |
| 127 | } |
| 128 | |
| 129 | public function perform( $row_id, $row_value, Source\Source $source, Search\Column $column, array $raw, $save_mode ) { |
| 130 | // Go through contexts and find the matching action that modifies it |
| 131 | if ( count( $column->get_contexts() ) === 1 ) { |
| 132 | $context = $column->get_contexts()[0]; |
| 133 | if ( ! $context instanceof Context\Type\Value ) { |
| 134 | return $column; |
| 135 | } |
| 136 | |
| 137 | $date = $this->value; |
| 138 | $value = $context->get_value(); |
| 139 | $mysql_date = mysql2date( 'U', (string) $value ); |
| 140 | |
| 141 | if ( $this->operation === 'increment' || $this->operation === 'decrement' ) { |
| 142 | $date = $this->get_changed_date( $this->unit, intval( $mysql_date, 10 ) ); |
| 143 | } |
| 144 | |
| 145 | if ( $date !== $value && $date !== null ) { |
| 146 | $context = new Context\Type\Replace( $value ); |
| 147 | $date = gmdate( 'Y-m-d H:i:s', $date ); |
| 148 | $context->set_replacement( $date, $source->convert_result_value( $this->schema, $date ) ); |
| 149 | $column->set_contexts( [ $context ] ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $column; |
| 154 | } |
| 155 | } |
| 156 |