Esempio n. 1
0
 /**
  * 根据在PHPDoc里面声明的属性(字段)类型,作出自动类型转换。一般用在保存之前,或写在子类的validate方法里面。
  * 注意是trait,不能重写覆盖。
  * @return void
  */
 public function autoConvert()
 {
     $vars = $this->toArray();
     foreach ($vars as $name => $value) {
         if (is_null($value)) {
             continue;
         }
         //通过ReflectionProperty类获取字段的类型
         $PropertyX = new ReflectionPropertyX($this, $name);
         $type = $PropertyX->_var;
         $type = strtolower($type);
         $type = preg_replace("/^\\\\/", "", $type);
         //开始自动转换
         if ($name == "_id" && is_string($value) || $type == "mongoid" && is_string($value)) {
             if ($value) {
                 $this->{$name} = new \MongoId($value);
             } else {
                 $this->{$name} = null;
             }
         } elseif ($type == "mongoid[]") {
             if (!$value) {
                 $this->{$name} = null;
             } elseif (is_string($value)) {
                 $this->{$name} = StringTool::toMongoIds($value);
             } elseif (is_array($value)) {
                 $this->{$name} = array_map(["\\rayful\\Tool\\StringTool", "toMongoId"], $value);
             }
         } elseif ($type == "bool" || $type == "boolean") {
             if ($value === "false") {
                 $value = false;
             }
             $this->{$name} = boolval($value);
         } elseif ($type == "string") {
             $this->{$name} = strval($value);
         } elseif ($type == "float") {
             $this->{$name} = floatval($value);
         } elseif ($type == "int" || $type == "integer") {
             $this->{$name} = intval($value);
         } elseif ($type == "mongodate") {
             //主要针对淘宝传过来的时间是字符串类型
             if (is_int($value)) {
                 $this->{$name} = new \MongoDate($value);
             } elseif (is_object($value) && $value instanceof \MongoDate) {
                 $this->{$name} = $value;
             } elseif ($value) {
                 $this->{$name} = new \MongoDate(strtotime($value));
             }
         } elseif (($type == "array" || strpos($type, "[]") !== false) && is_string($value)) {
             //主要针对使用textarea post过来是字符串类型的字段
             $value = trim($value);
             if ($value) {
                 $value = StringTool::toArray($value);
                 $this->{$name} = $value;
             } else {
                 $this->{$name} = null;
             }
         }
     }
 }