fenom/src/Cytro/Scope.php

139 lines
3.0 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-04-28 11:33:36 +04:00
/*
* This file is part of Cytro.
*
* (c) 2013 Ivan Shalganov
*
2013-04-28 18:08:57 +04:00
* For the full copyright and license information, please view the license.md
2013-04-28 11:33:36 +04:00
* file that was distributed with this source code.
*/
2013-04-04 10:56:44 +04:00
namespace Cytro;
2013-01-25 18:36:16 +04:00
/**
* Scope for blocks tags
*/
class Scope extends \ArrayObject {
public $line = 0;
public $name;
2013-02-20 18:12:44 +04:00
public $level = 0;
/**
* @var Template
*/
public $tpl;
public $is_compiler = true;
2013-03-17 14:37:23 +04:00
public $is_closed = false;
private $_action;
2013-02-27 20:55:08 +04:00
private $_body;
private $_offset;
2013-01-25 18:36:16 +04:00
/**
2013-02-27 20:55:08 +04:00
* Creating cope
*
2013-01-25 18:36:16 +04:00
* @param string $name
* @param Template $tpl
* @param int $line
* @param array $action
2013-02-20 18:12:44 +04:00
* @param int $level
2013-02-27 20:55:08 +04:00
* @param $body
2013-01-25 18:36:16 +04:00
*/
2013-02-27 20:55:08 +04:00
public function __construct($name, $tpl, $line, $action, $level, &$body) {
$this->line = $line;
$this->name = $name;
$this->tpl = $tpl;
$this->_action = $action;
$this->level = $level;
2013-02-27 20:55:08 +04:00
$this->_body = &$body;
$this->_offset = strlen($body);
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
*
* @param string $function
*/
2013-01-25 18:36:16 +04:00
public function setFuncName($function) {
$this["function"] = $function;
$this->is_compiler = false;
}
2013-02-20 18:12:44 +04:00
/**
* Open callback
*
* @param Tokenizer $tokenizer
* @return mixed
*/
2013-01-25 18:36:16 +04:00
public function open($tokenizer) {
2013-02-27 20:55:08 +04:00
return call_user_func($this->_action["open"], $tokenizer, $this);
2013-01-25 18:36:16 +04:00
}
2013-02-20 18:12:44 +04:00
/**
* Check, has the block this tag
*
* @param string $tag
* @param int $level
* @return bool
*/
public function hasTag($tag, $level) {
if(isset($this->_action["tags"][$tag])) {
2013-01-25 18:36:16 +04:00
if($level) {
return isset($this->_action["float_tags"][$tag]);
} else {
return true;
}
}
return false;
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
* Call tag callback
*
* @param string $tag
* @param Tokenizer $tokenizer
* @return string
*/
public function tag($tag, $tokenizer) {
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
* Close callback
*
* @param Tokenizer $tokenizer
* @return string
*/
2013-01-25 18:36:16 +04:00
public function close($tokenizer) {
return call_user_func($this->_action["close"], $tokenizer, $this);
}
/**
2013-02-20 18:12:44 +04:00
* Return content of block
*
* @throws \LogicException
* @return string
2013-01-25 18:36:16 +04:00
*/
2013-02-20 18:12:44 +04:00
public function getContent() {
2013-02-27 20:55:08 +04:00
return substr($this->_body, $this->_offset);
2013-01-25 18:36:16 +04:00
}
2013-02-26 23:56:06 +04:00
/**
2013-02-27 20:55:08 +04:00
* Cut scope content
*
2013-02-26 23:56:06 +04:00
* @return string
* @throws \LogicException
*/
public function cutContent() {
2013-02-27 20:55:08 +04:00
$content = substr($this->_body, $this->_offset + 1);
$this->_body = substr($this->_body, 0, $this->_offset);
return $content;
}
/**
* Replace scope content
*
* @param $new_content
*/
public function replaceContent($new_content) {
$this->cutContent();
$this->_body .= $new_content;
2013-02-26 23:56:06 +04:00
}
2013-01-25 18:36:16 +04:00
}