rs = $rs; } /** * @param $obj object to map * @param null $attributes attributes to map * @param null|array $attributesToAppend array of sub-object to map * @param array|null $callBackForObject * @param array|null $callBackForChildrensObject * @return array */ public function objectToArray($obj, $attributes = null, $attributesToAppend = null, ?array $callBackForObject = null, ?array $callBackForChildrensObject = null){ if(is_null($attributes)) return (array)$obj; if(is_null($attributesToAppend)) $attributesToAppend = array(); $return = array(); $prefixes = array('get','is'); if(!is_null($callBackForObject) && gettype($obj) == 'object' && key_exists($this->get_class($obj,false),$callBackForObject)){ $callBackArray = $callBackForObject[$this->get_class($obj,false)]; $argsArray = [$obj]; if(isset($callBackArray[3])) $argsArray = array_merge($callBackArray[3],$argsArray); $return = call_user_func_array([$callBackArray[0],$callBackArray[1]],$argsArray); } else { foreach($attributes as $varName){ $meth = $this->autoFormat($varName,true); foreach($prefixes as $prfx){ $fName = $prfx.$meth; if(method_exists($obj,$fName)){ $value = $obj->$fName(); $retValue = $value; /** * if it implements Collection * @see Collection */ if(gettype($value) == 'object' && method_exists($value,'toArray')){ $retValue = array(); foreach($value->toArray() as $obj2){ $objectName = $this->get_class($obj2,false); //var_dump(1,$objectName); if(isset($attributesToAppend[$objectName])){ $retValue[] = $this->objectToArray($obj2,$attributesToAppend[$objectName],$attributesToAppend,$callBackForObject ,$callBackForChildrensObject); } else if (isset($callBackForChildrensObject[$objectName])){ $v = $this->objectToArray($obj2,$attributesToAppend,$attributesToAppend,$callBackForChildrensObject, $callBackForChildrensObject); if(!is_null($v)) $retValue[] = $v; } } } /** * if its another type of object and its mapped in $attributesToAppend */ else if(!is_null($attributesToAppend) && gettype($value) == 'object' && key_exists($this->get_class($value,false),$attributesToAppend)){ $retValue = $this->objectToArray($value,$attributesToAppend[$this->get_class($value,false)],$attributesToAppend,$callBackForChildrensObject); } $return[$varName] = $retValue; } } } } return $return; } /** * Better get_class * * @param $class * @param bool $namespaced * @return string */ public function get_class($class, $namespaced = true){ $class = get_class($class); if($namespaced) return $class; preg_match('/([A-Z0-9_-]{1,})$/i', $class, $output_array); if(isset($output_array[1])) $class = $output_array[1]; return $class; } /** * @param $objs * @param null $attributes * @param null $attributesToAppend * @param array|null $callBackForObject * @param array|null $callBackForChildrensObject * @return array */ public function objectsToArray($objs, $attributes = null, $attributesToAppend = null, ?array $callBackForObject = null, ?array $callBackForChildrensObject = null){ $ret = array(); if(is_array($objs)){ foreach($objs as $obj){ $ret[] = $this->objectToArray($obj,$attributes,$attributesToAppend, $callBackForObject, $callBackForChildrensObject); } } return $ret; } /** * @param $obj * @param $fromArray * @param null|array $allowed * @return bool */ public function hydrateFromArray($obj, $fromArray, $allowed = null){ if(empty($fromArray)) return false; $prefixes = array('set'); foreach($fromArray as $varName=>$value){ $meth = $this->autoFormat($varName,true); if(is_null($allowed) || in_array($this->autoFormat($varName),$allowed)){ foreach($prefixes as $prfx){ $fName = $prfx.$meth; if(method_exists($obj,$fName)){ $obj->$fName($value); } } } } return true; } /** * @param $string * @param bool $capitalizeFirstCharacter * @return mixed|string */ protected function autoFormat($string, $capitalizeFirstCharacter = false) { //secure $str = str_replace(['$','%','<','>','?','.',' '], '', $string); $str = str_replace('-', '', ucwords($str, '-')); if (!$capitalizeFirstCharacter) { $str = lcfirst($str); } return $str; } }