forked from devsam/core
내부 코드 사용례에서 General 테이블의 column을 일부만 가져오는 경우가 다수 있습니다. 다만 General 클래스에서 각종 iAction에 해당하는 트리거 함수 호출을 위해서는 모든 column을 다 가져와야 정상 동작이 가능한 만큼, iAction interface를 구현한 General 클래스와, 구현하지 않은 GeneralLite 클래스 둘로 나눕니다. 또한 General 클래스에서는 필요한 column을 다 가져오도록 변경합니다. Reviewed-on: https://storage.hided.net/gitea/devsam/core/pulls/236
131 lines
3.5 KiB
PHP
131 lines
3.5 KiB
PHP
<?php
|
|
namespace sammo;
|
|
|
|
trait LazyVarUpdater{
|
|
protected $raw = [];
|
|
protected $updatedVar = [];
|
|
|
|
function getRaw():array{
|
|
return $this->raw;
|
|
}
|
|
|
|
function getVar(string|\BackedEnum $key){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
return $this->raw[$key];
|
|
}
|
|
|
|
function getVars(string|\BackedEnum ...$keys){
|
|
return array_map([$this, 'getVar'], $keys);
|
|
}
|
|
|
|
function touchVar(string|\BackedEnum $key):bool{
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
if(key_exists($key, $this->raw)){
|
|
return false;
|
|
}
|
|
$this->raw[$key] = null;
|
|
|
|
return true;
|
|
}
|
|
|
|
function setVar(string|\BackedEnum $key, $value){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
return $this->updateVar($key, $value);
|
|
}
|
|
|
|
function updateVar(string|\BackedEnum $key, $value){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
if(($this->raw[$key]??null) === $value){
|
|
return;
|
|
}
|
|
if(!key_exists($key, $this->updatedVar)){
|
|
$this->updatedVar[$key] = true;
|
|
}
|
|
$this->raw[$key] = $value;
|
|
}
|
|
|
|
function updateVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
if($min !== null && $value < $min){
|
|
$value = $min;
|
|
}
|
|
if($max !== null && $value > $max){
|
|
$value = $max;
|
|
}
|
|
$this->updateVar($key, $value);
|
|
}
|
|
|
|
function increaseVar(string|\BackedEnum $key, $value)
|
|
{
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
if($value === 0){
|
|
return;
|
|
}
|
|
$targetValue = $this->raw[$key] + $value;
|
|
$this->updateVar($key, $targetValue);
|
|
}
|
|
|
|
function increaseVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
$targetValue = $this->raw[$key] + $value;
|
|
if($min !== null && $targetValue < $min){
|
|
$targetValue = $min;
|
|
}
|
|
if($max !== null && $targetValue > $max){
|
|
$targetValue = $max;
|
|
}
|
|
$this->updateVar($key, $targetValue);
|
|
}
|
|
|
|
function multiplyVar(string|\BackedEnum $key, $value)
|
|
{
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
if($value === 1){
|
|
return;
|
|
}
|
|
$targetValue = $this->raw[$key] * $value;
|
|
$this->updateVar($key, $targetValue);
|
|
}
|
|
|
|
function multiplyVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){
|
|
if($key instanceof \BackedEnum){
|
|
$key = $key->value;
|
|
}
|
|
$targetValue = $this->raw[$key] * $value;
|
|
if($min !== null && $targetValue < $min){
|
|
$targetValue = $min;
|
|
}
|
|
if($max !== null && $targetValue > $max){
|
|
$targetValue = $max;
|
|
}
|
|
$this->updateVar($key, $targetValue);
|
|
}
|
|
|
|
function getUpdatedValues():array {
|
|
$updateVals = [];
|
|
foreach(array_keys($this->updatedVar) as $key){
|
|
$updateVals[$key] = $this->raw[$key];
|
|
}
|
|
return $updateVals;
|
|
}
|
|
|
|
function flushUpdateValues():void {
|
|
$this->updatedVar = [];
|
|
}
|
|
} |