catchAdmin/extend/catcher/CatchForm.php

434 lines
9.9 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
namespace catcher;
/**
* Class CatchForm
* @package catcher
*
*
2019-12-12 09:14:08 +08:00
* @method CatchForm text($column, $label = '', $required = false)
* @method CatchForm image($column, $label = '', $required = false)
* @method CatchForm radio($column, $label = '', $required = false)
* @method CatchForm select($column, $label = '', $required = false)
* @method CatchForm textarea($column, $label = '', $required = false)
* @method CatchForm password($column, $label = '', $required = false)
* @method CatchForm hidden($column, $label = '', $required = false)
* @method CatchForm dom($column, $label = '', $required = false)
2019-12-06 09:17:40 +08:00
*
*/
class CatchForm
{
protected $name;
private $fields = [];
2019-12-07 17:31:38 +08:00
protected $action;
protected $method;
protected $enctype;
protected $formId;
protected $btn;
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $acton
* @return CatchForm
*/
public function action($acton): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->action = $acton;
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $method
* @return CatchForm
*/
public function method($method): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->method = $method;
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $formId
* @return CatchForm
*/
public function formId($formId): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->formId = $formId;
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param string $enctype
* @return CatchForm
*/
public function enctype($enctype ="multipart/form-data"): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->enctype = $enctype;
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $id
* @return CatchForm
*/
public function id($id): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
2019-12-07 17:31:38 +08:00
'id' => sprintf('id="%s"', $id),
2019-12-06 09:17:40 +08:00
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param string $class
* @param string $labelClass
* @param string $inlineClass
* @return CatchForm
*/
public function class($class='', $labelClass = '', $inlineClass = ''): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'class' => $class,
'labelClass' => $labelClass,
'inlineClass' => $inlineClass,
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param array $options
* @return CatchForm
*/
public function options(array $options): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'options' => $options,
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $value
* @return CatchForm
*/
public function default($value): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'default' => $value,
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @return CatchForm
*/
public function disabled(): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'disabled' => '',
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $content
* @return CatchForm
*/
public function placeholder($content): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'placeholder' => 'placeholder='.$content,
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @return CatchForm
*/
public function readonly(): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'readonly' => 'readonly',
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @return string
*/
public function render(): string
2019-12-06 09:17:40 +08:00
{
2019-12-07 17:31:38 +08:00
$form = sprintf('<form id="%s" lay-filter="%s" class="layui-form model-form">', $this->formId, $this->formId);
2019-12-06 09:17:40 +08:00
foreach ($this->fields as $field) {
2019-12-12 09:14:08 +08:00
$form .= in_array($field['type'], ['hidden']) ?
$this->{$field['type'].'Field'}($field)
: sprintf($this->baseField(),
2019-12-06 09:17:40 +08:00
$field['labelClass'] ?? '',
$field['label'],
$field['inlineClass'] ?? '',
2019-12-12 09:14:08 +08:00
$this->{$field['type'].'Field'}($field));
2019-12-06 09:17:40 +08:00
}
2019-12-07 17:31:38 +08:00
return $form . $this->btn. '</form>';
2019-12-06 09:17:40 +08:00
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $append
* @return CatchForm
*/
public function append($append): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'append' => $append,
]);
return $this;
}
2019-12-06 09:17:40 +08:00
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $method
* @param $arguments
* @return $this
*/
2019-12-06 09:17:40 +08:00
public function __call($method, $arguments)
{
// TODO: Implement __call() method.
$this->name = $arguments[0] ?? '';
$label = $arguments[1] ?? '';
2019-12-12 09:14:08 +08:00
$required = $arguments[2] ?? false;
2019-12-06 09:17:40 +08:00
$this->fields[$this->name] = [
'name' => $this->name,
'type' => $method,
2019-12-12 09:14:08 +08:00
'label' => $required ? '<i style="color:red">*</i> '.$label : $label,
2019-12-07 17:31:38 +08:00
'inline' => false,
2019-12-06 09:17:40 +08:00
];
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @return CatchForm
*/
protected function inline(): CatchForm
2019-12-06 09:17:40 +08:00
{
$this->fields[] = array_merge($this->fields, [
'inline' => true,
]);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @return string
*/
private function baseField(): string
2019-12-06 09:17:40 +08:00
{
return
2019-12-07 17:31:38 +08:00
'<div class="layui-form-item">
<label class="layui-form-label%s">%s: </label>
<div class="layui-input-block%s">
2019-12-06 09:17:40 +08:00
%s
</div>
</div>';
}
2019-12-07 17:31:38 +08:00
/**
* form btn
*
* @time 2019年12月06日
* @param $filter
* @param string $position
2019-12-12 09:14:08 +08:00
* @return CatchForm
2019-12-07 17:31:38 +08:00
*/
2019-12-12 09:14:08 +08:00
public function formBtn($filter, $position = 'text-right'): CatchForm
2019-12-07 17:31:38 +08:00
{
$this->btn = sprintf('<div class="layui-form-item %s">
<button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
<button class="layui-btn" lay-filter="%s" lay-submit>保存</button>
</div>', $position, $filter);
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $rule
* @param array $equalTo
* @return CatchForm
*/
public function verify($rule, $equalTo = []): CatchForm
2019-12-07 17:31:38 +08:00
{
if (empty($equalTo)) {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'verify' => sprintf('lay-verType="tips" lay-verify="%s"', $rule),
]);
} else {
[$id, $msg] = $equalTo;
$this->fields[$this->name] = array_merge($this->fields[$this->name], [
'verify' => sprintf(' lay-verType="tips" lay-verify="%s" lay-equalTo="#%s"
lay-equalToText="%s" ', $rule, $id, $msg),
]);
}
return $this;
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
2019-12-06 09:17:40 +08:00
private function textField($field)
{
return
2019-12-07 17:31:38 +08:00
sprintf('<input name="%s" class="layui-input %s" %s value="%s" type="text" %s %s %s%s>',
2019-12-06 09:17:40 +08:00
$field['name'],
2019-12-07 17:31:38 +08:00
$field['id'] ?? '',
$field['class'] ?? '',
2019-12-06 09:17:40 +08:00
$field['default'] ?? '',
$field['readonly'] ?? '',
$field['placeholder'] ?? '',
2019-12-07 17:31:38 +08:00
$field['disabled'] ?? '',
$field['verify'] ?? ''
2019-12-06 09:17:40 +08:00
);
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
2019-12-06 09:17:40 +08:00
private function selectField($field)
{
$select = sprintf('<select name="%s">', $field['name']);
$default = $field['default'] ?? '';
foreach ($field['options'] as $key => $option) {
$select .= sprintf('<option value="%s"%s>%s</option>', $key, $default == $key ? ' selected' : '',$option);
}
return $select . '</select>';
}
2019-12-12 09:14:08 +08:00
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
2019-12-07 17:31:38 +08:00
private function passwordField($field)
{
return sprintf('<input name="%s" class="layui-input" %s type="password" %s %s>',
$field['name'],
$field['id'] ?? '',
$field['verify'] ?? '',
$field['placeholder'] ?? ''
);
}
2019-12-12 09:14:08 +08:00
private function radioField($field)
{
$radio = '';
foreach ($field['options'] as $option) {
$radio .= sprintf('<input name="%s" type="radio" value="%s" title="%s" %s/>',
$field['name'], $option['value'], $option['title'], $option['value'] == $field['default'] ? 'checked' : ''
);
}
2019-12-06 09:17:40 +08:00
2019-12-12 09:14:08 +08:00
return $radio;
}
/**
*
* @time 2019年12月09日
* @param $field
* @return string
*/
private function textareaField($field): string
{
return sprintf('<textarea name="%s" %s class="layui-textarea">%s</textarea>',
$field['name'],
$field['placeholder'] ?? '',
$field['default'] ?? ''
);
}
private function domField($field)
{
return $field['name'];
}
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
private function hiddenField($field): string
{
return sprintf('<input name="%s" value="%s" type="hidden">',
$field['name'], $field['default']
);
}
2019-12-06 09:17:40 +08:00
private function imageField()
{}
}