Skip to content

Commit 3753d36

Browse files
committed
used 'string|Stringable' for labels and messages
1 parent fd59a6b commit 3753d36

17 files changed

+101
-112
lines changed

src/Forms/Container.php

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\ArrayHash;
14+
use Stringable;
1415

1516

1617
/**
@@ -275,22 +276,24 @@ public function getForm(bool $throw = true): ?Form
275276

276277
/**
277278
* Adds single-line text input control to the form.
278-
* @param string|object $label
279279
*/
280-
public function addText(string $name, $label = null, int $cols = null, int $maxLength = null): Controls\TextInput
281-
{
280+
public function addText(
281+
string $name,
282+
string|Stringable $label = null,
283+
int $cols = null,
284+
int $maxLength = null,
285+
): Controls\TextInput {
282286
return $this[$name] = (new Controls\TextInput($label, $maxLength))
283287
->setHtmlAttribute('size', $cols);
284288
}
285289

286290

287291
/**
288292
* Adds single-line text input control used for sensitive input such as passwords.
289-
* @param string|object $label
290293
*/
291294
public function addPassword(
292295
string $name,
293-
$label = null,
296+
string|Stringable $label = null,
294297
int $cols = null,
295298
int $maxLength = null,
296299
): Controls\TextInput {
@@ -302,20 +305,22 @@ public function addPassword(
302305

303306
/**
304307
* Adds multi-line text input control to the form.
305-
* @param string|object $label
306308
*/
307-
public function addTextArea(string $name, $label = null, int $cols = null, int $rows = null): Controls\TextArea
308-
{
309+
public function addTextArea(
310+
string $name,
311+
string|Stringable $label = null,
312+
int $cols = null,
313+
int $rows = null,
314+
): Controls\TextArea {
309315
return $this[$name] = (new Controls\TextArea($label))
310316
->setHtmlAttribute('cols', $cols)->setHtmlAttribute('rows', $rows);
311317
}
312318

313319

314320
/**
315321
* Adds input for email.
316-
* @param string|object $label
317322
*/
318-
public function addEmail(string $name, $label = null): Controls\TextInput
323+
public function addEmail(string $name, string|Stringable $label = null): Controls\TextInput
319324
{
320325
return $this[$name] = (new Controls\TextInput($label))
321326
->addRule(Form::EMAIL);
@@ -324,9 +329,8 @@ public function addEmail(string $name, $label = null): Controls\TextInput
324329

325330
/**
326331
* Adds input for integer.
327-
* @param string|object $label
328332
*/
329-
public function addInteger(string $name, $label = null): Controls\TextInput
333+
public function addInteger(string $name, string|Stringable $label = null): Controls\TextInput
330334
{
331335
return $this[$name] = (new Controls\TextInput($label))
332336
->setNullable()
@@ -336,19 +340,17 @@ public function addInteger(string $name, $label = null): Controls\TextInput
336340

337341
/**
338342
* Adds control that allows the user to upload files.
339-
* @param string|object $label
340343
*/
341-
public function addUpload(string $name, $label = null): Controls\UploadControl
344+
public function addUpload(string $name, string|Stringable $label = null): Controls\UploadControl
342345
{
343346
return $this[$name] = new Controls\UploadControl($label, false);
344347
}
345348

346349

347350
/**
348351
* Adds control that allows the user to upload multiple files.
349-
* @param string|object $label
350352
*/
351-
public function addMultiUpload(string $name, $label = null): Controls\UploadControl
353+
public function addMultiUpload(string $name, string|Stringable $label = null): Controls\UploadControl
352354
{
353355
return $this[$name] = new Controls\UploadControl($label, true);
354356
}
@@ -366,52 +368,54 @@ public function addHidden(string $name, $default = null): Controls\HiddenField
366368

367369
/**
368370
* Adds check box control to the form.
369-
* @param string|object $caption
370371
*/
371-
public function addCheckbox(string $name, $caption = null): Controls\Checkbox
372+
public function addCheckbox(string $name, string|Stringable $caption = null): Controls\Checkbox
372373
{
373374
return $this[$name] = new Controls\Checkbox($caption);
374375
}
375376

376377

377378
/**
378379
* Adds set of radio button controls to the form.
379-
* @param string|object $label
380380
*/
381-
public function addRadioList(string $name, $label = null, array $items = null): Controls\RadioList
381+
public function addRadioList(string $name, string|Stringable $label = null, array $items = null): Controls\RadioList
382382
{
383383
return $this[$name] = new Controls\RadioList($label, $items);
384384
}
385385

386386

387387
/**
388388
* Adds set of checkbox controls to the form.
389-
* @param string|object $label
390389
*/
391-
public function addCheckboxList(string $name, $label = null, array $items = null): Controls\CheckboxList
392-
{
390+
public function addCheckboxList(
391+
string $name,
392+
string|Stringable $label = null,
393+
array $items = null,
394+
): Controls\CheckboxList {
393395
return $this[$name] = new Controls\CheckboxList($label, $items);
394396
}
395397

396398

397399
/**
398400
* Adds select box control that allows single item selection.
399-
* @param string|object $label
400401
*/
401-
public function addSelect(string $name, $label = null, array $items = null, int $size = null): Controls\SelectBox
402-
{
402+
public function addSelect(
403+
string $name,
404+
string|Stringable $label = null,
405+
array $items = null,
406+
int $size = null,
407+
): Controls\SelectBox {
403408
return $this[$name] = (new Controls\SelectBox($label, $items))
404409
->setHtmlAttribute('size', $size > 1 ? $size : null);
405410
}
406411

407412

408413
/**
409414
* Adds select box control that allows multiple item selection.
410-
* @param string|object $label
411415
*/
412416
public function addMultiSelect(
413417
string $name,
414-
$label = null,
418+
string|Stringable $label = null,
415419
array $items = null,
416420
int $size = null
417421
): Controls\MultiSelectBox {
@@ -422,19 +426,17 @@ public function addMultiSelect(
422426

423427
/**
424428
* Adds button used to submit form.
425-
* @param string|object $caption
426429
*/
427-
public function addSubmit(string $name, $caption = null): Controls\SubmitButton
430+
public function addSubmit(string $name, string|Stringable $caption = null): Controls\SubmitButton
428431
{
429432
return $this[$name] = new Controls\SubmitButton($caption);
430433
}
431434

432435

433436
/**
434437
* Adds push buttons with no default behavior.
435-
* @param string|object $caption
436438
*/
437-
public function addButton(string $name, $caption = null): Controls\Button
439+
public function addButton(string $name, string|Stringable $caption = null): Controls\Button
438440
{
439441
return $this[$name] = new Controls\Button($caption);
440442
}

src/Forms/Controls/BaseControl.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Nette\Forms\Form;
1515
use Nette\Forms\Rules;
1616
use Nette\Utils\Html;
17+
use Stringable;
1718

1819

1920
/**
@@ -23,7 +24,7 @@
2324
* @property-read string $htmlName
2425
* @property mixed $htmlId
2526
* @property mixed $value
26-
* @property string|object $caption
27+
* @property string|Stringable $caption
2728
* @property bool $disabled
2829
* @property bool $omitted
2930
* @property-read Html $control
@@ -55,7 +56,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
5556
/** @var callable[][] extension methods */
5657
private static $extMethods = [];
5758

58-
private string|object|null $caption;
59+
private string|Stringable|null $caption;
5960

6061
private array $errors = [];
6162

@@ -68,10 +69,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
6869
private array $options = [];
6970

7071

71-
/**
72-
* @param string|object $caption
73-
*/
74-
public function __construct($caption = null)
72+
public function __construct(string|Stringable $caption = null)
7573
{
7674
$this->control = Html::el('input', ['type' => null, 'name' => null]);
7775
$this->label = Html::el('label');
@@ -88,18 +86,16 @@ public function __construct($caption = null)
8886

8987
/**
9088
* Sets textual caption or label.
91-
* @param object|string $caption
9289
* @return static
9390
*/
94-
public function setCaption($caption): static
91+
public function setCaption(string|Stringable $caption): static
9592
{
9693
$this->caption = $caption;
9794
return $this;
9895
}
9996

10097

101-
/** @return object|string */
102-
public function getCaption()
98+
public function getCaption(): string|Stringable|null
10399
{
104100
return $this->caption;
105101
}
@@ -251,9 +247,8 @@ public function getControl(): Html|string
251247

252248
/**
253249
* Generates label's HTML element.
254-
* @param string|object $caption
255250
*/
256-
public function getLabel($caption = null): Html|string|null
251+
public function getLabel(string|Stringable $caption = null): Html|string|null
257252
{
258253
$label = clone $this->label;
259254
$label->for = $this->getHtmlId();
@@ -397,10 +392,12 @@ public function translate($value, ...$parameters): mixed
397392

398393
/**
399394
* Adds a validation rule.
400-
* @param string|object $errorMessage
401395
*/
402-
public function addRule(callable|string $validator, $errorMessage = null, mixed $arg = null): static
403-
{
396+
public function addRule(
397+
callable|string $validator,
398+
string|Stringable $errorMessage = null,
399+
mixed $arg = null,
400+
): static {
404401
$this->rules->addRule($validator, $errorMessage, $arg);
405402
return $this;
406403
}
@@ -434,9 +431,8 @@ public function getRules(): Rules
434431

435432
/**
436433
* Makes control mandatory.
437-
* @param bool|string|object $value
438434
*/
439-
public function setRequired($value = true): static
435+
public function setRequired(string|Stringable|bool $value = true): static
440436
{
441437
$this->rules->setRequired($value);
442438
return $this;
@@ -467,9 +463,8 @@ public function validate(): void
467463

468464
/**
469465
* Adds error message to the list.
470-
* @param string|object $message
471466
*/
472-
public function addError($message, bool $translate = true): void
467+
public function addError(string|Stringable $message, bool $translate = true): void
473468
{
474469
$this->errors[] = $translate ? $this->translate($message) : $message;
475470
}

src/Forms/Controls/Button.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
namespace Nette\Forms\Controls;
1111

1212
use Nette\Utils\Html;
13+
use Stringable;
1314

1415

1516
/**
1617
* Push button control with no default behavior.
1718
*/
1819
class Button extends BaseControl
1920
{
20-
/**
21-
* @param string|object $caption
22-
*/
23-
public function __construct($caption = null)
21+
public function __construct(string|Stringable $caption = null)
2422
{
2523
parent::__construct($caption);
2624
$this->control->type = 'button';
@@ -49,9 +47,8 @@ public function getLabel($caption = null): Html|string|null
4947

5048
/**
5149
* Generates control's HTML element.
52-
* @param string|object $caption
5350
*/
54-
public function getControl($caption = null): Html
51+
public function getControl(string|Stringable $caption = null): Html
5552
{
5653
$this->setOption('rendered', true);
5754
$el = clone $this->control;

src/Forms/Controls/Checkbox.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\Html;
14+
use Stringable;
1415

1516

1617
/**
@@ -22,10 +23,7 @@ class Checkbox extends BaseControl
2223
private Html $container;
2324

2425

25-
/**
26-
* @param string|object $label
27-
*/
28-
public function __construct($label = null)
26+
public function __construct(string|Stringable $label = null)
2927
{
3028
parent::__construct($label);
3129
$this->control->type = 'checkbox';

src/Forms/Controls/CheckboxList.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\Html;
14+
use Stringable;
1415

1516

1617
/**
@@ -32,10 +33,7 @@ class CheckboxList extends MultiChoiceControl
3233
protected Html $itemLabel;
3334

3435

35-
/**
36-
* @param string|object $label
37-
*/
38-
public function __construct($label = null, array $items = null)
36+
public function __construct(string|Stringable $label = null, array $items = null)
3937
{
4038
parent::__construct($label, $items);
4139
$this->control->type = 'checkbox';

src/Forms/Controls/CsrfProtection.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Application\UI\Presenter;
14+
use Stringable;
1415

1516

1617
/**
@@ -23,10 +24,7 @@ class CsrfProtection extends HiddenField
2324
public ?Nette\Http\Session $session = null;
2425

2526

26-
/**
27-
* @param string|object $errorMessage
28-
*/
29-
public function __construct($errorMessage)
27+
public function __construct(string|Stringable $errorMessage = null)
3028
{
3129
parent::__construct();
3230
$this->setOmitted()

0 commit comments

Comments
 (0)