From 6913fe03131249a69e147c5b53176d01ddecfe43 Mon Sep 17 00:00:00 2001 From: hide_d Date: Wed, 28 Mar 2018 03:19:05 +0900 Subject: [PATCH] =?UTF-8?q?Condition=EC=97=90=20Date=20=EC=B6=94=EA=B0=80.?= =?UTF-8?q?=20Condition=EC=97=90=20logic=20=EB=8B=A8=EC=B6=95=20=EB=AA=85?= =?UTF-8?q?=EB=A0=B9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- twe/sammo/Event/Condition.php | 8 +++- twe/sammo/Event/Condition/Date.php | 77 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 twe/sammo/Event/Condition/Date.php diff --git a/twe/sammo/Event/Condition.php b/twe/sammo/Event/Condition.php index 0b66dc23..0694f838 100644 --- a/twe/sammo/Event/Condition.php +++ b/twe/sammo/Event/Condition.php @@ -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 = []; diff --git a/twe/sammo/Event/Condition/Date.php b/twe/sammo/Event/Condition/Date.php new file mode 100644 index 00000000..e1b40c38 --- /dev/null +++ b/twe/sammo/Event/Condition/Date.php @@ -0,0 +1,77 @@ +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__] + ]; + + } +} \ No newline at end of file