Advertisement
Isigar

Obass 1.2

Feb 25th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.73 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by relisoft.cz
  4.  * @author Stanislav Isigar Opletal
  5.  * @version 1.0.2
  6.  * TODO: from class to array
  7.  */
  8.  
  9. namespace App\System\Core\Helper;
  10.  
  11.  
  12. use Nette\Database\Context;
  13. use Nette\Database\Table\ActiveRow;
  14. use Nette\MemberAccessException;
  15. use Nette\Object;
  16. use Nette\Reflection\ClassType;
  17. use Nette\Utils\ArrayHash;
  18. use Nette\Utils\Arrays;
  19. use Nette\Utils\Strings;
  20. use Tracy\Dumper;
  21.  
  22. class Obass extends Object
  23. {
  24.     const NON_EXCEPTION = 0;
  25.     const EXCEPTION = 1;
  26.  
  27.     /**
  28.      * @param ActiveRow|array $data
  29.      * @param int $type
  30.      * @return Obass
  31.      */
  32.     public function associate($data, $type = self::EXCEPTION)
  33.     {
  34.         if ($data instanceof ActiveRow) {
  35.             $arrayData = $data->toArray();
  36.  
  37.             foreach ($arrayData as $key => $val) {
  38.                 $method = $this->filter($key, true);
  39.                 $class = ClassType::from($this);
  40.                 if ($class->getMethod($method)->getParameters()[0]->hasType()) {
  41.                     if (method_exists($this, $method)) {
  42.                         $class = (string)$class->getMethod($method)->getParameters()[0]->getType();
  43.                         if ($val !== null) {
  44.                             $cC = new $class($val);
  45.                             $failed = false;
  46.                             try {
  47.                                 $cC->associate($data->ref(
  48.                                     (new ClassType($cC))->getConstant("TABLE")
  49.                                 ));
  50.                             } catch (MemberAccessException $e) {
  51.                                 $failed = true;
  52.                             } finally {
  53.                                 if ($failed) {
  54.                                     try{
  55.                                         $cC->associate($data->related(
  56.                                             (new ClassType($cC))->getConstant("TABLE")
  57.                                         ));
  58.                                         $failed = false;
  59.                                     }catch(MemberAccessException $e){
  60.                                         $cC = new $class($val);
  61.                                         $this->$method($cC);
  62.                                     }
  63.                                 }
  64.                             }
  65.                             $this->$method($cC);
  66.                         } else {
  67.                             $cC = new $class($val);
  68.                             $this->$method($cC);
  69.                         }
  70.                     } else {
  71.                         if ($type == self::EXCEPTION)
  72.                             throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
  73.                     }
  74.                 } else {
  75.                     if (method_exists($this, $method)) {
  76.                         $this->$method($val);
  77.                     } else {
  78.                         if ($type == self::EXCEPTION)
  79.                             throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
  80.                     }
  81.                 }
  82.  
  83.             }
  84.             return $this;
  85.  
  86.         } elseif (is_array($data)) {
  87.  
  88.             foreach ($data as $key => $val) {
  89.                 $method = $this->filter($key, true);
  90.  
  91.                 if (method_exists($this, $method)) {
  92.                     $this->$method($val);
  93.                 } else {
  94.                     if ($type == self::EXCEPTION)
  95.                         throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
  96.                 }
  97.             }
  98.             return $this;
  99.  
  100.         } else if ($data instanceof ArrayHash) {
  101.             foreach ($data as $key => $val) {
  102.                 $method = $this->filter($key, true);
  103.  
  104.                 if (method_exists($this, $method)) {
  105.                     $this->$method($val);
  106.                 } else {
  107.                     if ($type == self::EXCEPTION)
  108.                         throw new \BadFunctionCallException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
  109.                 }
  110.             }
  111.             return $this;
  112.         } else {
  113.             if ($type == self::EXCEPTION) {
  114.                 throw new ObassException("Wrong type!");
  115.             }
  116.         }
  117.  
  118.     }
  119.  
  120.     /**
  121.      * @param $string
  122.      * @param bool $forFunction
  123.      * @param string $method
  124.      * @return string
  125.      */
  126.     private function filter($string, $forFunction = false, $method = "set")
  127.     {
  128.         if (Strings::contains($string, "_")) {
  129.             $parts = explode("_", $string);
  130.             $upperParts = [];
  131.  
  132.             $iterator = 0;
  133.             foreach ($parts as $part) {
  134.                 //Pokud to je pro funkci i první pokud ne musíme první vynechat upper.
  135.                 if ($forFunction) {
  136.                     $upperParts[] = Strings::firstUpper($part);
  137.                 } else {
  138.                     if ($iterator != 0)
  139.                         $upperParts[] = Strings::firstUpper($part);
  140.                     else
  141.                         $upperParts[] = ($part);
  142.                 }
  143.                 $iterator++;
  144.             }
  145.             if ($forFunction)
  146.                 return $method . (join("", $upperParts));
  147.             else
  148.                 return (join("", $upperParts));
  149.  
  150.         } else {
  151.             return $method . (Strings::firstUpper($string));
  152.         }
  153.  
  154.     }
  155.  
  156.     /**
  157.      * @param class
  158.      * @return array
  159.      */
  160.     public function fromClass($class)
  161.     {
  162.         $reflection = $class::getReflection();
  163.         $methods = $class::getReflection()->getMethods();
  164.         $array = [];
  165.         foreach ($methods as $one) {
  166.             if (Strings::startsWith($one->getName(), "get")) {
  167.                 if ($one->getDeclaringClass() == $reflection->getName()) {
  168.                     $methodName = $one->getName();
  169.                     $name = explode("_", $this->fromCamelCase($one->getName()));
  170.                     unset($name[0]);
  171.                     if ($class->$methodName() instanceof Obass) {
  172.                         $array[join("_", $name)] = $class->$methodName()->getID();
  173.                     } else {
  174.                         $array[join("_", $name)] = $class->$methodName();
  175.                     }
  176.  
  177.                 }
  178.             }
  179.         }
  180.         return $array;
  181.     }
  182.  
  183.     private function fromCamelCase($input)
  184.     {
  185.         preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
  186.         $ret = $matches[0];
  187.         foreach ($ret as &$match) {
  188.             $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
  189.         }
  190.         return implode('_', $ret);
  191.     }
  192. }
  193.  
  194. class ObassException extends \RuntimeException
  195. {
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement