chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
 
namespace AlibabaCloud\Tea;
 
class Model
{
    protected $_name     = [];
    protected $_required = [];
 
    public function __construct($config = [])
    {
        if (!empty($config)) {
            foreach ($config as $k => $v) {
                $this->{$k} = $v;
            }
        }
    }
 
    public function getName($name = null)
    {
        if (null === $name) {
            return $this->_name;
        }
 
        return isset($this->_name[$name]) ? $this->_name[$name] : $name;
    }
 
    public function toMap()
    {
        $map = get_object_vars($this);
        foreach ($map as $k => $m) {
            if (0 === strpos($k, '_')) {
                unset($map[$k]);
            }
        }
        $res = [];
        foreach ($map as $k => $v) {
            $name       = isset($this->_name[$k]) ? $this->_name[$k] : $k;
            $res[$name] = $v;
        }
 
        return $res;
    }
 
    public function validate()
    {
        $vars = get_object_vars($this);
        foreach ($vars as $k => $v) {
            if (isset($this->_required[$k]) && $this->_required[$k] && empty($v)) {
                throw new \InvalidArgumentException("{$k} is required.");
            }
        }
    }
 
    public static function validateRequired($fieldName, $field, $val = null)
    {
        if (true === $val && null === $field) {
            throw new \InvalidArgumentException($fieldName . ' is required');
        }
    }
 
    public static function validateMaxLength($fieldName, $field, $val = null)
    {
        if (null !== $field && \strlen($field) > (int) $val) {
            throw new \InvalidArgumentException($fieldName . ' is exceed max-length: ' . $val);
        }
    }
 
    public static function validateMinLength($fieldName, $field, $val = null)
    {
        if (null !== $field && \strlen($field) < (int) $val) {
            throw new \InvalidArgumentException($fieldName . ' is less than min-length: ' . $val);
        }
    }
 
    public static function validatePattern($fieldName, $field, $regex = '')
    {
        if (null !== $field && '' !== $field && !preg_match("/^{$regex}$/", $field)) {
            throw new \InvalidArgumentException($fieldName . ' is not match ' . $regex);
        }
    }
 
    public static function validateMaximum($fieldName, $field, $val)
    {
        if (null !== $field && $field > $val) {
            throw new \InvalidArgumentException($fieldName . ' cannot be greater than ' . $val);
        }
    }
 
    public static function validateMinimum($fieldName, $field, $val)
    {
        if (null !== $field && $field < $val) {
            throw new \InvalidArgumentException($fieldName . ' cannot be less than ' . $val);
        }
    }
 
    /**
     * @param array $map
     * @param Model $model
     *
     * @return mixed
     */
    public static function toModel($map, $model)
    {
        $names = $model->getName();
        $names = array_flip($names);
        foreach ($map as $key => $value) {
            $name           = isset($names[$key]) ? $names[$key] : $key;
            $model->{$name} = $value;
        }
 
        return $model;
    }
}