fenom/src/Fenom/Tag.php

300 lines
7.1 KiB
PHP
Raw Normal View History

2014-04-12 01:00:58 +04:00
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom;
2014-04-17 23:27:59 +04:00
class Tag extends \ArrayObject
{
const COMPILER = 1;
2014-05-06 14:22:58 +04:00
const FUNC = 2;
const BLOCK = 4;
2014-04-12 01:00:58 +04:00
2014-05-06 00:45:37 +04:00
const LTRIM = 1;
const RTRIM = 2;
2014-04-12 01:00:58 +04:00
/**
* @var Template
*/
2023-02-06 00:18:18 +03:00
public Template $tpl;
public string $name;
public array $options = [];
public int $line = 0;
public int $level = 0;
public mixed $callback;
public bool $escape;
2014-04-12 01:00:58 +04:00
2023-02-06 00:18:18 +03:00
private int $_offset = 0;
private bool $_closed = true;
private string $_body;
private int $_type = 0;
2023-02-22 00:09:00 +03:00
private mixed $_open;
private mixed $_close;
2023-02-06 00:18:18 +03:00
private array $_tags = [];
private array $_floats = [];
private array $_changed = [];
2014-04-17 23:27:59 +04:00
/**
* Create tag entity
* @param string $name the tag name
* @param Template $tpl current template
2023-02-06 00:18:18 +03:00
* @param array $info tag's information
2014-04-17 23:27:59 +04:00
* @param string $body template's code
*/
2023-02-06 00:18:18 +03:00
public function __construct(string $name, Template $tpl, array $info, string &$body)
{
2023-02-06 00:18:18 +03:00
parent::__construct();
2014-05-06 14:22:58 +04:00
$this->tpl = $tpl;
$this->name = $name;
$this->line = $tpl->getLine();
$this->level = $tpl->getStackSize();
$this->_body = & $body;
$this->_offset = strlen($body);
2014-05-06 14:22:58 +04:00
$this->_type = $info["type"];
$this->escape = $tpl->getOptions() & \Fenom::AUTO_ESCAPE;
2014-04-17 23:27:59 +04:00
if ($this->_type & self::BLOCK) {
2014-05-06 14:22:58 +04:00
$this->_open = $info["open"];
$this->_close = $info["close"];
2023-02-06 00:18:18 +03:00
$this->_tags = $info["tags"] ?? [];
$this->_floats = $info["float_tags"] ?? [];
$this->_closed = false;
2014-04-17 23:27:59 +04:00
} else {
$this->_open = $info["parser"];
}
2014-04-17 23:27:59 +04:00
if ($this->_type & self::FUNC) {
$this->callback = $info["function"];
}
}
2014-04-17 23:27:59 +04:00
/**
* Set tag option
* @param string $option
2014-05-06 00:45:37 +04:00
* @throws \RuntimeException
*/
2023-02-06 00:18:18 +03:00
public function tagOption(string $option)
2014-05-06 00:45:37 +04:00
{
2014-05-06 14:22:58 +04:00
if (method_exists($this, 'opt' . $option)) {
2014-05-06 00:45:37 +04:00
$this->options[] = $option;
} else {
throw new \RuntimeException("Unknown tag option $option");
}
}
/**
* Rewrite template option for tag. When tag will be closed option will be reverted.
* @param int $option option constant
* @param bool $value true add option, false remove option
*/
2023-02-06 00:18:18 +03:00
public function setOption(int $option, bool $value)
2014-05-06 14:22:58 +04:00
{
2014-05-06 00:45:37 +04:00
$actual = (bool)($this->tpl->getOptions() & $option);
2014-05-06 14:22:58 +04:00
if ($actual != $value) {
2014-05-06 00:45:37 +04:00
$this->_changed[$option] = $actual;
$this->tpl->setOption(\Fenom::AUTO_ESCAPE, $value);
}
}
/**
* Restore the option
* @param int $option
2014-04-17 23:27:59 +04:00
*/
2023-02-06 00:18:18 +03:00
public function restore(int $option)
2014-04-17 23:27:59 +04:00
{
2014-05-06 14:22:58 +04:00
if (isset($this->_changed[$option])) {
2014-05-06 00:45:37 +04:00
$this->tpl->setOption($option, $this->_changed[$option]);
unset($this->_changed[$option]);
}
}
2014-05-06 00:45:37 +04:00
public function restoreAll()
{
2014-05-06 14:22:58 +04:00
foreach ($this->_changed as $option => $value) {
2014-05-06 00:45:37 +04:00
$this->tpl->setOption($option, $this->_changed[$option]);
unset($this->_changed[$option]);
}
}
2014-04-17 23:27:59 +04:00
/**
* Check, if the tag closed
* @return bool
*/
2023-02-06 00:18:18 +03:00
public function isClosed(): bool
2014-04-17 23:27:59 +04:00
{
return $this->_closed;
}
/**
* Open callback
*
* @param Tokenizer $tokenizer
* @return mixed
*/
2023-02-06 00:18:18 +03:00
public function start(Tokenizer $tokenizer): mixed
{
2014-05-06 14:22:58 +04:00
foreach ($this->options as $option) {
$option = 'opt' . $option;
2014-05-06 00:45:37 +04:00
$this->$option();
}
return call_user_func($this->_open, $tokenizer, $this);
}
/**
* Check, has the block this tag
*
* @param string $tag
* @param int $level
* @return bool
*/
2023-02-06 00:18:18 +03:00
public function hasTag(string $tag, int $level): bool
{
if (isset($this->_tags[$tag])) {
if ($level) {
return isset($this->_floats[$tag]);
} else {
return true;
}
}
return false;
}
/**
* Call tag callback
*
* @param string $tag
* @param Tokenizer $tokenizer
* @return string
2023-02-06 00:18:18 +03:00
* @throws \LogicException
*/
2023-02-06 00:18:18 +03:00
public function tag(string $tag, Tokenizer $tokenizer): string
{
if (isset($this->_tags[$tag])) {
return call_user_func($this->_tags[$tag], $tokenizer, $this);
} else {
throw new \LogicException("The block tag {$this->name} no have tag {$tag}");
}
}
/**
* Close callback
*
* @param Tokenizer $tokenizer
* @return string
2023-02-06 00:18:18 +03:00
* @throws \LogicException
*/
2023-02-06 00:18:18 +03:00
public function end(Tokenizer $tokenizer): string
{
2014-04-17 23:27:59 +04:00
if ($this->_closed) {
throw new \LogicException("Tag {$this->name} already closed");
}
2014-04-17 23:27:59 +04:00
if ($this->_close) {
2014-05-06 14:22:58 +04:00
foreach ($this->options as $option) {
$option = 'opt' . $option . 'end';
if (method_exists($this, $option)) {
2014-05-06 00:45:37 +04:00
$this->$option();
}
}
$code = call_user_func($this->_close, $tokenizer, $this);
$this->restoreAll();
2023-02-20 00:14:08 +03:00
return (string)$code;
} else {
2014-04-17 23:32:44 +04:00
throw new \LogicException("Can not use a inline tag {$this->name} as a block");
}
}
2014-04-17 23:27:59 +04:00
/**
* Forcefully close the tag
*/
public function close()
{
$this->_closed = true;
}
/**
2016-05-06 23:04:08 +03:00
* Returns tag's content
*
* @throws \LogicException
* @return string
*/
2023-02-06 00:18:18 +03:00
public function getContent(): string
{
return substr($this->_body, $this->_offset);
}
/**
2016-05-06 23:04:08 +03:00
* Cut tag's content
*
* @return string
* @throws \LogicException
*/
2023-02-06 00:18:18 +03:00
public function cutContent(): string
{
2016-11-18 22:07:14 +03:00
$content = substr($this->_body, $this->_offset);
$this->_body = substr($this->_body, 0, $this->_offset);
return $content;
}
/**
2016-05-06 23:04:08 +03:00
* Replace tag's content
*
* @param $new_content
*/
public function replaceContent($new_content)
{
$this->cutContent();
$this->_body .= $new_content;
}
2014-05-06 00:45:37 +04:00
/**
* Generate output code
* @param string $code
* @return string
*/
2023-02-06 00:18:18 +03:00
public function out(string $code): string
2014-04-17 23:27:59 +04:00
{
2014-05-06 00:45:37 +04:00
return $this->tpl->out($code, $this->escape);
2014-04-12 01:00:58 +04:00
}
2014-05-08 12:56:37 +04:00
/**
* Enable escape option for the tag
*/
2014-05-06 00:45:37 +04:00
public function optEscape()
2014-04-17 23:27:59 +04:00
{
2014-05-06 00:45:37 +04:00
$this->escape = true;
2014-04-12 01:00:58 +04:00
}
2014-05-08 12:56:37 +04:00
/**
* Disable escape option for the tag
*/
2014-04-17 23:27:59 +04:00
public function optRaw()
{
2014-05-06 00:45:37 +04:00
$this->escape = false;
2014-04-12 01:00:58 +04:00
}
2014-05-08 12:56:37 +04:00
/**
* Enable strip spaces option for the tag
*/
public function optStrip()
{
$this->setOption(\Fenom::AUTO_STRIP, true);
}
/**
* Enable ignore for body of the tag
*/
public function optIgnore()
{
if(!$this->isClosed()) {
$this->tpl->ignore($this->name);
}
}
2014-04-12 01:00:58 +04:00
}