API Params 유효성 검증
class Validator {
    private $checkMap;

    public function __construct($requiredMap) {
        $this->checkMap = $requiredMap;
    }

    public function checkValid($data) {
        $return = [];
        $return['type'] = !empty($data['useYn']) ? "setting" : "update";

        if (!empty($data['useYn'])) {
            foreach ($this->checkMap as $key => $set) {
                list($type, $length, $comment, $option, $required) = $this->parseSetting($set);

                if ($required && empty($data[$key])) {
                    return $this->logAndReturn(false, "$comment 값이 누락되었습니다.", "==== $key : $comment 값이 누락되었습니다.");
                }

                if ($type === 'varchar' || $type === 'int') {
                    if (!$this->validateLength($data[$key], $length)) {
                        return $this->logAndReturn(false, '변수 크기가 너무 큽니다.', "==== 데이타 length가 [$comment] $key : $length 보다 작아야 함..=> " . $data[$key]);
                    }

                    if ($type === 'int' && !is_numeric($data[$key])) {
                        return $this->logAndReturn(false, '변수 타입이 이상합니다.', "==== 데이타 가 [$comment] $key : Int 타입이 아닙니다. => " . $data[$key]);
                    }
                }

                if ($type === 'date' && !$this->validateDate($data[$key])) {
                    return $this->logAndReturn(false, '연월일 값이 이상합니다.', "==== 데이타 가 [$comment] $key : date 타입이 아닙니다. => " . $data[$key]);
                }

                if ($type === 'time' && !$this->validateTime($data[$key])) {
                    return $this->logAndReturn(false, '정확한 시간이 아닙니다 (00:00).', "==== 데이타 가 [$comment] $key : time 타입이 아닙니다. => " . $data[$key]);
                }

                if (!empty($option) && !in_array($data[$key], $option)) {
                    return $this->logAndReturn(false, '누락 된 변수가 있습니다.', "==== 허용 데이타 [$comment] $key : " . json_encode($option) . ' . => ' . $data[$key]);
                }

                $return['data'][$key] = $data[$key];
            }
        }

        return $return;
    }

    private function parseSetting($set) {
        $arrSet = explode('|', $set);
        $type = $arrSet[0];
        $length = $arrSet[1];
        $comment = $arrSet[2];
        $option = !empty($arrSet[3]) ? explode(',', $arrSet[3]) : [];
        $required = $arrSet[4];

        return [$type, $length, $comment, $option, $required];
    }

    private function validateLength($value, $length) {
        return strlen($value) <= $length;
    }

    private function validateDate($date) {
        if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
            list($year, $month, $day) = explode('-', $date);
            return checkdate($month, $day, $year);
        }
        return false;
    }

    private function validateTime($time) {
        return preg_match('/^([01]\d|2[0-3]):([0-5]\d)$/', $time);
    }

    private function logAndReturn($success, $msg, $log) {
        log::info($log);
        return ['success' => $success, 'msg' => $msg];
    }
}

예시

// remainSettingMap 예제 설정
$remainSettingMap = [
    'field1' => 'varchar|50|필드1 코멘트||1',
    'field2' => 'int|10|필드2 코멘트||1',
    'field3' => 'date|10|필드3 코멘트||0',
    'field4' => 'time|5|필드4 코멘트||0',
    'field5' => 'varchar|100|필드5 코멘트|option1,option2,option3|1',
];

// 데이터 예시
$data = [
    'useYn' => true,
    'field1' => 'some string',
    'field2' => 123,
    'field3' => '2023-12-31',
    'field4' => '12:34',
    'field5' => 'option1',
];

// Validator 인스턴스 생성 및 데이터 검증
$validator = new Validator($remainSettingMap);
$result = $validator->checkValid($data);

print_r($result);
2 Comments
Jordan Singer
2d
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Non minima ipsum at amet doloremque qui magni, placeat deserunt pariatur itaque laudantium impedit aliquam eligendi repellendus excepturi quibusdam nobis esse accusantium.
2 replies
Brandon Smith
2d
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
James Parsons
1d
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio dolore sed eos sapiente, praesentium.
Santiago Roberts
4d
Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto laborum in corrupti dolorum, quas delectus nobis porro accusantium molestias sequi.