tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Style
/
ConditionalFormatting
/
Wizard
/
DateValue.php
DateValue.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/DateValue.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Exception; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Conditional; |
| 7 | |
| 8 | /** |
| 9 | * @method DateValue yesterday() |
| 10 | * @method DateValue today() |
| 11 | * @method DateValue tomorrow() |
| 12 | * @method DateValue lastSevenDays() |
| 13 | * @method DateValue lastWeek() |
| 14 | * @method DateValue thisWeek() |
| 15 | * @method DateValue nextWeek() |
| 16 | * @method DateValue lastMonth() |
| 17 | * @method DateValue thisMonth() |
| 18 | * @method DateValue nextMonth() |
| 19 | */ |
| 20 | class DateValue extends WizardAbstract implements WizardInterface |
| 21 | { |
| 22 | protected const MAGIC_OPERATIONS = [ |
| 23 | 'yesterday' => Conditional::TIMEPERIOD_YESTERDAY, |
| 24 | 'today' => Conditional::TIMEPERIOD_TODAY, |
| 25 | 'tomorrow' => Conditional::TIMEPERIOD_TOMORROW, |
| 26 | 'lastSevenDays' => Conditional::TIMEPERIOD_LAST_7_DAYS, |
| 27 | 'last7Days' => Conditional::TIMEPERIOD_LAST_7_DAYS, |
| 28 | 'lastWeek' => Conditional::TIMEPERIOD_LAST_WEEK, |
| 29 | 'thisWeek' => Conditional::TIMEPERIOD_THIS_WEEK, |
| 30 | 'nextWeek' => Conditional::TIMEPERIOD_NEXT_WEEK, |
| 31 | 'lastMonth' => Conditional::TIMEPERIOD_LAST_MONTH, |
| 32 | 'thisMonth' => Conditional::TIMEPERIOD_THIS_MONTH, |
| 33 | 'nextMonth' => Conditional::TIMEPERIOD_NEXT_MONTH, |
| 34 | ]; |
| 35 | |
| 36 | protected const EXPRESSIONS = [ |
| 37 | Conditional::TIMEPERIOD_YESTERDAY => 'FLOOR(%s,1)=TODAY()-1', |
| 38 | Conditional::TIMEPERIOD_TODAY => 'FLOOR(%s,1)=TODAY()', |
| 39 | Conditional::TIMEPERIOD_TOMORROW => 'FLOOR(%s,1)=TODAY()+1', |
| 40 | Conditional::TIMEPERIOD_LAST_7_DAYS => 'AND(TODAY()-FLOOR(%s,1)<=6,FLOOR(%s,1)<=TODAY())', |
| 41 | Conditional::TIMEPERIOD_LAST_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(%s,0)<(WEEKDAY(TODAY())+7))', |
| 42 | Conditional::TIMEPERIOD_THIS_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(%s,0)-TODAY()<=7-WEEKDAY(TODAY()))', |
| 43 | Conditional::TIMEPERIOD_NEXT_WEEK => 'AND(ROUNDDOWN(%s,0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(%s,0)-TODAY()<(15-WEEKDAY(TODAY())))', |
| 44 | Conditional::TIMEPERIOD_LAST_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0-1)),YEAR(%s)=YEAR(EDATE(TODAY(),0-1)))', |
| 45 | Conditional::TIMEPERIOD_THIS_MONTH => 'AND(MONTH(%s)=MONTH(TODAY()),YEAR(%s)=YEAR(TODAY()))', |
| 46 | Conditional::TIMEPERIOD_NEXT_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0+1)),YEAR(%s)=YEAR(EDATE(TODAY(),0+1)))', |
| 47 | ]; |
| 48 | |
| 49 | protected string $operator; |
| 50 | |
| 51 | public function __construct(string $cellRange) |
| 52 | { |
| 53 | parent::__construct($cellRange); |
| 54 | } |
| 55 | |
| 56 | protected function operator(string $operator): void |
| 57 | { |
| 58 | $this->operator = $operator; |
| 59 | } |
| 60 | |
| 61 | protected function setExpression(): void |
| 62 | { |
| 63 | $referenceCount = substr_count(self::EXPRESSIONS[$this->operator], '%s'); |
| 64 | $references = array_fill(0, $referenceCount, $this->referenceCell); |
| 65 | $this->expression = sprintf(self::EXPRESSIONS[$this->operator], ...$references); |
| 66 | } |
| 67 | |
| 68 | public function getConditional(): Conditional |
| 69 | { |
| 70 | $this->setExpression(); |
| 71 | |
| 72 | $conditional = new Conditional(); |
| 73 | $conditional->setConditionType(Conditional::CONDITION_TIMEPERIOD); |
| 74 | $conditional->setText($this->operator); |
| 75 | $conditional->setConditions([$this->expression]); |
| 76 | $conditional->setStyle($this->getStyle()); |
| 77 | $conditional->setStopIfTrue($this->getStopIfTrue()); |
| 78 | |
| 79 | return $conditional; |
| 80 | } |
| 81 | |
| 82 | public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface |
| 83 | { |
| 84 | if ($conditional->getConditionType() !== Conditional::CONDITION_TIMEPERIOD) { |
| 85 | throw new Exception('Conditional is not a Date Value CF Rule conditional'); |
| 86 | } |
| 87 | |
| 88 | $wizard = new self($cellRange); |
| 89 | $wizard->style = $conditional->getStyle(); |
| 90 | $wizard->stopIfTrue = $conditional->getStopIfTrue(); |
| 91 | $wizard->operator = $conditional->getText(); |
| 92 | |
| 93 | return $wizard; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param mixed[] $arguments |
| 98 | */ |
| 99 | public function __call(string $methodName, array $arguments): self |
| 100 | { |
| 101 | if (!isset(self::MAGIC_OPERATIONS[$methodName])) { |
| 102 | throw new Exception('Invalid Operation for Date Value CF Rule Wizard'); |
| 103 | } |
| 104 | |
| 105 | $this->operator(self::MAGIC_OPERATIONS[$methodName]); |
| 106 | |
| 107 | return $this; |
| 108 | } |
| 109 | } |
| 110 |