template/src/Template.php

167 lines
4.6 KiB
PHP
Raw Normal View History

2017-06-10 15:23:03 +00:00
<?php
namespace Krutush\Template;
class Template{
2017-06-20 20:01:55 +00:00
/** @var string */
2017-06-10 15:23:03 +00:00
private $path;
2017-06-20 20:01:55 +00:00
/** @var string */
2017-06-10 15:23:03 +00:00
private $layout;
2017-06-20 20:01:55 +00:00
/** @var array */
2017-06-10 15:23:03 +00:00
private $data = array();
2017-06-20 20:01:55 +00:00
/** @var string */
2017-06-20 20:10:18 +00:00
const EXTENTION = '.tpl';
2017-06-10 15:23:03 +00:00
2017-06-20 20:01:55 +00:00
public function __construct(string $path, string $extention = null, bool $folder = true){
2017-06-10 15:23:03 +00:00
$this->path = $this->path($path, $extention, $folder);
}
2017-06-20 20:01:55 +00:00
public function set(string $key, mixed $value): self{
2017-06-10 15:23:03 +00:00
$this->data[$key] = $value;
return $this;
}
public function sets(array $array){
foreach($array as $key => $value){
$this->set($key, $value);
}
return $this;
}
2017-06-20 20:01:55 +00:00
public function extract(): array{
return [
'sets' => $this->data
];
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
public function insert(array $data): self{
$this->sets($data['sets']);
2017-06-10 15:23:03 +00:00
return $this;
}
2017-06-20 20:01:55 +00:00
public function run(string $output = 'direct'){
2017-06-10 15:23:03 +00:00
switch($output){
case 'array':
case 'direct':
break;
case 'buffer':
ob_start();
break;
default:
trigger_error('Unknow output type '.$output);
break;
}
$callable = function($t, $path){
include($path);
};
$callable($this, $this->path);
if(isset($this->layout)){
2017-06-20 20:01:55 +00:00
$layout = new self($this->layout, '', false);
$layout->insert($this->extract())->run();
2017-06-10 15:23:03 +00:00
}
switch($output){
case 'direct':
break;
case 'buffer':
return ob_get_clean();
case 'array':
2017-06-20 20:01:55 +00:00
return $this->extract();
2017-06-10 15:23:03 +00:00
default:
break;
}
}
2017-06-20 20:01:55 +00:00
public function path(string $path, string $extention = null, bool $folder = true): string{
2017-06-20 20:10:18 +00:00
$path .= $extention ?? self::EXTENTION;
2017-06-20 20:01:55 +00:00
if($folder == true && class_exists(\Krutush\Path)) //Remove require krutush/krutush
$path = \Krutush\Path::get('template').'/'.$path;
return $path;
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
public function _load(string $path, string $extention = null, bool $folder = true): self{
$load = new self($path, $extention, $folder);
$load->insert($this->extract())->run();
$this->insert($load->extract());
2017-06-10 15:23:03 +00:00
return $this;
}
2017-06-20 20:01:55 +00:00
public function _layout(string $path, string $extention = null, bool $folder = true): self{
$this->layout = $this->path($path, $extention, $folder);
return $this;
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
public function _exist(string $key): bool{
return isset($this->data[$key]);
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
public function _x(string $key): bool{ return $this->_exist($key); }
2017-06-10 15:23:03 +00:00
2017-06-20 20:01:55 +00:00
public function _exists(array $keys, bool $all = true){
foreach($keys as $key){
if($this->_exist($key)){
if(!$all)
return true;
}else{
if($all)
return false;
}
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
return $all;
}
public function _xs(array $keys, bool $all = true){ return $this->_exists($keys, $all); }
public static function filter($data, string $key, string $value){
switch($key){
case 'type':
switch($filters['type']){
case 'array':
if(!is_array($data))
return [$data];
break;
case 'string':
if(!is_string($data))
return strval($data);
break;
case 'int':
if(!is_int($data))
return intval($data);
}
break;
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
return $data;
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
public function _get(string $key, array $filters = array()){
if(!$this->_exist($key)){
if(isset($filters['type'])){
switch($filters['type']){
case 'array':
return array();
case 'string':
return '';
case 'int':
return 0;
2017-06-10 15:23:03 +00:00
}
}
2017-06-20 20:01:55 +00:00
return null;
2017-06-10 15:23:03 +00:00
}else{
2017-06-20 20:01:55 +00:00
$data = $this->data[$key];
foreach($filters as $name => $value){
$data = self::filter($data, $name, $value);
2017-06-10 15:23:03 +00:00
}
2017-06-20 20:01:55 +00:00
return $data;
2017-06-10 15:23:03 +00:00
}
}
2017-06-20 20:01:55 +00:00
public function _(string $key, array $filters = array()){ return $this->_get($key, $filters); }
2017-06-20 20:10:18 +00:00
}