Finish refactoring widget helper

This commit is contained in:
joyqi
2021-08-23 12:32:18 +08:00
parent b0df985d66
commit 82fe09cf52
16 changed files with 484 additions and 573 deletions

View File

@@ -2,6 +2,7 @@
namespace Typecho\Plugin;
use Typecho\Widget\Helper\Form;
/**
* 插件接口
@@ -32,19 +33,14 @@ interface PluginInterface
/**
* 获取插件配置面板
*
* @static
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
* @param Form $form 配置面板
*/
public static function config(Typecho_Widget_Helper_Form $form);
public static function config(Form $form);
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
* @param Form $form
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form);
public static function personalConfig(Form $form);
}

View File

@@ -1,14 +1,14 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 表单处理帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper;
use Typecho\Cookie;
use Typecho\Validate;
use Typecho\Widget\Helper\Form\Element;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 表单处理帮手
@@ -18,22 +18,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
class Form extends Layout
{
/** 表单post方法 */
const POST_METHOD = 'post';
public const POST_METHOD = 'post';
/** 表单get方法 */
const GET_METHOD = 'get';
public const GET_METHOD = 'get';
/** 标准编码方法 */
const STANDARD_ENCODE = 'application/x-www-form-urlencoded';
public const STANDARD_ENCODE = 'application/x-www-form-urlencoded';
/** 混合编码 */
const MULTIPART_ENCODE = 'multipart/form-data';
public const MULTIPART_ENCODE = 'multipart/form-data';
/** 文本编码 */
const TEXT_ENCODE = 'text/plain';
public const TEXT_ENCODE = 'text/plain';
/**
* 输入元素列表
@@ -41,7 +41,7 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
* @access private
* @var array
*/
private $_inputs = [];
private $inputs = [];
/**
* 构造函数,设置基本属性
@@ -65,11 +65,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 设置表单提交目的
*
* @access public
* @param string $action 表单提交目的
* @return Typecho_Widget_Helper_Form
* @param string|null $action 表单提交目的
* @return $this
*/
public function setAction($action)
public function setAction(?string $action): Form
{
$this->setAttribute('action', $action);
return $this;
@@ -78,11 +77,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 设置表单提交方法
*
* @access public
* @param string $method 表单提交方法
* @return Typecho_Widget_Helper_Form
* @return $this
*/
public function setMethod($method)
public function setMethod(string $method): Form
{
$this->setAttribute('method', $method);
return $this;
@@ -91,11 +89,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 设置表单编码方案
*
* @access public
* @param string $enctype 编码方法
* @return Typecho_Widget_Helper_Form
* @return $this
*/
public function setEncodeType($enctype)
public function setEncodeType(string $enctype): Form
{
$this->setAttribute('enctype', $enctype);
return $this;
@@ -105,12 +102,12 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
* 增加输入元素
*
* @access public
* @param Typecho_Widget_Helper_Form_Element $input 输入元素
* @return Typecho_Widget_Helper_Form
* @param Element $input 输入元素
* @return $this
*/
public function addInput(Typecho_Widget_Helper_Form_Element $input)
public function addInput(Element $input): Form
{
$this->_inputs[$input->name] = $input;
$this->inputs[$input->name] = $input;
$this->addItem($input);
return $this;
}
@@ -118,27 +115,25 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 获取输入项
*
* @access public
* @param string $name 输入项名称
* @return mixed
*/
public function getInput($name)
public function getInput(string $name)
{
return $this->_inputs[$name];
return $this->inputs[$name];
}
/**
* 获取所有输入项的提交值
*
* @access public
* @return array
*/
public function getAllRequest()
public function getAllRequest(): array
{
$result = [];
$source = (self::POST_METHOD == $this->getAttribute('method')) ? $_POST : $_GET;
foreach ($this->_inputs as $name => $input) {
foreach ($this->inputs as $name => $input) {
$result[$name] = $source[$name] ?? null;
}
return $result;
@@ -147,14 +142,13 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 获取此表单的所有输入项固有值
*
* @access public
* @return array
*/
public function getValues()
public function getValues(): array
{
$values = [];
foreach ($this->_inputs as $name => $input) {
foreach ($this->inputs as $name => $input) {
$values[$name] = $input->value;
}
return $values;
@@ -163,30 +157,28 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 获取此表单的所有输入项
*
* @access public
* @return array
*/
public function getInputs()
public function getInputs(): array
{
return $this->_inputs;
return $this->inputs;
}
/**
* 验证表单
*
* @access public
* @return mixed
* @return array
*/
public function validate()
public function validate(): array
{
$validator = new Typecho_Validate();
$validator = new Validate();
$rules = [];
foreach ($this->_inputs as $name => $input) {
foreach ($this->inputs as $name => $input) {
$rules[$name] = $input->rules;
}
$id = md5(implode('"', array_keys($this->_inputs)));
$id = md5(implode('"', array_keys($this->inputs)));
/** 表单值 */
$formData = $this->getParams(array_keys($rules));
@@ -194,10 +186,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
if ($error) {
/** 利用session记录错误 */
Typecho_Cookie::set('__typecho_form_message_' . $id, Json::encode($error));
Cookie::set('__typecho_form_message_' . $id, Json::encode($error));
/** 利用session记录表单值 */
Typecho_Cookie::set('__typecho_form_record_' . $id, Json::encode($formData));
Cookie::set('__typecho_form_record_' . $id, Json::encode($formData));
}
return $error;
@@ -206,11 +198,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 获取提交数据源
*
* @access public
* @param array $params 数据参数集
* @return array
*/
public function getParams(array $params)
public function getParams(array $params): array
{
$result = [];
$source = (self::POST_METHOD == $this->getAttribute('method')) ? $_POST : $_GET;
@@ -225,20 +216,19 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
/**
* 显示表单
*
* @access public
* @return void
*/
public function render()
{
$id = md5(implode('"', array_keys($this->_inputs)));
$record = Typecho_Cookie::get('__typecho_form_record_' . $id);
$message = Typecho_Cookie::get('__typecho_form_message_' . $id);
$id = md5(implode('"', array_keys($this->inputs)));
$record = Cookie::get('__typecho_form_record_' . $id);
$message = Cookie::get('__typecho_form_message_' . $id);
/** 恢复表单值 */
if (!empty($record)) {
$record = Json::decode($record, true);
$message = Json::decode($message, true);
foreach ($this->_inputs as $name => $input) {
$record = \Json::decode($record, true);
$message = \Json::decode($message, true);
foreach ($this->inputs as $name => $input) {
$input->value($record[$name] ?? $input->value);
/** 显示错误消息 */
@@ -247,10 +237,10 @@ class Typecho_Widget_Helper_Form extends Typecho_Widget_Helper_Layout
}
}
Typecho_Cookie::delete('__typecho_form_record_' . $id);
Cookie::delete('__typecho_form_record_' . $id);
}
parent::render();
Typecho_Cookie::delete('__typecho_form_message_' . $id);
Cookie::delete('__typecho_form_message_' . $id);
}
}

View File

@@ -1,14 +1,12 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 表单元素抽象帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 表单元素抽象类
@@ -18,7 +16,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_Layout
abstract class Element extends Layout
{
/**
* 单例唯一id
@@ -27,20 +25,23 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var integer
*/
protected static $uniqueId = 0;
/**
* 表单元素容器
*
* @access public
* @var Typecho_Widget_Helper_Layout
* @var Layout
*/
public $container;
/**
* 输入栏
*
* @access public
* @var Typecho_Widget_Helper_Layout
* @var Layout
*/
public $input;
/**
* inputs
*
@@ -48,13 +49,15 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @access public
*/
public $inputs = [];
/**
* 表单标题
*
* @access public
* @var Typecho_Widget_Helper_Layout
* @var Layout
*/
public $label;
/**
* 表单验证器
*
@@ -62,6 +65,7 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var array
*/
public $rules = [];
/**
* 表单名称
*
@@ -69,6 +73,7 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var string
*/
public $name;
/**
* 表单值
*
@@ -76,6 +81,7 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var mixed
*/
public $value;
/**
* 表单描述
*
@@ -83,6 +89,7 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var string
*/
protected $description;
/**
* 表单消息
*
@@ -90,6 +97,7 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
* @var string
*/
protected $message;
/**
* 多行输入
*
@@ -101,20 +109,28 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 构造函数
*
* @access public
* @param string $name 表单输入项名称
* @param array $options 选择项
* @param string|null $name 表单输入项名称
* @param array|null $options 选择项
* @param mixed $value 表单默认值
* @param string $label 表单标题
* @param string $description 表单描述
* @param string|null $label 表单标题
* @param string|null $description 表单描述
* @return void
*/
public function __construct($name = null, array $options = null, $value = null, $label = null, $description = null)
{
public function __construct(
?string $name = null,
?array $options = null,
$value = null,
?string $label = null,
?string $description = null
) {
/** 创建html元素,并设置class */
parent::__construct('ul', ['class' => 'typecho-option', 'id' => 'typecho-option-item-' . $name . '-' . self::$uniqueId]);
parent::__construct(
'ul',
['class' => 'typecho-option', 'id' => 'typecho-option-item-' . $name . '-' . self::$uniqueId]
);
$this->name = $name;
self::$uniqueId ++;
self::$uniqueId++;
/** 运行自定义初始函数 */
$this->init();
@@ -141,7 +157,6 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 自定义初始函数
*
* @access public
* @return void
*/
public function init()
@@ -151,15 +166,14 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 创建表单标题
*
* @access public
* @param string $value 标题字符串
* @return Typecho_Widget_Helper_Form_Element
* @return $this
*/
public function label($value)
public function label(string $value): Element
{
/** 创建标题元素 */
if (empty($this->label)) {
$this->label = new Typecho_Widget_Helper_Layout('label', ['class' => 'typecho-label']);
$this->label = new Layout('label', ['class' => 'typecho-label']);
$this->container($this->label);
}
@@ -170,15 +184,14 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 在容器里增加元素
*
* @access public
* @param Typecho_Widget_Helper_Layout $item 表单元素
* @param Layout $item 表单元素
* @return $this
*/
public function container(Typecho_Widget_Helper_Layout $item)
public function container(Layout $item): Element
{
/** 创建表单容器 */
if (empty($this->container)) {
$this->container = new Typecho_Widget_Helper_Layout('li');
$this->container = new Layout('li');
$this->addItem($this->container);
}
@@ -189,49 +202,36 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 初始化当前输入项
*
* @access public
* @param Typecho_Widget_Helper_Layout $container 容器对象
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Form_Element
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
abstract public function input($name = null, array $options = null);
abstract public function input(?string $name = null, ?array $options = null): Layout;
/**
* 设置表单元素值
*
* @access public
* @param mixed $value 表单元素值
* @return Typecho_Widget_Helper_Form_Element
* @return Element
*/
public function value($value)
public function value($value): Element
{
$this->value = $value;
$this->_value($value);
$this->inputValue($value);
return $this;
}
/**
* 设置表单元素值
*
* @access protected
* @param mixed $value 表单元素值
* @return void
*/
abstract protected function _value($value);
/**
* 设置描述信息
*
* @access public
* @param string $description 描述信息
* @return Typecho_Widget_Helper_Form_Element
* @return Element
*/
public function description($description)
public function description(string $description): Element
{
/** 创建描述元素 */
if (empty($this->description)) {
$this->description = new Typecho_Widget_Helper_Layout('p', ['class' => 'description']);
$this->description = new Layout('p', ['class' => 'description']);
$this->container($this->description);
}
@@ -242,14 +242,13 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 设置提示信息
*
* @access public
* @param string $message 提示信息
* @return Typecho_Widget_Helper_Form_Element
* @return Element
*/
public function message($message)
public function message(string $message): Element
{
if (empty($this->message)) {
$this->message = new Typecho_Widget_Helper_Layout('p', ['class' => 'message error']);
$this->message = new Layout('p', ['class' => 'message error']);
$this->container($this->message);
}
@@ -260,12 +259,11 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 多行输出模式
*
* @access public
* @return Typecho_Widget_Helper_Layout
* @return Layout
*/
public function multiline()
public function multiline(): Layout
{
$item = new Typecho_Widget_Helper_Layout('span');
$item = new Layout('span');
$this->multiline[] = $item;
return $item;
}
@@ -273,10 +271,9 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 多行输出模式
*
* @access public
* @return Typecho_Widget_Helper_Form_Element
* @return Element
*/
public function multiMode()
public function multiMode(): Element
{
foreach ($this->multiline as $item) {
$item->setAttribute('class', 'multiline');
@@ -287,36 +284,42 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
/**
* 增加验证器
*
* @access public
* @return Typecho_Widget_Helper_Form_Element
* @param mixed ...$rules
* @return $this
*/
public function addRule($name)
public function addRule(...$rules): Element
{
$this->rules[] = func_get_args();
$this->rules[] = $rules;
return $this;
}
/**
* 统一设置所有输入项的属性值
*
* @param $attributeName
* @param $attributeValue
* @param string $attributeName
* @param mixed $attributeValue
*/
public function setInputsAttribute($attributeName, $attributeValue)
public function setInputsAttribute(string $attributeName, $attributeValue)
{
foreach ($this->inputs as $input) {
$input->setAttribute($attributeName, $attributeValue);
}
}
/**
* 设置表单元素值
*
* @param mixed $value 表单元素值
*/
abstract protected function inputValue($value);
/**
* filterValue
*
* @param mixed $value
* @access protected
* @param string $value
* @return string
*/
protected function filterValue($value)
protected function filterValue(string $value): string
{
if (preg_match_all('/[_0-9a-z-]+/i', $value, $matches)) {
return implode('-', $matches[0]);

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 多选框帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 多选框帮手类
@@ -18,63 +17,59 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Checkbox extends Typecho_Widget_Helper_Form_Element
class Checkbox extends Element
{
/**
* 选择值
*
* @access private
* @var array
*/
private $_options = [];
private $options = [];
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
foreach ($options as $value => $label) {
$this->_options[$value] = new Typecho_Widget_Helper_Layout('input');
$this->options[$value] = new Layout('input');
$item = $this->multiline();
$id = $this->name . '-' . $this->filterValue($value);
$this->inputs[] = $this->_options[$value];
$this->inputs[] = $this->options[$value];
$item->addItem($this->_options[$value]->setAttribute('name', $this->name . '[]')
$item->addItem($this->options[$value]->setAttribute('name', $this->name . '[]')
->setAttribute('type', 'checkbox')
->setAttribute('value', $value)
->setAttribute('id', $id));
$labelItem = new Typecho_Widget_Helper_Layout('label');
$labelItem = new Layout('label');
$item->addItem($labelItem->setAttribute('for', $id)->html($label));
$this->container($item);
}
return current($this->_options);
return current($this->options);
}
/**
* 设置表单元素值
*
* @access protected
* @param mixed $value 表单元素值
* @return void
*/
protected function _value($value)
protected function inputValue($value)
{
$values = is_array($value) ? $value : [$value];
foreach ($this->_options as $option) {
foreach ($this->options as $option) {
$option->removeAttribute('checked');
}
foreach ($values as $value) {
if (isset($this->_options[$value])) {
$this->_options[$value]->setAttribute('checked', 'true');
if (isset($this->options[$value])) {
$this->options[$value]->setAttribute('checked', 'true');
}
}
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 虚拟域帮手类
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 虚拟域帮手类
@@ -18,20 +17,18 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Fake extends Typecho_Widget_Helper_Form_Element
class Fake extends Element
{
/**
* 构造函数
*
* @access public
* @param string $name 表单输入项名称
* @param mixed $value 表单默认值
* @return void
*/
public function __construct($name, $value)
public function __construct(string $name, $value)
{
$this->name = $name;
self::$uniqueId ++;
self::$uniqueId++;
/** 运行自定义初始函数 */
$this->init();
@@ -45,27 +42,16 @@ class Typecho_Widget_Helper_Form_Element_Fake extends Typecho_Widget_Helper_Form
}
}
/**
* 自定义初始函数
*
* @access public
* @return void
*/
public function init()
{
}
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('input');
$input = new Layout('input');
$this->inputs[] = $input;
return $input;
}
@@ -73,13 +59,10 @@ class Typecho_Widget_Helper_Form_Element_Fake extends Typecho_Widget_Helper_Form
/**
* 设置表单项默认值
*
* @access protected
* @param string $value 表单项默认值
* @return void
* @param mixed $value 表单项默认值
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->setAttribute('value', $value);
}
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 隐藏域帮手类
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 隐藏域帮手类
@@ -18,12 +17,11 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Hidden extends Typecho_Widget_Helper_Form_Element
class Hidden extends Element
{
/**
* 自定义初始函数
*
* @access public
* @return void
*/
public function init()
@@ -36,13 +34,13 @@ class Typecho_Widget_Helper_Form_Element_Hidden extends Typecho_Widget_Helper_Fo
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('input', ['name' => $name, 'type' => 'hidden']);
$input = new Layout('input', ['name' => $name, 'type' => 'hidden']);
$this->container($input);
$this->inputs[] = $input;
return $input;
@@ -51,11 +49,9 @@ class Typecho_Widget_Helper_Form_Element_Hidden extends Typecho_Widget_Helper_Fo
/**
* 设置表单项默认值
*
* @access protected
* @param string $value 表单项默认值
* @return void
* @param mixed $value 表单项默认值
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->setAttribute('value', htmlspecialchars($value));
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 密码输入表单项帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 密码输入表单项帮手类
@@ -18,19 +17,18 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Password extends Typecho_Widget_Helper_Form_Element
class Password extends Element
{
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'password', 'class' => 'password']);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->container($input);
@@ -41,11 +39,9 @@ class Typecho_Widget_Helper_Form_Element_Password extends Typecho_Widget_Helper_
/**
* 设置表单项默认值
*
* @access protected
* @param string $value 表单项默认值
* @return void
* @param mixed $value 表单项默认值
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->setAttribute('value', htmlspecialchars($value));
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 单选框帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 单选框帮手类
@@ -18,62 +17,58 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Radio extends Typecho_Widget_Helper_Form_Element
class Radio extends Element
{
/**
* 选择值
*
* @access private
* @var array
*/
private $_options = [];
private $options = [];
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
foreach ($options as $value => $label) {
$this->_options[$value] = new Typecho_Widget_Helper_Layout('input');
$this->options[$value] = new Layout('input');
$item = $this->multiline();
$id = $this->name . '-' . $this->filterValue($value);
$this->inputs[] = $this->_options[$value];
$this->inputs[] = $this->options[$value];
$item->addItem($this->_options[$value]->setAttribute('name', $this->name)
$item->addItem($this->options[$value]->setAttribute('name', $this->name)
->setAttribute('type', 'radio')
->setAttribute('value', $value)
->setAttribute('id', $id));
$labelItem = new Typecho_Widget_Helper_Layout('label');
$labelItem = new Layout('label');
$item->addItem($labelItem->setAttribute('for', $id)->html($label));
$this->container($item);
}
return current($this->_options);
return current($this->options);
}
/**
* 设置表单元素值
*
* @access protected
* @param mixed $value 表单元素值
* @return void
*/
protected function _value($value)
protected function inputValue($value)
{
foreach ($this->_options as $option) {
foreach ($this->options as $option) {
$option->removeAttribute('checked');
}
if (isset($this->_options[$value])) {
if (isset($this->options[$value])) {
$this->value = $value;
$this->_options[$value]->setAttribute('checked', 'true');
$this->input = $this->_options[$value];
$this->options[$value]->setAttribute('checked', 'true');
$this->input = $this->options[$value];
}
}
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 下拉选择框帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 下拉选择框帮手类
@@ -18,35 +17,33 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Select extends Typecho_Widget_Helper_Form_Element
class Select extends Element
{
/**
* 选择值
*
* @access private
* @var array
*/
private $_options = [];
private $options = [];
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('select');
$input = new Layout('select');
$this->container($input->setAttribute('name', $name)
->setAttribute('id', $name . '-0-' . self::$uniqueId));
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->inputs[] = $input;
foreach ($options as $value => $label) {
$this->_options[$value] = new Typecho_Widget_Helper_Layout('option');
$input->addItem($this->_options[$value]->setAttribute('value', $value)->html($label));
$this->options[$value] = new Layout('option');
$input->addItem($this->options[$value]->setAttribute('value', $value)->html($label));
}
return $input;
@@ -55,18 +52,16 @@ class Typecho_Widget_Helper_Form_Element_Select extends Typecho_Widget_Helper_Fo
/**
* 设置表单元素值
*
* @access protected
* @param mixed $value 表单元素值
* @return void
*/
protected function _value($value)
protected function inputValue($value)
{
foreach ($this->_options as $option) {
foreach ($this->options as $option) {
$option->removeAttribute('selected');
}
if (isset($this->_options[$value])) {
$this->_options[$value]->setAttribute('selected', 'true');
if (isset($this->options[$value])) {
$this->options[$value]->setAttribute('selected', 'true');
}
}
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 提交按钮表单项帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 提交按钮表单项帮手类
@@ -18,20 +17,19 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Submit extends Typecho_Widget_Helper_Form_Element
class Submit extends Element
{
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$this->setAttribute('class', 'typecho-option typecho-option-submit');
$input = new Typecho_Widget_Helper_Layout('button', ['type' => 'submit']);
$input = new Layout('button', ['type' => 'submit']);
$this->container($input);
$this->inputs[] = $input;
@@ -41,11 +39,9 @@ class Typecho_Widget_Helper_Form_Element_Submit extends Typecho_Widget_Helper_Fo
/**
* 设置表单元素值
*
* @access protected
* @param mixed $value 表单元素值
* @return void
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->html($value);
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 文字输入表单项帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 文字输入表单项帮手类
@@ -18,19 +17,18 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Text extends Typecho_Widget_Helper_Form_Element
class Text extends Element
{
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'text', 'class' => 'text']);
$this->container($input);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
@@ -42,11 +40,9 @@ class Typecho_Widget_Helper_Form_Element_Text extends Typecho_Widget_Helper_Form
/**
* 设置表单项默认值
*
* @access protected
* @param mixed $value 表单项默认值
* @return void
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->setAttribute('value', htmlspecialchars($value));
}

View File

@@ -1,14 +1,13 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 多行文字域帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 多行文字域帮手类
@@ -18,19 +17,18 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Form_Element_Textarea extends Typecho_Widget_Helper_Form_Element
class Textarea extends Element
{
/**
* 初始化当前输入项
*
* @access public
* @param string $name 表单元素名称
* @param array $options 选择项
* @return Typecho_Widget_Helper_Layout
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout
*/
public function input($name = null, array $options = null)
public function input(?string $name = null, ?array $options = null): Layout
{
$input = new Typecho_Widget_Helper_Layout('textarea', ['id' => $name . '-0-' . self::$uniqueId, 'name' => $name]);
$input = new Layout('textarea', ['id' => $name . '-0-' . self::$uniqueId, 'name' => $name]);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->container($input->setClose(false));
$this->inputs[] = $input;
@@ -41,11 +39,9 @@ class Typecho_Widget_Helper_Form_Element_Textarea extends Typecho_Widget_Helper_
/**
* 设置表单项默认值
*
* @access protected
* @param string $value 表单项默认值
* @return void
* @param mixed $value 表单项默认值
*/
protected function _value($value)
protected function inputValue($value)
{
$this->input->html(htmlspecialchars($value));
}

View File

@@ -1,13 +1,6 @@
<?php
/**
* HTML布局帮手
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper;
/**
* HTML布局帮手类
@@ -17,7 +10,7 @@
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_Layout
class Layout
{
/**
* 元素列表
@@ -25,7 +18,7 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var array
*/
private $_items = [];
private $items = [];
/**
* 表单属性列表
@@ -33,7 +26,7 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var array
*/
private $_attributes = [];
private $attributes = [];
/**
* 标签名称
@@ -41,7 +34,7 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var string
*/
private $_tagName = 'div';
private $tagName = 'div';
/**
* 是否自闭合
@@ -49,7 +42,7 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var boolean
*/
private $_close = false;
private $close = false;
/**
* 是否强制自闭合
@@ -57,7 +50,7 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var boolean
*/
private $_forceClose = null;
private $forceClose = null;
/**
* 内部数据
@@ -65,31 +58,30 @@ class Typecho_Widget_Helper_Layout
* @access private
* @var string
*/
private $_html;
private $html;
/**
* 父节点
*
* @access private
* @var Typecho_Widget_Helper_Layout
* @var Layout
*/
private $_parent;
private $parent;
/**
* 构造函数,设置标签名称
*
* @access public
* @param string $tagName 标签名称
* @param array $attributes 属性列表
* @return void
* @param array|null $attributes 属性列表
*
*/
public function __construct($tagName = 'div', array $attributes = null)
public function __construct(string $tagName = 'div', ?array $attributes = null)
{
$this->setTagName($tagName);
if (!empty($attributes)) {
foreach ($attributes as $attributeName => $attributeValue) {
$this->setAttribute($attributeName, $attributeValue);
$this->setAttribute($attributeName, (string)$attributeValue);
}
}
}
@@ -97,75 +89,68 @@ class Typecho_Widget_Helper_Layout
/**
* 设置表单属性
*
* @access public
* @param string $attributeName 属性名称
* @param string $attributeValue 属性值
* @return Typecho_Widget_Helper_Layout
* @param mixed $attributeValue 属性值
* @return $this
*/
public function setAttribute($attributeName, $attributeValue)
public function setAttribute(string $attributeName, $attributeValue): Layout
{
$this->_attributes[$attributeName] = $attributeValue;
$this->attributes[$attributeName] = (string) $attributeValue;
return $this;
}
/**
* 删除元素
*
* @access public
* @param Typecho_Widget_Helper_Layout $item 元素
* @return Typecho_Widget_Helper_Layout
* @param Layout $item 元素
* @return $this
*/
public function removeItem(Typecho_Widget_Helper_Layout $item)
public function removeItem(Layout $item): Layout
{
unset($this->_items[array_search($item, $this->_items)]);
unset($this->items[array_search($item, $this->items)]);
return $this;
}
/**
* getItems
*
* @access public
* @return array
*/
public function getItems()
public function getItems(): array
{
return $this->_items;
return $this->items;
}
/**
* getTagName
*
* @param mixed $tagName
* @access public
* @return void
* @return string
*/
public function getTagName($tagName)
public function getTagName(): string
{
return $this->tagName;
}
/**
* 设置标签名
*
* @access public
* @param string $tagName 标签名
* @return void
*/
public function setTagName($tagName)
public function setTagName(string $tagName)
{
$this->_tagName = $tagName;
$this->tagName = $tagName;
}
/**
* 移除某个属性
*
* @access public
* @param string $attributeName 属性名称
* @return Typecho_Widget_Helper_Layout
* @return $this
*/
public function removeAttribute($attributeName)
public function removeAttribute(string $attributeName): Layout
{
if (isset($this->_attributes[$attributeName])) {
unset($this->_attributes[$attributeName]);
if (isset($this->attributes[$attributeName])) {
unset($this->attributes[$attributeName]);
}
return $this;
@@ -175,59 +160,56 @@ class Typecho_Widget_Helper_Layout
* 获取属性
*
* @access public
*
* @param string $attributeName 属性名
* @return string
* @return string|null
*/
public function getAttribute($attributeName)
public function getAttribute(string $attributeName): ?string
{
return $this->_attributes[$attributeName] ?? null;
return $this->attributes[$attributeName] ?? null;
}
/**
* 设置是否自闭合
*
* @access public
* @param boolean $close 是否自闭合
* @return Typecho_Widget_Helper_Layout
* @return $this
*/
public function setClose($close)
public function setClose(bool $close): Layout
{
$this->_forceClose = $close;
$this->forceClose = $close;
return $this;
}
/**
* 获取父节点
*
* @access public
* @return Typecho_Widget_Helper_Layout
* @return Layout
*/
public function getParent()
public function getParent(): Layout
{
return $this->_parent;
return $this->parent;
}
/**
* 设置父节点
*
* @access public
* @param Typecho_Widget_Helper_Layout $parent 父节点
* @return Typecho_Widget_Helper_Layout
* @param Layout $parent 父节点
* @return $this
*/
public function setParent(Typecho_Widget_Helper_Layout $parent)
public function setParent(Layout $parent): Layout
{
$this->_parent = $parent;
$this->parent = $parent;
return $this;
}
/**
* 增加到某布局元素集合中
*
* @access public
* @param Typecho_Widget_Helper_Layout $parent 布局对象
* @return Typecho_Widget_Helper_Layout
* @param Layout $parent 布局对象
* @return $this
*/
public function appendTo(Typecho_Widget_Helper_Layout $parent)
public function appendTo(Layout $parent): Layout
{
$parent->addItem($this);
return $this;
@@ -236,56 +218,49 @@ class Typecho_Widget_Helper_Layout
/**
* 增加元素
*
* @access public
* @param Typecho_Widget_Helper_Layout $item 元素
* @return Typecho_Widget_Helper_Layout
* @param Layout $item 元素
* @return $this
*/
public function addItem(Typecho_Widget_Helper_Layout $item)
public function addItem(Layout $item): Layout
{
$item->setParent($this);
$this->_items[] = $item;
$this->items[] = $item;
return $this;
}
/**
* 获取属性
*
* @access public
* @param string $attributeName 属性名称
* @return void
* @param string $name 属性名称
* @return string|null
*/
public function __get($name)
public function __get(string $name): ?string
{
return $this->_attributes[$name] ?? null;
return $this->attributes[$name] ?? null;
}
/**
* 设置属性
*
* @access public
* @param string $attributeName 属性名称
* @param string $attributeValue 属性值
* @return void
* @param string $name 属性名称
* @param string $value 属性
*/
public function __set($name, $value)
public function __set(string $name, string $value)
{
$this->_attributes[$name] = $value;
$this->attributes[$name] = $value;
}
/**
* 输出所有元素
*
* @access public
* @return void
*/
public function render()
{
if (empty($this->_items) && empty($this->_html)) {
$this->_close = true;
if (empty($this->items) && empty($this->html)) {
$this->close = true;
}
if (null !== $this->_forceClose) {
$this->_close = $this->_forceClose;
if (null !== $this->forceClose) {
$this->close = $this->forceClose;
}
$this->start();
@@ -295,22 +270,19 @@ class Typecho_Widget_Helper_Layout
/**
* 开始标签
*
* @access public
* @return void
*/
public function start()
{
/** 输出标签 */
echo $this->_tagName ? "<{$this->_tagName}" : null;
echo $this->tagName ? "<{$this->tagName}" : null;
/** 输出属性 */
foreach ($this->_attributes as $attributeName => $attributeValue) {
foreach ($this->attributes as $attributeName => $attributeValue) {
echo " {$attributeName}=\"{$attributeValue}\"";
}
/** 支持自闭合 */
if (!$this->_close && $this->_tagName) {
if (!$this->close && $this->tagName) {
echo ">\n";
}
}
@@ -318,22 +290,21 @@ class Typecho_Widget_Helper_Layout
/**
* 设置内部数据
*
* @access public
* @param mixed $html 内部数据
* @return unknown
* @param string|null $html 内部数据
* @return void|$this
*/
public function html($html = false)
public function html(?string $html = null)
{
if (false === $html) {
if (empty($this->_html)) {
foreach ($this->_items as $item) {
if (null === $html) {
if (empty($this->html)) {
foreach ($this->items as $item) {
$item->render();
}
} else {
echo $this->_html;
echo $this->html;
}
} else {
$this->_html = $html;
$this->html = $html;
return $this;
}
}
@@ -341,13 +312,12 @@ class Typecho_Widget_Helper_Layout
/**
* 结束标签
*
* @access public
* @return void
*/
public function end()
{
if ($this->_tagName) {
echo $this->_close ? " />\n" : "</{$this->_tagName}>\n";
if ($this->tagName) {
echo $this->close ? " />\n" : "</{$this->tagName}>\n";
}
}
}

View File

@@ -1,131 +1,115 @@
<?php
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper;
use Typecho\Widget\Exception;
/**
* 内容分页抽象类
*
* @package Widget
*/
abstract class Typecho_Widget_Helper_PageNavigator
abstract class PageNavigator
{
/**
* 记录总数
*
* @access protected
* @var integer
*/
protected $_total;
protected $total;
/**
* 页面总数
*
* @access protected
* @var integer
*/
protected $_totalPage;
protected $totalPage;
/**
* 当前页面
*
* @access protected
* @var integer
*/
protected $_currentPage;
protected $currentPage;
/**
* 每页内容数
*
* @access protected
* @var integer
*/
protected $_pageSize;
protected $pageSize;
/**
* 页面链接模板
*
* @access protected
* @var string
*/
protected $_pageTemplate;
protected $pageTemplate;
/**
* 链接锚点
*
* @access protected
* @var string
*/
protected $_anchor;
protected $anchor;
/**
* 页面占位符
*
* @access protected
* @var mixed
*/
protected $_pageHolder = ['{page}', '%7Bpage%7D'];
protected $pageHolder = ['{page}', '%7Bpage%7D'];
/**
* 构造函数,初始化页面基本信息
*
* @access public
* @param integer $total 记录总数
* @param integer $page 当前页面
* @param integer $currentPage 当前页面
* @param integer $pageSize 每页记录数
* @param string $pageTemplate 页面链接模板
* @return void
* @throws Exception
*/
public function __construct($total, $currentPage, $pageSize, $pageTemplate)
public function __construct(int $total, int $currentPage, int $pageSize, string $pageTemplate)
{
$this->_total = $total;
$this->_totalPage = ceil($total / $pageSize);
$this->_currentPage = $currentPage;
$this->_pageSize = $pageSize;
$this->_pageTemplate = $pageTemplate;
$this->total = $total;
$this->totalPage = ceil($total / $pageSize);
$this->currentPage = $currentPage;
$this->pageSize = $pageSize;
$this->pageTemplate = $pageTemplate;
if (($currentPage > $this->_totalPage || $currentPage < 1) && $total > 0) {
throw new Typecho_Widget_Exception('Page Not Exists', 404);
if (($currentPage > $this->totalPage || $currentPage < 1) && $total > 0) {
throw new Exception('Page Not Exists', 404);
}
}
/**
* 设置页面占位符
*
* @access protected
* @param string $holder 页面占位符
* @return void
*/
public function setPageHolder($holder)
public function setPageHolder(string $holder)
{
$this->_pageHolder = ['{' . $holder . '}',
$this->pageHolder = ['{' . $holder . '}',
str_replace(['{', '}'], ['%7B', '%7D'], $holder)];
}
/**
* 设置锚点
*
* @access public
* @param string $anchor 锚点
* @return void
*/
public function setAnchor($anchor)
public function setAnchor(string $anchor)
{
$this->_anchor = '#' . $anchor;
$this->anchor = '#' . $anchor;
}
/**
* 输出方法
*
* @access public
* @return void
* @throws Exception
*/
public function render()
{
throw new Typecho_Widget_Exception(get_class($this) . ':' . __METHOD__, 500);
throw new Exception(get_class($this) . ':' . __METHOD__, 500);
}
}

View File

@@ -1,12 +1,12 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\PageNavigator;
use Typecho\Widget\Helper\PageNavigator;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 盒状分页样式
@@ -17,7 +17,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_PageNavigator_Box extends Typecho_Widget_Helper_PageNavigator
class Box extends PageNavigator
{
/**
* 输出盒装样式分页栏
@@ -27,12 +27,17 @@ class Typecho_Widget_Helper_PageNavigator_Box extends Typecho_Widget_Helper_Page
* @param string $nextWord 下一页文字
* @param int $splitPage 分割范围
* @param string $splitWord 分割字符
* @param string $currentClass 当前激活元素class
* @param array $template
* @return void
*/
public function render($prevWord = 'PREV', $nextWord = 'NEXT', $splitPage = 3, $splitWord = '...', array $template = [])
{
if ($this->_total < 1) {
public function render(
string $prevWord = 'PREV',
string $nextWord = 'NEXT',
int $splitPage = 3,
string $splitWord = '...',
array $template = []
) {
if ($this->total < 1) {
return;
}
@@ -70,19 +75,22 @@ class Typecho_Widget_Helper_PageNavigator_Box extends Typecho_Widget_Helper_Page
: $linkBegin;
$linkEnd = '</a>';
$from = max(1, $this->_currentPage - $splitPage);
$to = min($this->_totalPage, $this->_currentPage + $splitPage);
$from = max(1, $this->currentPage - $splitPage);
$to = min($this->totalPage, $this->currentPage + $splitPage);
//输出上一页
if ($this->_currentPage > 1) {
echo $itemPrevBegin . sprintf($linkPrevBegin,
str_replace($this->_pageHolder, $this->_currentPage - 1, $this->_pageTemplate) . $this->_anchor)
if ($this->currentPage > 1) {
echo $itemPrevBegin . sprintf(
$linkPrevBegin,
str_replace($this->pageHolder, $this->currentPage - 1, $this->pageTemplate) . $this->anchor
)
. $prevWord . $linkEnd . $itemEnd;
}
//输出第一页
if ($from > 1) {
echo $itemBegin . sprintf($linkBegin, str_replace($this->_pageHolder, 1, $this->_pageTemplate) . $this->_anchor)
echo $itemBegin
. sprintf($linkBegin, str_replace($this->pageHolder, 1, $this->pageTemplate) . $this->anchor)
. '1' . $linkEnd . $itemEnd;
if ($from > 2) {
@@ -92,28 +100,36 @@ class Typecho_Widget_Helper_PageNavigator_Box extends Typecho_Widget_Helper_Page
}
//输出中间页
for ($i = $from; $i <= $to; $i ++) {
$current = ($i == $this->_currentPage);
for ($i = $from; $i <= $to; $i++) {
$current = ($i == $this->currentPage);
echo ($current ? $itemCurrentBegin : $itemBegin) . sprintf(($current ? $linkCurrentBegin : $linkBegin),
str_replace($this->_pageHolder, $i, $this->_pageTemplate) . $this->_anchor)
echo ($current ? $itemCurrentBegin : $itemBegin) . sprintf(
($current ? $linkCurrentBegin : $linkBegin),
str_replace($this->pageHolder, $i, $this->pageTemplate) . $this->anchor
)
. $i . $linkEnd . $itemEnd;
}
//输出最后页
if ($to < $this->_totalPage) {
if ($to < $this->_totalPage - 1) {
if ($to < $this->totalPage) {
if ($to < $this->totalPage - 1) {
echo $itemBegin . $textBegin . $splitWord . $textEnd . $itemEnd;
}
echo $itemBegin . sprintf($linkBegin, str_replace($this->_pageHolder, $this->_totalPage, $this->_pageTemplate) . $this->_anchor)
. $this->_totalPage . $linkEnd . $itemEnd;
echo $itemBegin
. sprintf(
$linkBegin,
str_replace($this->pageHolder, $this->totalPage, $this->pageTemplate) . $this->anchor
)
. $this->totalPage . $linkEnd . $itemEnd;
}
//输出下一页
if ($this->_currentPage < $this->_totalPage) {
echo $itemNextBegin . sprintf($linkNextBegin,
str_replace($this->_pageHolder, $this->_currentPage + 1, $this->_pageTemplate) . $this->_anchor)
if ($this->currentPage < $this->totalPage) {
echo $itemNextBegin . sprintf(
$linkNextBegin,
str_replace($this->pageHolder, $this->currentPage + 1, $this->pageTemplate) . $this->anchor
)
. $nextWord . $linkEnd . $itemEnd;
}
}

View File

@@ -1,12 +1,12 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Typecho\Widget\Helper\PageNavigator;
use Typecho\Widget\Helper\PageNavigator;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 经典分页样式
@@ -17,7 +17,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Typecho_Widget_Helper_PageNavigator_Classic extends Typecho_Widget_Helper_PageNavigator
class Classic extends PageNavigator
{
/**
* 输出经典样式的分页
@@ -27,7 +27,7 @@ class Typecho_Widget_Helper_PageNavigator_Classic extends Typecho_Widget_Helper_
* @param string $nextWord 下一页文字
* @return void
*/
public function render($prevWord = 'PREV', $nextWord = 'NEXT')
public function render(string $prevWord = 'PREV', string $nextWord = 'NEXT')
{
$this->prev($prevWord);
$this->next($nextWord);
@@ -40,11 +40,13 @@ class Typecho_Widget_Helper_PageNavigator_Classic extends Typecho_Widget_Helper_
* @param string $prevWord 上一页文字
* @return void
*/
public function prev($prevWord = 'PREV')
public function prev(string $prevWord = 'PREV')
{
//输出上一页
if ($this->_total > 0 && $this->_currentPage > 1) {
echo '<a class="prev" href="' . str_replace($this->_pageHolder, $this->_currentPage - 1, $this->_pageTemplate) . $this->_anchor . '">'
if ($this->total > 0 && $this->currentPage > 1) {
echo '<a class="prev" href="'
. str_replace($this->pageHolder, $this->currentPage - 1, $this->pageTemplate)
. $this->anchor . '">'
. $prevWord . '</a>';
}
}
@@ -53,14 +55,16 @@ class Typecho_Widget_Helper_PageNavigator_Classic extends Typecho_Widget_Helper_
* 输出下一页
*
* @access public
* @param string $prevWord 下一页文字
* @param string $nextWord 下一页文字
* @return void
*/
public function next($nextWord = 'NEXT')
public function next(string $nextWord = 'NEXT')
{
//输出下一页
if ($this->_total > 0 && $this->_currentPage < $this->_totalPage) {
echo '<a class="next" title="" href="' . str_replace($this->_pageHolder, $this->_currentPage + 1, $this->_pageTemplate) . $this->_anchor . '">'
if ($this->total > 0 && $this->currentPage < $this->totalPage) {
echo '<a class="next" title="" href="'
. str_replace($this->pageHolder, $this->currentPage + 1, $this->pageTemplate)
. $this->anchor . '">'
. $nextWord . '</a>';
}
}