Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by relisoft.cz
- * @author Stanislav Isigar Opletal
- * @version 1.0.2
- * TODO: from class to array
- */
- namespace App\System\Core\Helper;
- use Nette\Database\Context;
- use Nette\Database\Table\ActiveRow;
- use Nette\MemberAccessException;
- use Nette\Object;
- use Nette\Reflection\ClassType;
- use Nette\Utils\ArrayHash;
- use Nette\Utils\Arrays;
- use Nette\Utils\Strings;
- use Tracy\Dumper;
- class Obass extends Object
- {
- const NON_EXCEPTION = 0;
- const EXCEPTION = 1;
- /**
- * @param ActiveRow|array $data
- * @param int $type
- * @return Obass
- */
- public function associate($data, $type = self::EXCEPTION)
- {
- if ($data instanceof ActiveRow) {
- $arrayData = $data->toArray();
- foreach ($arrayData as $key => $val) {
- $method = $this->filter($key, true);
- $class = ClassType::from($this);
- if ($class->getMethod($method)->getParameters()[0]->hasType()) {
- if (method_exists($this, $method)) {
- $class = (string)$class->getMethod($method)->getParameters()[0]->getType();
- if ($val !== null) {
- $cC = new $class($val);
- $failed = false;
- try {
- $cC->associate($data->ref(
- (new ClassType($cC))->getConstant("TABLE")
- ));
- } catch (MemberAccessException $e) {
- $failed = true;
- } finally {
- if ($failed) {
- try{
- $cC->associate($data->related(
- (new ClassType($cC))->getConstant("TABLE")
- ));
- $failed = false;
- }catch(MemberAccessException $e){
- $cC = new $class($val);
- $this->$method($cC);
- }
- }
- }
- $this->$method($cC);
- } else {
- $cC = new $class($val);
- $this->$method($cC);
- }
- } else {
- if ($type == self::EXCEPTION)
- throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
- }
- } else {
- if (method_exists($this, $method)) {
- $this->$method($val);
- } else {
- if ($type == self::EXCEPTION)
- throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
- }
- }
- }
- return $this;
- } elseif (is_array($data)) {
- foreach ($data as $key => $val) {
- $method = $this->filter($key, true);
- if (method_exists($this, $method)) {
- $this->$method($val);
- } else {
- if ($type == self::EXCEPTION)
- throw new ObassException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
- }
- }
- return $this;
- } else if ($data instanceof ArrayHash) {
- foreach ($data as $key => $val) {
- $method = $this->filter($key, true);
- if (method_exists($this, $method)) {
- $this->$method($val);
- } else {
- if ($type == self::EXCEPTION)
- throw new \BadFunctionCallException("Function [{$method}] doesnt exists in class [{get_class($this)}]!");
- }
- }
- return $this;
- } else {
- if ($type == self::EXCEPTION) {
- throw new ObassException("Wrong type!");
- }
- }
- }
- /**
- * @param $string
- * @param bool $forFunction
- * @param string $method
- * @return string
- */
- private function filter($string, $forFunction = false, $method = "set")
- {
- if (Strings::contains($string, "_")) {
- $parts = explode("_", $string);
- $upperParts = [];
- $iterator = 0;
- foreach ($parts as $part) {
- //Pokud to je pro funkci i první pokud ne musíme první vynechat upper.
- if ($forFunction) {
- $upperParts[] = Strings::firstUpper($part);
- } else {
- if ($iterator != 0)
- $upperParts[] = Strings::firstUpper($part);
- else
- $upperParts[] = ($part);
- }
- $iterator++;
- }
- if ($forFunction)
- return $method . (join("", $upperParts));
- else
- return (join("", $upperParts));
- } else {
- return $method . (Strings::firstUpper($string));
- }
- }
- /**
- * @param class
- * @return array
- */
- public function fromClass($class)
- {
- $reflection = $class::getReflection();
- $methods = $class::getReflection()->getMethods();
- $array = [];
- foreach ($methods as $one) {
- if (Strings::startsWith($one->getName(), "get")) {
- if ($one->getDeclaringClass() == $reflection->getName()) {
- $methodName = $one->getName();
- $name = explode("_", $this->fromCamelCase($one->getName()));
- unset($name[0]);
- if ($class->$methodName() instanceof Obass) {
- $array[join("_", $name)] = $class->$methodName()->getID();
- } else {
- $array[join("_", $name)] = $class->$methodName();
- }
- }
- }
- }
- return $array;
- }
- private function fromCamelCase($input)
- {
- preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
- $ret = $matches[0];
- foreach ($ret as &$match) {
- $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
- }
- return implode('_', $ret);
- }
- }
- class ObassException extends \RuntimeException
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement