Isigar

Associate

Apr 14th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1.  /**
  2.      * @param array $data
  3.      * @return Easy|null
  4.      * @throws EasyException
  5.      */
  6.     public function associate(array $data = null,$class=null)
  7.     {
  8.  
  9.         if(!is_null($data)){
  10.             if(is_null($class)) { $class = $this->_this; }
  11.             if(is_string($class))
  12.             {
  13.                 $class = new $class;
  14.             }
  15.             foreach($data as $key => $val)
  16.             {
  17.                 $func = "set".$this->filter($key,true);
  18.                 if(method_exists($class ,$func ))
  19.                 {
  20.                     $class->$func($val); //call_user_func([$class,$func],$val);
  21.                 }
  22.                 else
  23.                 {
  24.                     throw new EasyException('V funkci associate došlo k chybě. Snažíte se přiradit hodnotu v array ale funkce k ní neexistuje: '.$func.' [ '.get_class($class).' ]');
  25.                 }
  26.             }
  27.             return $class;
  28.         }
  29.     }
  30.  
  31.     /**
  32.      * @param $var
  33.      * @param bool $forFunction
  34.      * @return mixed|string
  35.      * @throws EasyException
  36.      */
  37.     public function filter($var, $forFunction = false)
  38.     {
  39.         if(is_string($var))
  40.         {
  41.             $count = substr_count($var,'_');
  42.             $lastPos = 0;
  43.             $editVar = $var;
  44.  
  45.             for($i = 0; $i < $count; $i++)
  46.             {
  47.                 $pos = strpos($editVar,'_',$lastPos);
  48.                 $editVar = substr_replace($editVar,'',$pos,1);
  49.                 $stringArray = str_split($editVar);
  50.                 $editVar = substr_replace($editVar,strtoupper($stringArray[$pos]),$pos,1);
  51.                 $lastPos = $pos;
  52.             }
  53.  
  54.             if($forFunction)
  55.             {
  56.                 return ucfirst($editVar);
  57.             }
  58.             else
  59.             {
  60.                 return $editVar;
  61.             }
  62.         }
  63.         else
  64.         {
  65.             throw new EasyException("Funkce filter přímá pouze string!");
  66.         }
  67.  
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment