Condition에 Date 추가.

Condition에 logic 단축 명령 추가
This commit is contained in:
2018-03-28 03:19:05 +09:00
parent e739b7a39f
commit 6913fe0313
2 changed files with 84 additions and 1 deletions
+7 -1
View File
@@ -10,7 +10,13 @@ abstract class Condition{
return $conditionChain;
}
$className = 'sammo\\Event\\Condition\\'.$conditionChain[0];
$key = $conditionChain[0];
if(\array_key_exists(strtolower($key), Condition\Logic::AVAILABLE_LOGIC_NAME)){
//logic 단축 명령.
return new Condition\Logic($key, array_slice($conditionChain, 1));
}
$className = 'sammo\\Event\\Condition\\'.$key;
if(class_exists($className)){
$args = [];
+77
View File
@@ -0,0 +1,77 @@
<?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__]
];
}
}