/**
  * Validate all rulers
  *
  * @access public
  *
  * @return bool
  */
 public function isValid()
 {
     $setOfRules = $this->record->getRules();
     foreach ($setOfRules as $var => $rules) {
         foreach ($rules as $rule) {
             $rule->validate($this->record->{$var}, $var, $this);
         }
     }
     return !empty($this->errors) ? false : true;
 }
 /**
  * Serves for finding posts
  *
  * @param string $id
  * @return array
  */
 static function find($id = 'all')
 {
     $db = ActiveRecord::getDbConnection();
     $table = static::getTable();
     if ($id == 'all') {
         $result = [];
         $query = $db->prepare("SELECT * FROM {$table} ORDER BY id");
         $query->execute();
         while ($row = $query->fetchObject()) {
             array_push($result, $row);
         }
     } else {
         $query = $db->prepare("SELECT * FROM {$table} WHERE id = :id");
         $query->execute([':id' => $id]);
         $result = $query->fetchObject();
     }
     return $result;
 }
Exemple #3
0
 /**
  * User constructor
  *
  * @access public
  *
  * @param array $record
  */
 public function __construct($record = array())
 {
     parent::__construct($record);
 }
Exemple #4
0
 public function __get($name)
 {
     if ($name === 'name') {
         return ActiveRecord::getUserEmailById((int) $this->user_id);
     }
 }
 /**
  * Create Router class and call function that
  * process URL address
  *
  * Check using reflection presence of necessary class
  * of controller and his methods
  *
  * Create Response class
  */
 public function run()
 {
     $response = null;
     $router = new Router(include '../app/config/routes.php');
     $route = $router->parseUrl(trim(strip_tags($_SERVER['REQUEST_URI'])));
     try {
         if (!empty($route)) {
             $controllerReflection = new \ReflectionClass($route['controller']);
             $action = $route['action'] . 'Action';
             if ($controllerReflection->hasMethod($action)) {
                 // Control user role
                 if ($action != 'indexAction' && $action != 'loginAction' && $action != 'signinAction') {
                     if ($_SESSION['role'] != 'ROLE_USER') {
                         throw new AuthRequredException('You must login');
                     }
                 }
                 $controller = $controllerReflection->newInstance();
                 if ($controller instanceof Controller) {
                     $actionReflection = $controllerReflection->getMethod($action);
                     ActiveRecord::getDBCon();
                     $response = $actionReflection->invokeArgs($controller, $route['params']);
                     if ($response instanceof Response) {
                         $response->send();
                         // Close database connection
                         call_user_func(Service::get('event')->trigger('db_close'));
                     } else {
                         throw new BadResponseTypeException('Missing type of response');
                     }
                 } else {
                     throw new BadControllerTypeException('Missing type of controller');
                 }
             }
         } else {
             throw new HttpNotFoundException('Route not found');
         }
     } catch (HttpNotFoundException $e) {
         // Render 404 or just show msg
         $error = $e->getMessage();
         include Service::get('config')->get('error_404');
     } catch (AuthRequredException $e) {
         // Reroute to login page
         Service::get('session')->addFlush('error', $e->getMessage());
         $response = new ResponseRedirect("/login");
         $response->sendHeaders();
     } catch (InvalidArgumentException $e) {
         echo $e->getMessage();
     } catch (BadResponseTypeException $e) {
         echo $e->getMessage();
     } catch (BadPathTypeException $e) {
         echo $e->getMessage();
     } catch (\PDOException $e) {
         echo $e->getMessage();
     } catch (DatabaseException $e) {
         echo $e->getMessage();
     } catch (InvalidTypeException $e) {
         echo $e->getMessage();
     } catch (BadControllerTypeException $e) {
         echo $e->getMessage();
     } catch (\Exception $e) {
         // Do 500 layout...
         include Service::get('config')->get('error_500');
     }
 }