3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class test{ protected $id; protected $name; public function getId(){ return $this->id; } public function setId($id){ $this->id = $id; return $this; } public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; return $this; } } class AbstractDbToMapper { /** * @var array */ protected $fields = [ 'new_spec_name' => 'name', 'object_id' => 'id' ]; /** * @param $data * * @return mixed */ public function mapFromDbData($data, $object) { $arrayKeys = array_keys($data); if(empty($arrayKeys)){ return $object; } foreach ($arrayKeys as $key){ if(empty($key)){ continue; } if(!array_key_exists($key,$this->fields)){ continue; } $field = $this->fields[$key]; $method = sprintf('set%s', ucfirst($field)); if(!method_exists($object,$method)){ continue; } call_user_func([$object, $method,$data[$field]]); } return $object; } /** * @param $object * * @return mixed */ public function mapToDbData($object) { $objectFields = array_map( function ($field) { return lcfirst(substr($field, 3)); }, preg_grep('/^get/', get_class_methods($object)) ); $resultArray = []; foreach ($objectFields as $field) { if (!in_array($field, $this->fields)) { continue; } $key = array_search($field,$this->fields); if (empty($key)) { continue; } $method = sprintf('get%s', ucfirst($field)); $resultArray[$key] = call_user_func([$object, $method]); } return $resultArray; } } $array = [ 'object_id' => 4, 'new_spec_name' => 'Супер пупер имя', ]; $test = new test(); $mapper = new AbstractDbToMapper(); var_dump($mapper->mapFromDbData($array,$test)); var_dump($mapper->mapToDbData($test));
based on BqZj9
Output for 5.6.30, 7.0.30 - 7.3.0beta1
Notice: Undefined index: id in /in/Z7hKd on line 67 Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 Notice: Undefined index: name in /in/Z7hKd on line 67 Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 object(test)#1 (2) { ["id":protected]=> NULL ["name":protected]=> NULL } array(2) { ["object_id"]=> NULL ["new_spec_name"]=> NULL }
Output for hhvm-3.22.0
Notice: Undefined index: id in /in/Z7hKd on line -1 Warning: Invalid argument: function: not a valid callback array in /in/Z7hKd on line -1 Notice: Undefined index: name in /in/Z7hKd on line -1 Warning: Invalid argument: function: not a valid callback array in /in/Z7hKd on line -1 object(test)#1 (2) { ["id":protected]=> NULL ["name":protected]=> NULL } array(2) { ["object_id"]=> NULL ["new_spec_name"]=> NULL }
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined index: id in /in/Z7hKd on line 67 Warning: Invalid argument: function: not a valid callback array in /in/Z7hKd on line 67 Notice: Undefined index: name in /in/Z7hKd on line 67 Warning: Invalid argument: function: not a valid callback array in /in/Z7hKd on line 67 object(test)#1 (2) { ["id":protected]=> NULL ["name":protected]=> NULL } array(2) { ["object_id"]=> NULL ["new_spec_name"]=> NULL }