php 数据转对象,对象转数组

<?php


trait testAttribute
{
   public function __cal($method,$params){
       if(!method_exists($this,$method)){
           return null;
       }
       $val=$this->{$method}(...$params);
       if(substr($val,0,3)=='get'){
           $val=($val=='(null)')?null:$val;
       }
       return $val;
   }

   public function fill($data): self
   {
       if(empty($data)){
           return $this;
       }
       $reflectionClass=new ReflectionClass($this);
       $properties=$reflectionClass->getProperties();
       foreach ($properties as $property){
           $underLine = $this->convertUnderLine($property->getName());//大写转下划线
           $lineUnder = $this->underLineConvert($property->getName());//下划线转大驼峰
           $methodName='set'.$lineUnder;
           if(isset($data[$underLine]) || isset($data[lcfirst($lineUnder)])){
               $val = $data[$underLine]??($data[lcfirst($lineUnder)]??null);
               $val = is_null($val)?'{null}':$val;
               if($reflectionClass->hasMethod($methodName)){
                   $this->{$methodName}($val);
               }
           }
       }
       return $this;
   }


   public function toArray(bool $isUnder=true):array{

       $data=[];
       $reflectionClass=new ReflectionClass($this);
       $properties=$reflectionClass->getProperties();
       foreach ($properties as $property){
           $lineUnder=$this->underLineConvert($property->getName());
           $methodName='get'.$lineUnder;//判断getName
           if(!$reflectionClass->hasMethod($methodName)){
               $methodName='is'.$lineUnder;//判断isName
               if(!$reflectionClass->hasMethod($methodName)){
                   $methodName=$property->getName();//判断Name
                   if($reflectionClass->hasMethod($methodName)){
                       continue;
                   }
               }
           }
           $value=$this->{$methodName}();
           $objectName=$isUnder?$this->convertUnderLine($property->getName()):$property->getName();
           $data[$objectName]=$value=='{null}'?null:$value;
       }
       return $data;

   }

   public function toData():array{
       $data=[];
       $reflectionClass=new ReflectionClass($this);
       $properties=$reflectionClass->getProperties();
       foreach ($properties as $property){
           $lineUnder=$this->underLineConvert($property->getName());
           $methodName='get'.$lineUnder;//判断getName
           if(!$reflectionClass->hasMethod($methodName)){
               $methodName='is'.$lineUnder;//判断isName
               if(!$reflectionClass->hasMethod($methodName)){
                   $methodName=$property->getName();//判断Name
                   if($reflectionClass->hasMethod($methodName)){
                       continue;
                   }
               }
           }
           $value=$this->{$methodName}();
           $value=$value=='{null}'?null:$value;
           if(is_null($value)){
               continue;
           }
           $objectName=$this->convertUnderLine($property->name);
         $data[$objectName]=$value;
       }
       return $data;
   }


   public function underLineConvert(string $name):string{
      return  str_replace(' ','',ucwords(str_replace('_',' ',$name)));
   }

   //大小驼峰转下划线
   public function convertUnderLine(string  $name):string{
       $tmp='';
       for($i=0;$i<strlen($name);$i++){
           $asciiCode=ord($name[$i]);
           if ($asciiCode>=65 && $asciiCode<=90){ //大写字母
               $tmp.=($i==0?'':'_').chr($asciiCode+32); //大写转小写
           }else{
               $tmp.=$name[$i];
           }
       }
       return $tmp;
   }

}

class  testObject
{
    use  testAttribute;

    /**
     * @var string $name
     */
    public string $name;

    /**
     * @var string $sub_name
     */
    public string $sub_name;
    /**
     * @var int
     */
    public int $age;

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getSubName(): string
    {
        return $this->sub_name;
    }

    /**
     * @param string $sub_name
     */
    public function setSubName(string $sub_name): self
    {
        $this->sub_name = $sub_name;
        return $this;
    }

    /**
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @param int $age
     */
    public function setAge(int $age): self
    {
        $this->age = $age;
        return  $this;
    }
}


$testObject=new testObject();
$testObject->fill(['name'=>'xiaoxiao','sub_name'=>'xiaohua','age'=>10]);
$data=$testObject->toArray(false);
var_dump($data);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值