aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/View.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Minz/View.php')
-rw-r--r--lib/Minz/View.php154
1 files changed, 79 insertions, 75 deletions
diff --git a/lib/Minz/View.php b/lib/Minz/View.php
index 3b5052c36..3b18c1dba 100644
--- a/lib/Minz/View.php
+++ b/lib/Minz/View.php
@@ -8,24 +8,29 @@
* The Minz_View represents a view in the MVC paradigm
*/
class Minz_View {
- const VIEWS_PATH_NAME = '/views';
- const LAYOUT_PATH_NAME = '/layout/';
- const LAYOUT_DEFAULT = 'layout';
+ private const VIEWS_PATH_NAME = '/views';
+ private const LAYOUT_PATH_NAME = '/layout/';
+ private const LAYOUT_DEFAULT = 'layout';
+ /** @var string */
private $view_filename = '';
+ /** @var string */
private $layout_filename = '';
-
+ /** @var array<string> */
private static $base_pathnames = array(APP_PATH);
+ /** @var string */
private static $title = '';
- private static $styles = array ();
- private static $scripts = array ();
+ /** @var array<array{'media':string,'url':string}> */
+ private static $styles = [];
+ /** @var array<array{'url':string,'id':string,'defer':string,'async':string}> */
+ private static $scripts = [];
+ /** @var string|array{'dark'?:string,'light'?:string,'default'?:string} */
private static $themeColors;
-
- private static $params = array ();
+ /** @var array<string,mixed> */
+ private static $params = [];
/**
- * Constructeur
- * Détermine si on utilise un layout ou non
+ * Determines if a layout is used or not
*/
public function __construct() {
$this->_layout(self::LAYOUT_DEFAULT);
@@ -34,9 +39,9 @@ class Minz_View {
}
/**
- * [deprecated] Change the view file based on controller and action.
+ * @deprecated Change the view file based on controller and action.
*/
- public function change_view($controller_name, $action_name) {
+ public function change_view(string $controller_name, string $action_name): void {
Minz_Log::warning('Minz_View::change_view is deprecated, it will be removed in a future version. Please use Minz_View::_path instead.');
$this->_path($controller_name. '/' . $action_name . '.phtml');
}
@@ -46,7 +51,7 @@ class Minz_View {
*
* @param string $path the new path
*/
- public function _path($path) {
+ public function _path(string $path): void {
$this->view_filename = self::VIEWS_PATH_NAME . '/' . $path;
}
@@ -57,14 +62,14 @@ class Minz_View {
*
* @param string $base_pathname the new base pathname.
*/
- public static function addBasePathname($base_pathname) {
+ public static function addBasePathname(string $base_pathname): void {
array_unshift(self::$base_pathnames, $base_pathname);
}
/**
- * Construit la vue
+ * Builds the view filename based on controller and action.
*/
- public function build () {
+ public function build(): void {
if ($this->layout_filename !== '') {
$this->buildLayout ();
} else {
@@ -80,7 +85,7 @@ class Minz_View {
* @param string $filename the name of the file to include.
* @return boolean true if the file has been included, false else.
*/
- private function includeFile($filename) {
+ private function includeFile(string $filename): bool {
// We search the filename in the list of base pathnames. Only the first view
// found is considered.
foreach (self::$base_pathnames as $base) {
@@ -95,9 +100,9 @@ class Minz_View {
}
/**
- * Construit le layout
+ * Builds the layout
*/
- public function buildLayout () {
+ public function buildLayout(): void {
header('Content-Type: text/html; charset=UTF-8');
if (!$this->includeFile($this->layout_filename)) {
Minz_Log::notice('File not found: `' . $this->layout_filename . '`');
@@ -105,9 +110,9 @@ class Minz_View {
}
/**
- * Affiche la Vue en elle-même
+ * Displays the View itself
*/
- public function render () {
+ public function render(): void {
if (!$this->includeFile($this->view_filename)) {
Minz_Log::notice('File not found: `' . $this->view_filename . '`');
}
@@ -116,14 +121,14 @@ class Minz_View {
public function renderToString(): string {
ob_start();
$this->render();
- return ob_get_clean();
+ return ob_get_clean() ?: '';
}
/**
- * Ajoute un élément du layout
- * @param string $part l'élément partial à ajouter
+ * Adds a layout element
+ * @param string $part the partial element to be added
*/
- public function partial ($part) {
+ public function partial(string $part): void {
$fic_partial = self::LAYOUT_PATH_NAME . '/' . $part . '.phtml';
if (!$this->includeFile($fic_partial)) {
Minz_Log::warning('File not found: `' . $fic_partial . '`');
@@ -131,10 +136,10 @@ class Minz_View {
}
/**
- * Affiche un élément graphique situé dans APP./views/helpers/
- * @param string $helper l'élément à afficher
+ * Displays a graphic element located in APP./views/helpers/
+ * @param string $helper the element to be displayed
*/
- public function renderHelper ($helper) {
+ public function renderHelper(string $helper): void {
$fic_helper = '/views/helpers/' . $helper . '.phtml';
if (!$this->includeFile($fic_helper)) {
Minz_Log::warning('File not found: `' . $fic_helper . '`');
@@ -142,20 +147,20 @@ class Minz_View {
}
/**
- * Retourne renderHelper() dans une chaîne
- * @param string $helper l'élément à traîter
+ * Returns renderHelper() in a string
+ * @param string $helper the element to be treated
*/
- public function helperToString($helper) {
+ public function helperToString(string $helper): string {
ob_start();
$this->renderHelper($helper);
- return ob_get_clean();
+ return ob_get_clean() ?: '';
}
/**
* Choose the current view layout.
* @param string|false $layout the layout name to use, false to use no layouts.
*/
- public function _layout($layout) {
+ public function _layout($layout): void {
if ($layout) {
$this->layout_filename = self::LAYOUT_PATH_NAME . $layout . '.phtml';
} else {
@@ -168,7 +173,7 @@ class Minz_View {
* @deprecated Please use the `_layout` function instead.
* @param bool $use true if we want to use the layout, false else
*/
- public function _useLayout ($use) {
+ public function _useLayout(bool $use): void {
Minz_Log::warning('Minz_View::_useLayout is deprecated, it will be removed in a future version. Please use Minz_View::_layout instead.');
if ($use) {
$this->_layout(self::LAYOUT_DEFAULT);
@@ -178,60 +183,57 @@ class Minz_View {
}
/**
- * Gestion du titre
+ * Title management
*/
- public static function title () {
+ public static function title(): string {
return self::$title;
}
- public static function headTitle () {
+ public static function headTitle(): string {
return '<title>' . self::$title . '</title>' . "\n";
}
- public static function _title ($title) {
+ public static function _title(string $title): void {
self::$title = $title;
}
- public static function prependTitle ($title) {
+ public static function prependTitle(string $title): void {
self::$title = $title . self::$title;
}
- public static function appendTitle ($title) {
+ public static function appendTitle(string $title): void {
self::$title = self::$title . $title;
}
/**
- * Gestion des feuilles de style
+ * Style sheet management
*/
- public static function headStyle () {
+ public static function headStyle(): string {
$styles = '';
-
foreach(self::$styles as $style) {
$styles .= '<link rel="stylesheet" ' .
($style['media'] === 'all' ? '' : 'media="' . $style['media'] . '" ') .
'href="' . $style['url'] . '" />';
-
$styles .= "\n";
}
return $styles;
}
+
/**
* Prepends a <link> element referencing stylesheet.
- *
- * @param string $url
- * @param string $media
- * @param bool $cond Conditional comment for IE, now deprecated and ignored
+ * @param bool $cond Conditional comment for IE, now deprecated and ignored @deprecated
*/
- public static function prependStyle($url, $media = 'all', $cond = false) {
+ public static function prependStyle(string $url, string $media = 'all', bool $cond = false): void {
array_unshift (self::$styles, array (
'url' => $url,
'media' => $media,
));
}
+
/**
* Append a `<link>` element referencing stylesheet.
* @param string $url
* @param string $media
- * @param bool $cond Conditional comment for IE, now deprecated and ignored
+ * @param bool $cond Conditional comment for IE, now deprecated and ignored @deprecated
*/
- public static function appendStyle($url, $media = 'all', $cond = false) {
+ public static function appendStyle(string $url, string $media = 'all', bool $cond = false): void {
self::$styles[] = array (
'url' => $url,
'media' => $media,
@@ -239,7 +241,7 @@ class Minz_View {
}
/**
- * @param array|string $themeColors
+ * @param string|array{'dark'?:string,'light'?:string,'default'?:string} $themeColors
*/
public static function appendThemeColors($themeColors): void {
self::$themeColors = $themeColors;
@@ -250,29 +252,27 @@ class Minz_View {
*/
public static function metaThemeColor(): string {
$meta = '';
-
- if (!empty(self::$themeColors['light'])) {
- $meta .= '<meta name="theme-color" media="(prefers-color-scheme: light)" content="' . htmlspecialchars(self::$themeColors['light']) . '" />';
- }
- if (!empty(self::$themeColors['dark'])) {
- $meta .= '<meta name="theme-color" media="(prefers-color-scheme: dark)" content="' . htmlspecialchars(self::$themeColors['dark']) . '" />';
- }
- if (!empty(self::$themeColors['default'])) {
- $meta .= '<meta name="theme-color" content="' . htmlspecialchars(self::$themeColors['default']) . '" />';
- }
- if (empty(self::$themeColors['default']) && !empty(self::$themeColors) && empty(self::$themeColors['light']) && empty(self::$themeColors['dark'])) {
+ if (is_array(self::$themeColors)) {
+ if (!empty(self::$themeColors['light'])) {
+ $meta .= '<meta name="theme-color" media="(prefers-color-scheme: light)" content="' . htmlspecialchars(self::$themeColors['light']) . '" />';
+ }
+ if (!empty(self::$themeColors['dark'])) {
+ $meta .= '<meta name="theme-color" media="(prefers-color-scheme: dark)" content="' . htmlspecialchars(self::$themeColors['dark']) . '" />';
+ }
+ if (!empty(self::$themeColors['default'])) {
+ $meta .= '<meta name="theme-color" content="' . htmlspecialchars(self::$themeColors['default']) . '" />';
+ }
+ } elseif (is_string(self::$themeColors)) {
$meta .= '<meta name="theme-color" content="' . htmlspecialchars(self::$themeColors) . '" />';
}
-
return $meta;
}
/**
- * Gestion des scripts JS
+ * JS script management
*/
- public static function headScript () {
+ public static function headScript(): string {
$scripts = '';
-
foreach (self::$scripts as $script) {
$scripts .= '<script src="' . $script['url'] . '"';
if (!empty($script['id'])) {
@@ -293,12 +293,12 @@ class Minz_View {
/**
* Prepend a `<script>` element.
* @param string $url
- * @param bool $cond Conditional comment for IE, now deprecated and ignored
+ * @param bool $cond Conditional comment for IE, now deprecated and ignored @deprecated
* @param bool $defer Use `defer` flag
* @param bool $async Use `async` flag
* @param string $id Add a script `id` attribute
*/
- public static function prependScript($url, $cond = false, $defer = true, $async = true, $id = '') {
+ public static function prependScript(string $url, bool $cond = false, bool $defer = true, bool $async = true, string $id = ''): void {
array_unshift(self::$scripts, array (
'url' => $url,
'defer' => $defer,
@@ -306,15 +306,16 @@ class Minz_View {
'id' => $id,
));
}
-/**
+
+ /**
* Append a `<script>` element.
* @param string $url
- * @param bool $cond Conditional comment for IE, now deprecated and ignored
+ * @param bool $cond Conditional comment for IE, now deprecated and ignored @deprecated
* @param bool $defer Use `defer` flag
* @param bool $async Use `async` flag
* @param string $id Add a script `id` attribute
*/
- public static function appendScript($url, $cond = false, $defer = true, $async = true, $id = '') {
+ public static function appendScript(string $url, bool $cond = false, bool $defer = true, bool $async = true, string $id = ''): void {
self::$scripts[] = array (
'url' => $url,
'defer' => $defer,
@@ -324,12 +325,15 @@ class Minz_View {
}
/**
- * Gestion des paramètres ajoutés à la vue
+ * Management of parameters added to the view
+ * @param string $key
+ * @param mixed $value
*/
- public static function _param ($key, $value) {
+ public static function _param(string $key, $value): void {
self::$params[$key] = $value;
}
- public function attributeParams () {
+
+ public function attributeParams(): void {
foreach (Minz_View::$params as $key => $value) {
$this->$key = $value;
}