Model.php 3.1 KB

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