Files
core/twe/sammo/Event/Condition/Date.php
T
Hide_D 6913fe0313 Condition에 Date 추가.
Condition에 logic 단축 명령 추가
2018-03-28 03:19:05 +09:00

77 lines
2.0 KiB
PHP

<?php
namespace sammo\Event\Condition;
class Date extends sammo\Event\Condition{
const AVAILABLE_CMP = [
'=='=>true,
'!='=>true,
'<'=>true,
'>'=>true,
'<='=>true,
'>='=>true,
];
private $cmp;
private $year;
private $month;
//TODO:구현
public function __construct(string $cmp, int $year, int $month){
//Cmp('==', '!=', '<=', '>=', '<', '>'), Year, Month(Optional)
if(!array_key_exists($cmp, self::AVAILABLE_CMP)){
throw new \InvalidArgumentException('올바르지 않은 비교연산자입니다');
}
if($year === null && $month === null){
throw new \InvalidArgumentException('year과 month가 둘다 null일 수 없습니다.');
}
$this->cmp = $cmp;
$this->year = $year;
$this->month = $month;
}
public function eval(array $env=null){
if($env === null){
return [
'value'=>false,
'chain'=>[__CLASS__]
];
}
if($this->year !== null && !isset($env['year'])){
throw new \InvalidArgumentException('env에 year가 없습니다.');
}
if($this->month !== null && !isset($env['month'])){
throw new \InvalidArgumentException('env에 month가 없습니다.');
}
$lhs = [
$this->$year,
$this->month
];
$rhs = [
$this->year!==null?(int)$env['year']:null,
$this->month!==null?(int)$env['month']:null
];
$value = false;
switch($this->cmp){
case '==': $value = ($lhs == $rhs); break;
case '!=': $value = ($lhs != $rhs); break;
case '<=': $value = ($lhs <= $rhs); break;
case '>=': $value = ($lhs >= $rhs); break;
case '<': $value = ($lhs < $rhs); break;
case '>': $value = ($lhs > $rhs); break;
}
return [
'value'=>$value,
'chain'=>[__CLASS__]
];
}
}