PluginProbe
Packeta / 1.6.0
Packeta v1.6.0
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / TextBase.php

TextBase.php in Packeta 1.6.0, at deps/nette/forms/src/Forms/Controls/TextBase.php

124 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
84 * Appends input string filter callback.
85 * @return static
86 */
87 public function addFilter(callable $filter)
88 {
89 $this->getRules()->addFilter($filter);
90 return $this;
91 }
92 public function getControl() : \Packetery\Nette\Utils\Html
93 {
94 $el = parent::getControl();
95 if ($this->emptyValue !== '') {
96 $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
97 }
98 if (isset($el->placeholder)) {
99 $el->placeholder = $this->translate($el->placeholder);
100 }
101 return $el;
102 }
103 protected function getRenderedValue() : ?string
104 {
105 return $this->rawValue === '' ? $this->emptyValue === '' ? null : $this->translate($this->emptyValue) : $this->rawValue;
106 }
107 /** @return static */
108 public function addRule($validator, $errorMessage = null, $arg = null)
109 {
110 foreach ($this->getRules() as $rule) {
111 if (!$rule->canExport() && !$rule->branch) {
112 return parent::addRule($validator, $errorMessage, $arg);
113 }
114 }
115 if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
116 $tmp = \is_array($arg) ? $arg[1] : $arg;
117 if (\is_scalar($tmp)) {
118 $this->control->maxlength = isset($this->control->maxlength) ? \min($this->control->maxlength, $tmp) : $tmp;
119 }
120 }
121 return parent::addRule($validator, $errorMessage, $arg);
122 }
123 }
124