Skip to content

Commit

Permalink
added type hints (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 19, 2024
1 parent 61456ba commit 43e8f59
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 45 deletions.
5 changes: 2 additions & 3 deletions src/Forms/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ interface Control
/**
* Sets control's value.
* @param mixed $value
* @return static
*/
function setValue(mixed $value);
function setValue(mixed $value): static;

/**
* Returns control's value.
* @return mixed
*/
function getValue();
function getValue(): mixed;

function validate(): void;

Expand Down
21 changes: 8 additions & 13 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ public function getHtmlName(): string

/**
* Sets control's value.
* @return static
* @internal
*/
public function setValue(mixed $value)
public function setValue($value): static
{
$this->value = $value;
return $this;
Expand All @@ -147,7 +146,7 @@ public function setValue(mixed $value)
* Returns control's value.
* @return mixed
*/
public function getValue()
public function getValue(): mixed
{
return $this->value;
}
Expand All @@ -165,9 +164,8 @@ public function isFilled(): bool

/**
* Sets control's default value.
* @return static
*/
public function setDefaultValue($value)
public function setDefaultValue($value): static
{
$form = $this->getForm(throw: false);
if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
Expand All @@ -180,9 +178,8 @@ public function setDefaultValue($value)

/**
* Disables or enables control.
* @return static
*/
public function setDisabled(bool $state = true)
public function setDisabled(bool $state = true): static
{
$this->disabled = $state;
if ($state) {
Expand Down Expand Up @@ -228,9 +225,8 @@ public function isOmitted(): bool

/**
* Generates control's HTML element.
* @return Html|string
*/
public function getControl()
public function getControl(): Html|string
{
$this->setOption('rendered', true);
$el = clone $this->control;
Expand All @@ -246,9 +242,8 @@ public function getControl()

/**
* Generates label's HTML element.
* @return Html|string|null
*/
public function getLabel(string|Stringable|null $caption = null)
public function getLabel(string|Stringable|null $caption = null): Html|string|null
{
$label = clone $this->label;
$label->for = $this->getHtmlId();
Expand Down Expand Up @@ -396,13 +391,13 @@ public function translate($value, ...$parameters): mixed

/**
* Adds a validation rule.
* @return static
*/
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
) {
): static
{
$this->rules->addRule($validator, $errorMessage, $arg);
return $this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Forms/Controls/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public function __construct(string|Stringable|null $label = null)

/**
* Sets control's value.
* @return static
* @internal
*/
public function setValue($value)
public function setValue($value): static
{
if (!is_scalar($value) && $value !== null) {
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName()));
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public function loadHttpData(): void
/**
* Sets selected item (by key).
* @param string|int|\BackedEnum|\Stringable|null $value
* @return static
* @internal
*/
public function setValue($value)
public function setValue($value): static
{
if ($value === null) {
$this->value = null;
Expand Down Expand Up @@ -105,9 +104,8 @@ public function isFilled(): bool

/**
* Sets items from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
public function setItems(array $items, bool $useKeys = true): static
{
$this->choices = [];
foreach ($items as $k => $v) {
Expand Down
3 changes: 1 addition & 2 deletions src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public function __construct($persistentValue = null)

/**
* Sets control's value.
* @return static
* @internal
*/
public function setValue($value)
public function setValue($value): static
{
if ($value === null) {
$value = '';
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public function loadHttpData(): void

/**
* Sets selected items (by keys).
* @return static
* @internal
*/
public function setValue($values)
public function setValue($values): static
{
if (is_scalar($values) || $values === null) {
$values = (array) $values;
Expand Down Expand Up @@ -98,9 +97,8 @@ public function getRawValue(): array

/**
* Sets items from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
public function setItems(array $items, bool $useKeys = true): static
{
$this->choices = [];
foreach ($items as $k => $v) {
Expand Down
3 changes: 1 addition & 2 deletions src/Forms/Controls/MultiSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public function __construct($label = null, ?array $items = null)

/**
* Sets options and option groups from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
public function setItems(array $items, bool $useKeys = true): static
{
if (!$useKeys) {
$res = [];
Expand Down
3 changes: 1 addition & 2 deletions src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ public function getPrompt(): string|Stringable|false

/**
* Sets options and option groups from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
public function setItems(array $items, bool $useKeys = true): static
{
if (!$useKeys) {
$res = [];
Expand Down
7 changes: 3 additions & 4 deletions src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ abstract class TextBase extends BaseControl

/**
* Sets control's value.
* @return static
* @internal
*/
public function setValue($value)
public function setValue($value): static
{
if ($value === null) {
$value = '';
Expand Down Expand Up @@ -125,12 +124,12 @@ protected function getRenderedValue(): ?string
}


/** @return static */
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
) {
): static
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return parent::addRule($validator, $errorMessage, $arg);
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function getControl(): Nette\Utils\Html
}


/** @return static */
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
) {
): static
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return parent::addRule($validator, $errorMessage, $arg);
Expand Down
7 changes: 3 additions & 4 deletions src/Forms/Controls/UploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ public function getHtmlName(): string


/**
* @return static
* @internal
*/
public function setValue($value)
public function setValue($value): static
{
return $this;
}
Expand Down Expand Up @@ -121,12 +120,12 @@ public function isOk(): bool
}


/** @return static */
public function addRule(
callable|string $validator,
string|Stringable|null $errorMessage = null,
mixed $arg = null,
) {
): static
{
if ($validator === Form::Image) {
$this->control->accept = implode(', ', Forms\Helpers::getSupportedImages());

Expand Down
4 changes: 1 addition & 3 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ class Form extends Container implements Nette\HtmlStringable

/** @internal used only by standalone form */
public Nette\Http\IRequest $httpRequest;

/** @var bool */
protected $crossOrigin = false;
protected bool $crossOrigin = false;
private static ?Nette\Http\IRequest $defaultHttpRequest = null;
private SubmitterControl|bool $submittedBy = false;
private array $httpData;
Expand Down

0 comments on commit 43e8f59

Please sign in to comment.