Added PHP Type hints to View class

pull/515/head
fadrian06 1 year ago
parent 7ac12b7891
commit 147c42449d

@ -17,40 +17,24 @@ namespace flight\template;
*/ */
class View class View
{ {
/** /** Location of view templates. */
* Location of view templates. public string $path;
*
* @var string
*/
public $path;
/** /** File extension. */
* File extension. public string $extension = '.php';
*
* @var string
*/
public $extension = '.php';
/** /** @var array<string, mixed> View variables. */
* View variables. protected array $vars = [];
*
* @var array<string, mixed>
*/
protected $vars = [];
/** /** Template file. */
* Template file. private string $template;
*
* @var string
*/
private $template;
/** /**
* Constructor. * Constructor.
* *
* @param string $path Path to templates directory * @param string $path Path to templates directory
*/ */
public function __construct($path = '.') public function __construct(string $path = '.')
{ {
$this->path = $path; $this->path = $path;
} }
@ -58,11 +42,9 @@ class View
/** /**
* Gets a template variable. * Gets a template variable.
* *
* @param string $key Key * @return mixed Variable value or `null` if doesn't exists
*
* @return mixed Value
*/ */
public function get($key) public function get(string $key)
{ {
return $this->vars[$key] ?? null; return $this->vars[$key] ?? null;
} }
@ -70,13 +52,13 @@ class View
/** /**
* Sets a template variable. * Sets a template variable.
* *
* @param string|iterable<string, mixed> $key Key * @param string|iterable<string, mixed> $key
* @param mixed $value Value * @param mixed $value Value
* @return static * @return $this
*/ */
public function set($key, $value = null) public function set($key, $value = null): self
{ {
if (\is_array($key) || \is_object($key)) { if (\is_iterable($key)) {
foreach ($key as $k => $v) { foreach ($key as $k => $v) {
$this->vars[$k] = $v; $this->vars[$k] = $v;
} }
@ -90,11 +72,9 @@ class View
/** /**
* Checks if a template variable is set. * Checks if a template variable is set.
* *
* @param string $key Key
*
* @return bool If key exists * @return bool If key exists
*/ */
public function has($key) public function has(string $key): bool
{ {
return isset($this->vars[$key]); return isset($this->vars[$key]);
} }
@ -102,10 +82,9 @@ class View
/** /**
* Unsets a template variable. If no key is passed in, clear all variables. * Unsets a template variable. If no key is passed in, clear all variables.
* *
* @param string $key Key * @return $this
* @return static
*/ */
public function clear($key = null) public function clear(?string $key = null): self
{ {
if (null === $key) { if (null === $key) {
$this->vars = []; $this->vars = [];
@ -120,25 +99,24 @@ class View
* Renders a template. * Renders a template.
* *
* @param string $file Template file * @param string $file Template file
* @param array<string, mixed> $data Template data * @param array<string, mixed> $data Template data
* *
* @throws \Exception If template not found * @throws \Exception If template not found
* @return void
*/ */
public function render($file, $data = null) public function render(string $file, ?array $data = null): void
{ {
$this->template = $this->getTemplate($file); $this->template = $this->getTemplate($file);
if (!file_exists($this->template)) { if (!\file_exists($this->template)) {
$this->template = self::normalizePath($this->template); $this->template = self::normalizePath($this->template);
throw new \Exception("Template file not found: {$this->template}."); throw new \Exception("Template file not found: {$this->template}.");
} }
if (\is_array($data)) { if (\is_array($data)) {
$this->vars = array_merge($this->vars, $data); $this->vars = \array_merge($this->vars, $data);
} }
extract($this->vars); \extract($this->vars);
include $this->template; include $this->template;
} }
@ -147,17 +125,17 @@ class View
* Gets the output of a template. * Gets the output of a template.
* *
* @param string $file Template file * @param string $file Template file
* @param array<string, mixed> $data Template data * @param ?array<string, mixed> $data Template data
* *
* @return string Output of template * @return string Output of template
*/ */
public function fetch($file, $data = null) public function fetch(string $file, ?array $data = null): string
{ {
ob_start(); \ob_start();
$this->render($file, $data); $this->render($file, $data);
return ob_get_clean(); return \ob_get_clean();
} }
/** /**
@ -167,9 +145,9 @@ class View
* *
* @return bool Template file exists * @return bool Template file exists
*/ */
public function exists($file) public function exists(string $file): bool
{ {
return file_exists($this->getTemplate($file)); return \file_exists($this->getTemplate($file));
} }
/** /**
@ -179,17 +157,17 @@ class View
* *
* @return string Template file location * @return string Template file location
*/ */
public function getTemplate($file) public function getTemplate(string $file): string
{ {
$ext = $this->extension; $ext = $this->extension;
if (!empty($ext) && (substr($file, -1 * \strlen($ext)) != $ext)) { if (!empty($ext) && (\substr($file, -1 * \strlen($ext)) != $ext)) {
$file .= $ext; $file .= $ext;
} }
$is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; $is_windows = \strtoupper(\substr(PHP_OS, 0, 3)) === 'WIN';
if (('/' == substr($file, 0, 1)) || ($is_windows === true && ':' == substr($file, 1, 1))) { if (('/' == \substr($file, 0, 1)) || ($is_windows === true && ':' == \substr($file, 1, 1))) {
return $file; return $file;
} }
@ -203,15 +181,15 @@ class View
* *
* @return string Escaped string * @return string Escaped string
*/ */
public function e($str) public function e(string $str): string
{ {
$value = htmlentities($str); $value = \htmlentities($str);
echo $value; echo $value;
return $value; return $value;
} }
protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string
{ {
return str_replace(['\\', '/'], $separator, $path); return \str_replace(['\\', '/'], $separator, $path);
} }
} }

Loading…
Cancel
Save