+Базовая версия пакета форм

This commit is contained in:
Andrey Pokidov
2020-03-18 16:23:08 +07:00
commit 35cc11bc59
40 changed files with 2760 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractVisibleField;
/**
* Description of TextField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class CheckboxField extends AbstractVisibleField
{
private $checked = false;
public function __construct($form, $name)
{
parent::__construct($form, 'checkbox', $name);
}
/**
* Возвращает ранее заданное значение поля
* @return boolean Метод возвращает true, если поле отмечено флагом, иначе - false
*/
public function value()
{
return $this->checked;
}
/**
* Устанавливает новое значение для поля
* @param boolean $newValue Необходимо передать true, если необходимо установить флаг, false, чтобы убрать флаг
* @return $this
*/
public function setValue($newValue)
{
$this->checked = !empty($newValue);
return $this;
}
public function getAssociativeAttributes()
{
$attributes = parent::getAssociativeAttributes();
$attributes['value'] = 1;
if ($this->checked) {
$attributes['checked'] = null;
}
return $attributes;
}
}

18
src/Fields/EmailField.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractTextField;
/**
* Description of EmailField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class EmailField extends AbstractTextField
{
public function __construct($form, $name, $value = '')
{
parent::__construct($form, 'email', $name, $value);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractField;
/**
* Description of HiddenField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class HiddenField extends AbstractField
{
private $value;
public function __construct($form, $name, $value = '')
{
parent::__construct($form, 'hidden', $name);
$this->setValue($value);
}
/**
* Возвращает значение поля
* @return string
*/
public function value()
{
return $this->value;
}
/**
* Задаёт новое значение для поля типа hidden
* @param string|int|float $newValue
* @return $this
*/
public function setValue($newValue)
{
if (is_string($newValue) || is_numeric($newValue)) {
$this->value = strval($newValue);
}
return $this;
}
public function getAssociativeAttributes()
{
$attributes = parent::getAssociativeAttributes();
$attributes['value'] = $this->value;
return $attributes;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractTextField;
/**
* Description of PasswordField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class PasswordField extends AbstractTextField
{
public function __construct($form, $name)
{
parent::__construct($form, 'password', $name, '');
}
public function setValue($newValue)
{
//No value need to be written
return $this;
}
}

54
src/Fields/RadioField.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractChoiceField;
use Artmark\Forms\AbstractChoiceOption;
/**
* Description of RadioField
*
* @author Andrey Pokidov <andrey.pokidov@gmail.com>
*/
class RadioField extends AbstractChoiceField
{
public function __construct($form, $name)
{
parent::__construct($form, 'radio', $name);
}
/**
* Создаёт новый экземпляр опции
* @return RadioOption
*/
protected function createOption($value, $text)
{
return new RadioOption($this, $value, $text);
}
public function appendAttributesToOption(AbstractChoiceOption $option, array & $attributes)
{
if ($this->isSelected($option)) {
$attributes['checked'] = null;
}
if (!$option->hasCssClasses()) {
$this->appendCssClassAttribute($attributes);
}
if (!$option->hasStyle()) {
$this->appendStyleAttribute($attributes);
}
if ($this->isDisabled()) {
$this->appendDisabledAttribute($attributes);
}
$attributes['name'] = $this->name();
if (!$option->hasId()) {
$option->setId($this->name() . '_' . $option->value());
$attributes['id'] = $option->id();
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractChoiceOption;
/**
* Description of RadioOption
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class RadioOption extends AbstractChoiceOption
{
/**
* The field which is the owner of the option
* @var RadioField
*/
private $field;
public function __construct(RadioField $field, $value, $text)
{
parent::__construct($value, $text);
$this->field = $field;
}
/**
* Метод возвращает поле типа RadioField
* @return RadioField
*/
public function field()
{
return $this->field;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractChoiceField;
use Artmark\Forms\AbstractChoiceOption;
use Artmark\Forms\Attributes\SizeAttribute;
/**
* Description of TextField
*
* @author Andrey Pokidov <andrey.pokidov@gmail.com>
*/
class SelectField extends AbstractChoiceField
{
use SizeAttribute;
public function __construct($form, $name)
{
parent::__construct($form, 'select', $name);
}
public function getAssociativeAttributes()
{
$attributes = parent::getAssociativeAttributes();
$this->appendSizeAttribute($attributes);
if ($this->isMultiSelect()) {
$attributes['multiple'] = null;
}
return $attributes;
}
/**
* Создаёт новый экземпляр опции
* @return SelectOption
*/
protected function createOption($value, $text)
{
return new SelectOption($this, $value, $text);
}
public function appendAttributesToOption(AbstractChoiceOption $option, array & $attributes)
{
if ($this->isSelected($option)) {
$attributes['selected'] = null;
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractChoiceOption;
/**
* Description of SelectionOption
*
* @author Andrey Pokidov <andrey.pokidov@gmail.com>
*/
class SelectOption extends AbstractChoiceOption
{
/**
* The field which is the owner of the option
* @var SelectField
*/
private $field;
public function __construct(SelectField $field, $value, $text)
{
parent::__construct($value, $text);
$this->field = $field;
}
/**
* Метод возвращает поле типа SelectField
* @return SelectField
*/
public function field()
{
return $this->field;
}
}

18
src/Fields/TextField.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace Artmark\Forms\Fields;
use Artmark\Forms\AbstractTextField;
/**
* Description of TextField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class TextField extends AbstractTextField
{
public function __construct($form, $name, $value = '')
{
parent::__construct($form, 'text', $name, $value);
}
}