/** * @param $name * @param array $fields * @param int $method * @throws \RuntimeException */ public function __construct($name, array $fields, $model = null, $method = self::METHOD_POST, $action = null) { if (empty($name) || false === is_string($name)) { throw new \RuntimeException('Form name must be defined and must be string.'); } $this->name = $name; $this->model = $model; foreach ($fields as $field => $value) { if (empty($value['type'])) { $value['type'] = 'text'; } if (empty($value['collection'])) { $this->fields[$field] = $value; } else { $this->fields[$field] = $value; $this->fields[$field]['fields'][] = $value; } } if (self::METHOD_GET === $method) { $this->method['type'] = 'get'; } else { $this->method['type'] = 'post'; } $this->method['identifier'] = $method; if (empty($action)) { $this->action = Di::getInstance()->getShared('system.router')->getCurrentRoute(); } else { $this->action = $action; } }
private function getParent() { $pattern = '/\\{\\%parent \\"([A-Za-z._\\/]+)\\" {0,}+\\%\\}/i'; $parent = []; preg_match_all($pattern, $this->source, $parent); if (empty($parent[0]) || empty($parent[1])) { return; } $this->source = str_replace($parent[0][0], '', $this->source); if (count($parent[1]) > 1) { throw new \RuntimeException("Template can have only 1 parent. Template: {$this->file}"); } $template = Di::getInstance()->getDefault('system.template'); if ($this->child) { $child = $this->child; } else { $child = $this->file; } $template->load($parent[1][0], $child); $this->parent = $template; }
/** * @param string $route * @param callable $callback */ public static function delete($route, callable $callback) { /* @var $router RouterInterface */ $router = Di::getInstance()->getShared('system.router'); $router->add($route, MethodInterface::METHOD_DELETE, $callback); }
<?php use System\Application\App; use System\Di\Di; use System\Form\Form; use System\Http\Response\ResponseInterface; use System\Session\SessionInterface; App::get('/users/profile/{id}/{action}', function (ResponseInterface $response, $id, $action) { /** @var SessionInterface $s */ $s = Di::getInstance()->getShared('system.session'); $s->addOneTime(['coa' => 'test', 'test' => 'asd']); $s->add(['coa' => 'zivanovic', 'marjan' => 'hrzic']); return $response; }); App::get('/users/profile/{id}/view', function (ResponseInterface $response, $id) { $form = new Form('test_form', ['first_name' => ['validators' => [new \System\Form\Validators\NotBlank('First name may not be empty')], 'type' => 'text', 'attr' => ['placeholder' => 'First Name']], 'last_name' => ['type' => 'text', 'attr' => ['placeholder' => 'Last Names']], 'email' => ['type' => 'email', 'attr' => ['placeholder' => 'Email']], 'save' => ['type' => 'submit', 'value' => 'save']], new ModelTest(), Form::METHOD_GET, Di::getInstance()->getShared('system.router')->getCurrentRoute()); $form->validate(); var_dump($form->getModel()->getData()); $one = new stdClass(); $one->type = 'Wood'; $one->position = 'Forest'; $two = new stdClass(); $two->type = 'Sand'; $two->position = 'Desert'; $three = new stdClass(); $three->type = 'Water'; $three->position = 'Ocean'; return $response->render('Views/Partial/child.html', ['form' => $form->render(), 'array' => [['type' => 'id', 'position' => $id], ['type' => 'asdasdasd', 'position' => 'testing'], $one, $two]]); });
/** * @param callable $callback * @param array $parameters * * @return ResponseInterface * * @throws \RuntimeException */ private function callCallback(callable $callback, array $parameters = array()) { $reflection = new \ReflectionFunction($callback); $session = Di::getInstance()->getShared('system.session'); $response = Di::getInstance()->getShared('system.http.response'); array_unshift($parameters, $response); /** @var ResponseInterface $return */ $return = $reflection->invokeArgs($parameters); if (false == $return instanceof ResponseInterface) { throw new \RuntimeException("Controller return value must be instance of response"); } $return->updateHeaders(); $session->persistSession(); return $return; }