/** * Creates an html control / element / form element by using DI container instance method * * Injects an instance of the HtmlFactory so the created html object can use it to create sub elements if needed. * * @param string $class * Short NS to used class like 'Elements\Div' or 'Form\Input' or 'Bootstrap\Button\Button'. * @param array $args * Optional assoc arguments array to be used as $html->$method($value) call. * * @throws HtmlException * * @return AbstractHtml */ public function create(string $class, array $args = []) { $class = __NAMESPACE__ . '\\' . $class; $html = new $class(); $html->factory = $this; foreach ($args as $method => $arg) { if (!method_exists($html, $method)) { throw new HtmlException('Html object has no "' . $method . '" method.'); } if (is_array($arg)) { $array = new IsAssoc($arg); if (!$array->isAssoc()) { throw new HtmlException('Arrayed arguments for html objects created by HtmlFactory have to be associative.'); } foreach ($arg as $attr => $val) { $html->{$method}($attr, $val); } } else { $html->{$method}($arg); } } return $html; }
/** * Adds single and multiple elements to properties * * @param string $func * Name of the attribute eg property the arguments ($args)have to be added * @param mixed $args */ private function addTo(string $func, $args) { // Only already existing attributes can get filled with the args if (!isset($this->{$func})) { throw new HtmlException(sprintf('Type "%s" is not allowed as html property/attribute', $func)); } // Do we have one argument or two? if (count($args) == 1) { // One argument and not an array means we have one single value to add // This is when you set attributes without values like selected, disabled etc. if (!is_array($args[0])) { $this->{$func}[$args[0]] = false; } else { // Check the arguments for assoc array and add arguments according to the // result of check as key, val or only as val $array = new IsAssoc($args[0]); if ($array->isAssoc()) { foreach ($args[0] as $key => $val) { $this->{$func}[$key] = $val; } } else { foreach ($args[0] as $val) { $this->{$func}[$val] = false; } } } } else { $this->{$func}[$args[0]] = $args[1]; } }
/** * Adds one parameter in form of key and value or a list of parameters as assoc array by resetting existing * parameters. * Setting an array as $arg1 and leaving $arg2 empty means to add an assoc array of paramters * Setting $arg1 and $arg2 means to set on parameter by name and value. * * @var string array String with parametername or list of parameters of type assoc array * @var string $arg2 Needs only to be set when seting on paramters by name and value. * @var bool $reset Optional: Set this to true when you want to reset already existing parameters * * @throws QueryBuilderException * * @return \Core\Url */ public function setParameter($arg1, $arg2 = null) { if ($arg2 === null) { $array = new IsAssoc($arg1); if (!is_array($arg1) || !$array->isAssoc()) { throw new QueryBuilderException('Setting one QueryBuilder argument means you have to set an assoc array as argument.'); } $this->params = array_merge($this->params, $arg1); } else { $this->params[$arg1] = $arg2; } return $this; }