使用Laravel中的动态数据生成自定义验证规则

I have an application which contains products with

  1. dynamic dimensions. eg. 2D/3D products
  2. preset sizes eg. 2x2, 3x3
  3. custom size inputs AxB (user input)

These configurations are stored in the model and database.

I need to validate the size input.

This is an example of what I have written in my model. How do I go about converting this to a valid validation rule?

function validSizeString($size_string) {
        $size_array = explode('x', $size_string);
        $options = $this->getAttribute('options');
        $size_type = $this->getAttribute('size_type');
        if($size_type == self::SIZE_TYPE_2D && count($size_array) != 2){
            return false;
        } elseif($size_type == self::SIZE_TYPE_3D && count($size_array) != 3) {
            return false;
        } elseif ( !in_array($size_type, [self::SIZE_TYPE_2D, self::SIZE_TYPE_3D])) {
            return false;
        }
        if(isset($options->preset_sizes_on) && $options->preset_sizes_on){
            if(in_array($size_string, $options->preset_sizes) ){
                return true;
            }
        }
}

Custom size validation logic

if(isset($options->custom_size_on) && $options->custom_size_on){
        $within_limits = function($value, $type) use ($options) {
            if($value <= $options->{"max_.$type"} && $value >= $options->{"min_.$type"} ){
                return true;
            }
            return false;
        };
        if ( !$within_limits($size_array[0], 'width') ) {
            return false;
        }
        if ( !$within_limits($size_array[1], 'height') ) {
            return false;
        }
        if($size_type == self::SIZE_TYPE_3D) {
            if( !$within_limits($size_array[2], 'length') ) {
                return false;
            }
        }
        return true;
    }