Files
core/hwe/sammo/Constraint/ReqNationAuxValue.php
T
Hide_D ee8854451c fix: phan 지시에 따라 일부 버그 수정
- null && key_exists 버그
- or assign이 integer 대상이므로 직접 연산
- false 대신 0 입력한 곳 수정
- 자체 Deprecate 처리한 함수 회피
- 초기화되지 않은 [] 확인하여 처리
- sleep은 정수만 받으므로 usleep으로 변경
- 선언하지 않고 그냥 사용하던 member 변수 선언
- boolean operation 순서 틀린 부분 수정
2021-08-12 23:58:20 +09:00

141 lines
3.9 KiB
PHP

<?php
namespace sammo\Constraint;
use \sammo\JosaUtil;
use \sammo\Util;
use \sammo\Json;
/**
* 범용으로 사용 가능한 국가 Aux 변수 검사도구
*/
class ReqNationAuxValue extends Constraint
{
const REQ_VALUES = Constraint::REQ_NATION | Constraint::REQ_ARRAY_ARG;
protected $key;
protected $maxKey;
protected $defaultValue;
protected $reqVal;
protected $comp;
protected $errMsg;
protected $auxVal;
public function checkInputValues(bool $throwExeception = true): bool
{
if (!parent::checkInputValues($throwExeception) && !$throwExeception) {
return false;
}
if (count($this->arg) == 5) {
[$this->key, $this->defaultValue, $comp, $this->reqVal, $this->errMsg] = $this->arg;
if (!in_array($comp, ['>', '>=', '==', '<=', '<', '!=', '===', '!=='])) {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("invalid comparator");
}
} else {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("require key, defaultValue, comp, reqVal, errMsg");
}
$this->comp = $comp;
$this->maxKey = $this->key . '_max';
if (!key_exists('aux', $this->nation)) {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("require aux in nation");
}
if ($this->nation['aux'] === null) {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("invalid aux in nation(null)");
}
if (is_array($this->nation['aux'])) {
$this->auxVal = $this->nation['aux'];
} else if (is_string($this->nation['aux'])) {
$this->auxVal = Json::decode($this->nation['aux']);
} else {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("inavlid aux in nation(type)");
}
if (!key_exists($this->key, $this->auxVal) && $this->defaultValue === null) {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("require {$this->key} in nation['aux']");
}
if (!is_string($this->errMsg)) {
if (!$throwExeception) {
return false;
}
throw new \InvalidArgumentException("{$this->errMsg} must be string");
}
return true;
}
public function test(): bool
{
$this->checkInputValues();
$this->tested = true;
$compList = [
'<'=>function($target, $src){
return ($target < $src);
},
'<='=>function($target, $src){
return ($target <= $src);
},
'=='=>function($target, $src){
return ($target == $src);
},
'!='=>function($target, $src){
return ($target != $src);
},
'==='=>function($target, $src){
return ($target === $src);
},
'!=='=>function($target, $src){
return ($target !== $src);
},
'>='=>function($target, $src){
return ($target >= $src);
},
'>'=>function($target, $src){
return ($target > $src);
},
];
$comp = $compList[$this->comp];
$result = ($comp)($this->auxVal[$this->key]??$this->defaultValue, $this->reqVal);
if ($result === true) {
return true;
}
if($result === true){
return true;
}
$this->reason = $this->errMsg;
return false;
}
}