파일 위치를 twe에서 hwe로 옮김.

This commit is contained in:
2018-04-01 01:23:44 +09:00
parent 1ddd26c8a1
commit 25ec9a2579
201 changed files with 0 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace sammo\Event\Condition;
class ConstBool extends \sammo\Event\Condition{
private $fixedResult = true;
public function __construct(bool $value){
$this->fixedResult = $value;
}
public function eval($env=null){
return [
'value'=>$this->fixedResult,
'chain'=>['ConstBool']
];
}
}
+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__]
];
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace sammo\Event\Condition;
class Interval extends sammo\Event\Condition{
//TODO:구현
public function __construct(...$args){
throw new \BadMethodCallException('Not Yet Implmented.');
//from Year, from Month, Interval, to Year, to Month
}
public function eval($env=null){
throw new \BadMethodCallException('Not Yet Implmented.');
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace sammo\Event\Condition;
class Logic extends sammo\Event\Condition{
private $mode = 'and';
private $conditions = [];
const AVAILABLE_LOGIC_NAME = [
'not'=>false,
'and'=>true,
'or'=>true,
'xor'=>true
];
public function __construct(string $mode, ...$conditions){
$mode = strtolower($mode);
if(!array_key_exists($mode, self::AVAILABLE_LOGIC_NAME)){
throw new \InvalidArgumentException('첫번째 인자는 not, and, or, xor 중 하나여야 합니다.');
}
if(!self::AVAILABLE_LOGIC_NAME[$mode] && count($conditions)>1){
throw new \InvalidArgumentException('조건을 하나만 받을 수 있습니다.');
}
$this->mode = $mode;
$this->conditions = $conditions;
}
public function eval($env=null){
switch($this->$mode){
case 'not':
return $this->logicNot($env);
case 'and':
return $this->logicAnd($env);
case 'or':
return $this->logicOr($env);
case 'xor':
return $this->logicXor($env);
}
throw new \InvalidArgumentException('올바르지 않은 mode.');
}
private function logicNot($env){
$sub = self::_eval($this->conditions[0], $env);
$result['value'] = !$result['value'];
$result['chain'][] = 'not';
return $result;
}
private function logicAnd($env){
$value = true;
$chain = [];
foreach($this->conditions as $cond){
$sub = self::_eval($cond, $env);
$chain[] = $sub['chain'];
if(!$sub['value']){
$result['value'] = false;
break;
}
}
return [
'value'=>$value,
'chain'=>[$chain, 'and']
];
}
private function logicOr($env){
$value = false;
$chain = [];
foreach($this->conditions as $cond){
$sub = self::_eval($cond, $env);
$chain[] = $sub['chain'];
if($sub['value']){
$result['value'] = true;
break;
}
}
return [
'value'=>$value,
'chain'=>[$chain, 'or']
];
}
private function logicXor($env){
$value = false;
$chain = [];
foreach($this->conditions as $cond){
$sub = self::_eval($cond, $env);
$chain[] = $sub['chain'];
$value ^= $sub['value'];
}
return [
'value'=>$value,
'chain'=>[$chain, 'xor']
];
}
}