/** * Create a IoC-container, that is: load a class with typehinting-autoload functionality * * @param string $concrete the class you want to load * @param string $method the method you want to load * @param array $args the arguments for this method * @param array $mainargs the arguments for submethods * * @return mixed result object or value of the object */ public static function init($concrete, $method = null, $args = [], $mainargs = []) { if ($method !== null) { // heeft een method $methodReflection = new reflectionMethod($concrete, $method); $parameters = []; $methodArguments = $methodReflection->getParameters(); $i = 0; if (!empty($methodArguments)) { $nargs = []; foreach ($args as $k => $v) { if (!is_integer($k)) { $nargs[$k] = $v; unset($args[$k]); } } $args = array_values($args); foreach ($methodArguments as $k => $v) { $class = $v->getClass(); if (!is_null($class)) { $parameters[] = self::init($class->name, null, [], $mainargs); } elseif (isset($nargs[$v->name])) { $parameters[] = $nargs[$v->name]; } else { $parameters[] = @$args[$i]; ++$i; } } } if (!empty($parameters)) { $reflection = new reflectionClass($concrete); return $methodReflection->invokeArgs($reflection->newInstanceWithoutConstructor(), $parameters); } else { return $methodReflection->invoke(self::init($concrete)); } } else { // gebruik construct (als die er is) $reflection = new reflectionClass($concrete); $constructor = $reflection->getConstructor(); if (is_null($constructor)) { if (String::like($concrete, 'Application\\Validation\\%')) { $o = new $concrete(); $r = $o->execute($o->_rules); return $o; } elseif (String::like($concrete, '%\\%') === false) { return new $concrete($args['id']); } else { $obj = new $concrete(); return $obj; } } else { $parameters = []; $methodArguments = $constructor->getParameters(); if (!empty($methodArguments)) { foreach ($methodArguments as $k => $v) { $class = $v->getClass(); if (!is_null($class)) { $parameters[] = self::init($class->name, null, []); } elseif (isset($args[$v->name])) { $parameters[] = $args[$v->name]; } } } if (!empty($parameters)) { return $constructor->invokeArgs($reflection->newInstanceWithoutConstructor(), $parameters); } else { if (String::like($concrete, 'Application\\Validation\\%')) { $o = new $concrete(); $r = $o->execute($o->_rules); return $o; } elseif (String::like($concrete, '%\\%') === false && !empty($mainargs['id'])) { return new $concrete($mainargs['id']); } else { return new $concrete(); } } } } }
/** * Make an errorstring * * @param string $name the name of the column * @param value $value the given value * * @return string the errorstring */ public function makeError($name, $value) { $column = $value; $num = null; if (String::like($column, '%:%')) { $e = String::split(':', $column); $column = $e[0]; $num = $e[1]; } if (App::hasLanguage($name)) { $name = App::language($name); } return App::language('validation.error.' . $column, ['name' => String::lower($name), 'num' => $num]); }