SMMP/build/generic/core/View.php

79 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace generic\core;
class View{
/* (1) Attributes
---------------------------------------------------------*/
private $view_class;
private $arguments;
public static $html_error = "<span class='error'>Une erreur est survenue, veuilez contacter le webmaster si cette erreur persiste.</span>";
/* (2) Instance constructor
*
* @view_class<String> The target view class
*
---------------------------------------------------------*/
private function __construct(String $view_class){
$this->view_class = $view_class;
}
/* (3) Renders a view with injected data
*
* @injected_data<Array> Data to inject
*
* @return render<String> Rendered view
*
---------------------------------------------------------*/
public function render($injected_data=[]){
/* (1) Create instance with data */
$view = new $this->view_class(...$injected_data);
/* (2) Dispatch rendering */
return $view->render();
}
/* (4) Loads a view from its path (builder)
*
* @view_path<String> View path
*
* @return view<View> View instance
* NULL on error
*
---------------------------------------------------------*/
public static function load(String $view_path){
/* (1) Check arguments
---------------------------------------------------------*/
/* (1) Check path format */
if( !preg_match('@^[\w+\.]+$@', $view_path) )
return null;
/* (2) Extract class */
$view_class = '\\view\\'.str_replace('.', '\\', $view_path).'\\main';
/* (3) Check if class exists */
if( !class_exists($view_class) )
return null;
/* (2) Return View instance
---------------------------------------------------------*/
return new self($view_class);
}
}