From 3f8423b91f1517b483f8124b9bdba09db3a7d57c Mon Sep 17 00:00:00 2001 From: RunsTp Date: Wed, 10 Jul 2019 05:57:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8DkeyMap=E5=9C=A8ar?= =?UTF-8?q?rayToBean=20=E6=97=B6=E7=9A=84=E5=A4=B1=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SplBean.php | 175 +++++++++++++++++++++++++++++------------------- 1 file changed, 107 insertions(+), 68 deletions(-) diff --git a/src/SplBean.php b/src/SplBean.php index 2322779..79862b0 100644 --- a/src/SplBean.php +++ b/src/SplBean.php @@ -25,22 +25,22 @@ class SplBean implements \JsonSerializable private $_keyMap = []; private $_classMap = []; - public function __construct(array $data = null,$autoCreateProperty = false) + public function __construct(array $data = null, $autoCreateProperty = false) { $this->_keyMap = $this->setKeyMapping(); $this->_classMap = $this->setClassMapping(); - if($data){ - $this->arrayToBean($data,$autoCreateProperty); + if ($data) { + $this->arrayToBean($data, $autoCreateProperty); } $this->initialize(); $this->classMap(); } - final public function allProperty():array + final public function allProperty(): array { $data = []; - foreach ($this as $key => $item){ - array_push($data,$key); + foreach ($this as $key => $item) { + array_push($data, $key); } $data = array_flip($data); unset($data['_keyMap']); @@ -48,30 +48,30 @@ final public function allProperty():array return array_flip($data); } - function toArray(array $columns = null,$filter = null):array + function toArray(array $columns = null, $filter = null): array { $data = $this->jsonSerialize(); - if($columns){ + if ($columns) { $data = array_intersect_key($data, array_flip($columns)); } - if($filter === self::FILTER_NOT_NULL){ - return array_filter($data,function ($val){ + if ($filter === self::FILTER_NOT_NULL) { + return array_filter($data, function ($val) { return !is_null($val); }); - }else if($filter === self::FILTER_NOT_EMPTY){ - return array_filter($data,function ($val){ + } else if ($filter === self::FILTER_NOT_EMPTY) { + return array_filter($data, function ($val) { return !empty($val); }); - }else if($filter === self::FILTER_NULL){ - return array_filter($data,function ($val){ + } else if ($filter === self::FILTER_NULL) { + return array_filter($data, function ($val) { return is_null($val); }); - }else if($filter === self::FILTER_EMPTY){ - return array_filter($data,function ($val){ + } else if ($filter === self::FILTER_EMPTY) { + return array_filter($data, function ($val) { return empty($val); }); - }else if(is_callable($filter)){ - return array_filter($data,$filter); + } else if (is_callable($filter)) { + return array_filter($data, $filter); } return $data; } @@ -79,67 +79,64 @@ function toArray(array $columns = null,$filter = null):array /* * 返回转化后的array */ - function toArrayWithMapping(array $columns = null,$filter = null) + function toArrayWithMapping(array $columns = null, $filter = null) { $array = $this->toArray(); - if(!empty($this->_keyMap)){ - foreach ($this->_keyMap as $beanKey => $dataKey) { - if(array_key_exists($beanKey,$array)){ - $array[$dataKey] = $array[$beanKey]; - unset($array[$beanKey]); - } - } - } - if($columns){ + $array = $this->beanKeyMap($array); + + if ($columns) { $array = array_intersect_key($array, array_flip($columns)); } - if($filter === self::FILTER_NOT_NULL){ - return array_filter($array,function ($val){ + if ($filter === self::FILTER_NOT_NULL) { + return array_filter($array, function ($val) { return !is_null($val); }); - }else if($filter === self::FILTER_NOT_EMPTY){ - return array_filter($array,function ($val){ - if($val === 0 || $val === '0'){ + } else if ($filter === self::FILTER_NOT_EMPTY) { + return array_filter($array, function ($val) { + if ($val === 0 || $val === '0') { return true; - }else{ + } else { return !empty($val); } }); - }else if(is_callable($filter)){ - return array_filter($array,$filter); + } else if (is_callable($filter)) { + return array_filter($array, $filter); } return $array; } - final private function arrayToBean(array $data,$autoCreateProperty = false):SplBean + final private function arrayToBean(array $data, $autoCreateProperty = false): SplBean { - if($autoCreateProperty == false){ - $data = array_intersect_key($data,array_flip($this->allProperty())); + + $data = $this->dataKeyMap($data); + + if ($autoCreateProperty == false) { + $data = array_intersect_key($data, array_flip($this->allProperty())); } - foreach ($data as $key => $item){ - $this->addProperty($key,$item); + foreach ($data as $key => $item) { + $this->addProperty($key, $item); } return $this; } - final public function addProperty($name,$value = null):void + final public function addProperty($name, $value = null): void { $this->$name = $value; } final public function getProperty($name) { - if(isset($this->$name)){ + if (isset($this->$name)) { return $this->$name; - }else{ + } else { return null; } } - final public function jsonSerialize():array + final public function jsonSerialize(): array { $data = []; - foreach ($this as $key => $item){ + foreach ($this as $key => $item) { $data[$key] = $item; } unset($data['_keyMap']); @@ -150,30 +147,32 @@ final public function jsonSerialize():array /* * 在子类中重写该方法,可以在类初始化的时候进行一些操作 */ - protected function initialize():void + protected function initialize(): void { } /* * 如果需要用到keyMap 请在子类重构并返回对应的map数据 - * return ['dataKey'=>'beanKey'] + * return ['beanKey'=>'dataKey'] + * return ['实际的键名'=>'传人的键名'] */ - protected function setKeyMapping():array + protected function setKeyMapping(): array { return []; } + /* * return ['property'=>class string] */ - protected function setClassMapping():array + protected function setClassMapping(): array { return []; } public function __toString() { - return json_encode($this->jsonSerialize(),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); + return json_encode($this->jsonSerialize(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } /* @@ -182,13 +181,14 @@ public function __toString() public function restore(array $data = [], $autoCreateProperty = false) { $this->clear(); - $this->arrayToBean($data+get_class_vars(static::class), $autoCreateProperty); + $this->arrayToBean($data + get_class_vars(static::class), $autoCreateProperty); $this->initialize(); $this->classMap(); return $this; } - private function clear() { + private function clear() + { $keys = $this->allProperty(); $ref = new \ReflectionClass(static::class); $fields = array_keys($ref->getDefaultProperties()); @@ -203,29 +203,29 @@ private function clear() { private function classMap() { - if(!empty($this->_classMap)){ + if (!empty($this->_classMap)) { $propertyList = $this->allProperty(); - foreach ($this->_classMap as $property => $class){ - if(in_array($property,$propertyList)){ + foreach ($this->_classMap as $property => $class) { + if (in_array($property, $propertyList)) { $val = $this->$property; $force = true; - if(strpos($class,'@') !== false){ + if (strpos($class, '@') !== false) { $force = false; - $class = substr($class,1); + $class = substr($class, 1); } - if(is_object($val)){ - if(!$val instanceof $class){ - throw new Exception("value for property:{$property} dot not match in ".(static::class)); + if (is_object($val)) { + if (!$val instanceof $class) { + throw new Exception("value for property:{$property} dot not match in " . (static::class)); } - }else if($val === null){ - if($force){ + } else if ($val === null) { + if ($force) { $this->$property = $this->createClass($class); } - }else{ - $this->$property = $this->createClass($class,$val); + } else { + $this->$property = $this->createClass($class, $val); } - }else{ - throw new Exception("property:{$property} not exist in ".(static::class)); + } else { + throw new Exception("property:{$property} not exist in " . (static::class)); } } } @@ -233,7 +233,7 @@ private function classMap() /** * @param string $class - * @param null $arg + * @param null $arg * @return object * @throws \ReflectionException */ @@ -243,4 +243,43 @@ private function createClass(string $class, $arg = null) return $ref->newInstance($arg); } + /** + * beanKeyMap + * 将Bean的属性名转化为data数据键名 + * + * @param array $array + * @return array + */ + final private function beanKeyMap(array $array): array + { + if (!empty($this->_keyMap)) { + foreach ($this->_keyMap as $beanKey => $dataKey) { + if (array_key_exists($beanKey, $array)) { + $array[$dataKey] = $array[$beanKey]; + unset($array[$beanKey]); + } + } + } + return $array; + } + + /** + * dataKeyMap + * 将data中的键名 转化为Bean的属性名 + * + * @param array $array + * @return array + */ + final private function dataKeyMap(array $array): array + { + if (!empty($this->_keyMap)) { + foreach ($this->_keyMap as $beanKey => $dataKey) { + if (array_key_exists($dataKey, $array)) { + $array[$beanKey] = $array[$dataKey]; + unset($array[$dataKey]); + } + } + } + return $array; + } } \ No newline at end of file From bcc6c72abaf97f17cfa818682e310ec641484016 Mon Sep 17 00:00:00 2001 From: RunsTp Date: Wed, 10 Jul 2019 05:57:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:art:=20=E6=96=B0=E5=A2=9E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=20arrayToBean=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/SplBeanTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/SplBeanTest.php b/test/SplBeanTest.php index a7384fc..7c7760e 100644 --- a/test/SplBeanTest.php +++ b/test/SplBeanTest.php @@ -67,6 +67,12 @@ function testToArrayThree() { $this->assertTrue($result['orderId'] === 1 && $result['name'] === 'order' && count(array_diff($goodItem, $good)) === 0 && count(array_diff($good, $goodItem)) === 0); } + function testArrayToBeanOne() { + $good = ['goodId' => 12, 'goodName' => 'apple', 'price' => 12, 'number' => 4]; + $bean = new AppleBean($good); + $this->assertTrue(!empty($bean->getName()) && $bean->getName() === $good['goodName']); + } + function testToArrayWithMappingOne() { $data = ['goodId' => 12, 'name' => 'apple', 'price' => 12, 'number' => 4, 'age' => 12]; $bean = new AppleBean($data);