Skip to content

Commit

Permalink
Merge pull request #5 from RunsTp/master
Browse files Browse the repository at this point in the history
🐛 修复keyMap在arrayToBean 时的失效问题
  • Loading branch information
kiss291323003 authored Jul 10, 2019
2 parents d9c6491 + bcc6c72 commit 3cb7ecd
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 68 deletions.
175 changes: 107 additions & 68 deletions src/SplBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,121 +25,118 @@ 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']);
unset($data['_classMap']);
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;
}

/*
* 返回转化后的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']);
Expand All @@ -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);
}

/*
Expand All @@ -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());
Expand All @@ -203,37 +203,37 @@ 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));
}
}
}
}

/**
* @param string $class
* @param null $arg
* @param null $arg
* @return object
* @throws \ReflectionException
*/
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions test/SplBeanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3cb7ecd

Please sign in to comment.