contraint 추가

This commit is contained in:
2018-09-02 13:46:57 +09:00
parent df6b2bd0e8
commit 7283153f44
6 changed files with 164 additions and 13 deletions
+30 -12
View File
@@ -10,10 +10,13 @@ abstract class Constraint{
const REQ_CITY = 0x20;
const REQ_NATION = 0x40;
const REQ_ARG = 0x80;
const REQ_STRING_ARG = self::REQ_ARG | 0x100;
const REQ_INT_ARG = self::REQ_ARG | 0x200;
const REQ_NUMERIC_ARG = self::REQ_ARG | 0x400;
const REQ_ARRAY_ARG = self::REQ_ARG | 0x800;
const REQ_DEST_GENERAL = 0x100;
const REQ_DEST_CITY = 0x200;
const REQ_DEST_NATION = 0x400;
const REQ_STRING_ARG = self::REQ_ARG | 0x1000;
const REQ_INT_ARG = self::REQ_ARG | 0x2000;
const REQ_NUMERIC_ARG = self::REQ_ARG | 0x4000;
const REQ_ARRAY_ARG = self::REQ_ARG | 0x8000;
const REQ_VALUES = 0;
@@ -94,22 +97,37 @@ abstract class Constraint{
public function checkInputValues(bool $throwExeception=true):bool{
$valueType = static::requiredValueType();
if(($valueType&REQ_GENERAL) && $this->general === null){
if(($valueType&static::REQ_GENERAL) && $this->general === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require general');
}
if(($valueType&REQ_CITY) && $this->city === null){
if(($valueType&static::REQ_CITY) && $this->city === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require city');
}
if(($valueType&REQ_NATION) && $this->nation === null){
if(($valueType&static::REQ_NATION) && $this->nation === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require nation');
}
if (!($valueType&REQ_ARG)) {
if(($valueType&static::REQ_DEST_GENERAL) && $this->destGeneral === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require dest general');
}
if(($valueType&static::REQ_DEST_CITY) && $this->destCity === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require dest city');
}
if(($valueType&static::REQ_DEST_NATION) && $this->destNation === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require dest nation');
}
if (!($valueType&static::REQ_ARG)) {
return true;
}
@@ -118,22 +136,22 @@ abstract class Constraint{
throw new \InvalidArgumentException('require arg');
}
if(($valueType&REQ_STRING_ARG) && !is_string($this->arg)){
if(($valueType&static::REQ_STRING_ARG) && !is_string($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require string arg');
}
if(($valueType&REQ_INT_ARG) && !is_int($this->arg)){
if(($valueType&static::REQ_INT_ARG) && !is_int($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require int arg');
}
if(($valueType&REQ_NUMERIC_ARG) && !is_numeric($this->arg)){
if(($valueType&static::REQ_NUMERIC_ARG) && !is_numeric($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require numeric arg');
}
if(!($valueType&REQ_ARRAY_ARG) && !is_array($this->arg)){
if(!($valueType&static::REQ_ARRAY_ARG) && !is_array($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require array arg');
}
+1 -1
View File
@@ -2,7 +2,7 @@
namespace sammo\Constraint;
class NoOpeningPart extends Constraint{
class NoWanderingNation extends Constraint{
const REQ_VALUES = Constraint::REQ_NATION;
public function checkInputValues(bool $throwExeception=true){
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace sammo\Constraint;
class NoWanderingNation extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL|Constraint::REQ_CITY;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('nation', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require nation in general");
}
if(!key_exists('nation', $this->city)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require nation in city");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->city['nation'] == $this->general['nation']){
return true;
}
$this->reason = "아국이 아닙니다.";
return false;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class ReqGeneralGold extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL|Constraint::REQ_NUMERIC_ARG;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('gold', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require gold in general");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->general['gold'] < $this->arg){
return true;
}
$this->reason = "자금이 모자랍니다.";
return false;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class ReqGeneralRice extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL|Constraint::REQ_NUMERIC_ARG;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('rice', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require rice in general");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->general['rice'] < $this->arg){
return true;
}
$this->reason = "군량이 모자랍니다.";
return false;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class NoWanderingNation extends Constraint{
const REQ_VALUES = Constraint::REQ_CITY;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('supply', $this->city)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require supply in city");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->city['supply']){
return true;
}
$this->reason = "고립된 도시입니다.";
return false;
}
}