| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Forms\Controls; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Forms\Form; |
| 12 |
use Packetery\Nette\Utils\Strings; |
| 13 |
/** |
| 14 |
* Implements the basic functionality common to text input controls. |
| 15 |
*/ |
| 16 |
abstract class TextBase extends BaseControl |
| 17 |
{ |
| 18 |
/** @var string */ |
| 19 |
protected $emptyValue = ''; |
| 20 |
/** @var mixed unfiltered submitted value */ |
| 21 |
protected $rawValue = ''; |
| 22 |
/** @var bool */ |
| 23 |
private $nullable; |
| 24 |
/** |
| 25 |
* Sets control's value. |
| 26 |
* @return static |
| 27 |
* @internal |
| 28 |
*/ |
| 29 |
public function setValue($value) |
| 30 |
{ |
| 31 |
if ($value === null) { |
| 32 |
$value = ''; |
| 33 |
} elseif (!\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) { |
| 34 |
throw new \Packetery\Nette\InvalidArgumentException(\sprintf("Value must be scalar or null, %s given in field '%s'.", \gettype($value), $this->name)); |
| 35 |
} |
| 36 |
$this->value = $value; |
| 37 |
$this->rawValue = (string) $value; |
| 38 |
return $this; |
| 39 |
} |
| 40 |
/** |
| 41 |
* Returns control's value. |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public function getValue() |
| 45 |
{ |
| 46 |
$value = $this->value === Strings::trim($this->translate($this->emptyValue)) ? '' : $this->value; |
| 47 |
return $this->nullable && $value === '' ? null : $value; |
| 48 |
} |
| 49 |
/** |
| 50 |
* Sets whether getValue() returns null instead of empty string. |
| 51 |
* @return static |
| 52 |
*/ |
| 53 |
public function setNullable(bool $value = \true) |
| 54 |
{ |
| 55 |
$this->nullable = $value; |
| 56 |
return $this; |
| 57 |
} |
| 58 |
/** |
| 59 |
* Sets the special value which is treated as empty string. |
| 60 |
* @return static |
| 61 |
*/ |
| 62 |
public function setEmptyValue(string $value) |
| 63 |
{ |
| 64 |
$this->emptyValue = $value; |
| 65 |
return $this; |
| 66 |
} |
| 67 |
/** |
| 68 |
* Returns the special value which is treated as empty string. |
| 69 |
*/ |
| 70 |
public function getEmptyValue() : string |
| 71 |
{ |
| 72 |
return $this->emptyValue; |
| 73 |
} |
| 74 |
/** |
| 75 |
* Sets the maximum number of allowed characters. |
| 76 |
* @return static |
| 77 |
*/ |
| 78 |
public function setMaxLength(int $length) |
| 79 |
{ |
| 80 |
$this->control->maxlength = $length; |
| 81 |
return $this; |
| 82 |
} |
| 83 |
public function getControl() : \Packetery\Nette\Utils\Html |
| 84 |
{ |
| 85 |
$el = parent::getControl(); |
| 86 |
if ($this->emptyValue !== '') { |
| 87 |
$el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue)); |
| 88 |
} |
| 89 |
if (isset($el->placeholder)) { |
| 90 |
$el->placeholder = $this->translate($el->placeholder); |
| 91 |
} |
| 92 |
return $el; |
| 93 |
} |
| 94 |
protected function getRenderedValue() : ?string |
| 95 |
{ |
| 96 |
return $this->rawValue === '' ? $this->emptyValue === '' ? null : $this->translate($this->emptyValue) : $this->rawValue; |
| 97 |
} |
| 98 |
/** @return static */ |
| 99 |
public function addRule($validator, $errorMessage = null, $arg = null) |
| 100 |
{ |
| 101 |
foreach ($this->getRules() as $rule) { |
| 102 |
if (!$rule->canExport() && !$rule->branch) { |
| 103 |
return parent::addRule($validator, $errorMessage, $arg); |
| 104 |
} |
| 105 |
} |
| 106 |
if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) { |
| 107 |
$tmp = \is_array($arg) ? $arg[1] : $arg; |
| 108 |
if (\is_scalar($tmp)) { |
| 109 |
$this->control->maxlength = isset($this->control->maxlength) ? \min($this->control->maxlength, $tmp) : $tmp; |
| 110 |
} |
| 111 |
} |
| 112 |
return parent::addRule($validator, $errorMessage, $arg); |
| 113 |
} |
| 114 |
} |
| 115 |
|