<?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);
php 数据转对象,对象转数组
最新推荐文章于 2024-08-10 14:41:21 发布