Skip to content

Commit

Permalink
added PHP 8 typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 2, 2021
1 parent 77d4c49 commit 8c40db5
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 227 deletions.
6 changes: 3 additions & 3 deletions src/Bridges/FormsLatte/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public static function renderBlueprint($form): void
public $inner;


public function getLabel($name = null)
public function getLabel($name = null): Html|string|null
{
return $this->inner->getLabel()
? '{label ' . $this->inner->lookupPath(Form::class) . '/}'
: null;
}


public function getControl()
public function getControl(): Html|string
{
return '{input ' . $this->inner->lookupPath(Form::class) . '}';
}
Expand All @@ -106,7 +106,7 @@ public function isRequired(): bool
}


public function getOption($key, $default = null)
public function getOption($key, mixed $default = null): mixed
{
return $key === 'rendered'
? parent::getOption($key)
Expand Down
36 changes: 13 additions & 23 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ class Container extends Nette\ComponentModel\Container implements \ArrayAccess

/**
* Fill-in with default values.
* @param array|object $data
* @return static
*/
public function setDefaults($data, bool $erase = false)
public function setDefaults(array|object $data, bool $erase = false): static
{
$form = $this->getForm(false);
if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
Expand All @@ -62,20 +60,15 @@ public function setDefaults($data, bool $erase = false)

/**
* Fill-in with values.
* @param array|object $data
* @return static
* @internal
*/
public function setValues($data, bool $erase = false)
public function setValues(array|object $data, bool $erase = false): static
{
if ($data instanceof \Traversable) {
$values = iterator_to_array($data);

} elseif (is_object($data) || is_array($data) || $data === null) {
$values = (array) $data;

} else {
throw new Nette\InvalidArgumentException(sprintf('First parameter must be an array or object, %s given.', gettype($data)));
}

foreach ($this->getComponents() as $name => $control) {
Expand All @@ -88,7 +81,7 @@ public function setValues($data, bool $erase = false)
}

} elseif ($control instanceof self) {
if (array_key_exists($name, $values)) {
if (isset($values[$name])) {
$control->setValues($values[$name], $erase);

} elseif ($erase) {
Expand All @@ -104,9 +97,8 @@ public function setValues($data, bool $erase = false)
* Returns the values submitted by the form.
* @param string|object|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getValues($returnType = null, array $controls = null)
public function getValues(string|object|bool $returnType = null, array $controls = null): object|array
{
$form = $this->getForm(false);
if ($form && ($submitter = $form->isSubmitted())) {
Expand All @@ -126,9 +118,8 @@ public function getValues($returnType = null, array $controls = null)
* Returns the potentially unvalidated values submitted by the form.
* @param string|object|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getUnsafeValues($returnType, array $controls = null)
public function getUnsafeValues(string|object|null $returnType, array $controls = null): object|array
{
if (is_object($returnType)) {
$obj = $returnType;
Expand Down Expand Up @@ -163,8 +154,7 @@ public function getUnsafeValues($returnType, array $controls = null)
}


/** @return static */
public function setMappedType(string $type)
public function setMappedType(string $type): static
{
$this->mappedType = $type;
return $this;
Expand Down Expand Up @@ -229,8 +219,7 @@ public function getErrors(): array
/********************* form building ****************d*g**/


/** @return static */
public function setCurrentGroup(ControlGroup $group = null)
public function setCurrentGroup(ControlGroup $group = null): static
{
$this->currentGroup = $group;
return $this;
Expand All @@ -248,11 +237,13 @@ public function getCurrentGroup(): ?ControlGroup

/**
* Adds the specified component to the IContainer.
* @return static
* @throws Nette\InvalidStateException
*/
public function addComponent(Nette\ComponentModel\IComponent $component, ?string $name, string $insertBefore = null)
{
public function addComponent(
Nette\ComponentModel\IComponent $component,
?string $name,
string $insertBefore = null,
): static {
parent::addComponent($component, $name, $insertBefore);
if ($this->currentGroup !== null) {
$this->currentGroup->add($component);
Expand Down Expand Up @@ -469,9 +460,8 @@ public function addImage(): Controls\ImageButton

/**
* Adds naming container to the form.
* @param string|int $name
*/
public function addContainer($name): self
public function addContainer(string|int $name): self
{
$control = new self;
$control->currentGroup = $this->currentGroup;
Expand Down
7 changes: 2 additions & 5 deletions src/Forms/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ interface Control
{
/**
* Sets control's value.
* @param mixed $value
* @return static
*/
function setValue($value);
function setValue(mixed $value): static;

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

function validate(): void;

Expand Down
10 changes: 3 additions & 7 deletions src/Forms/ControlGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function __construct()
}


/** @return static */
public function add(...$items)
public function add(...$items): static
{
foreach ($items as $item) {
if ($item instanceof Control) {
Expand Down Expand Up @@ -84,10 +83,8 @@ public function getControls(): array
* - 'container' - container as Html object
* - 'description' - textual or Nette\HtmlStringable object description
* - 'embedNext' - describes how render next group
*
* @return static
*/
public function setOption(string $key, $value)
public function setOption(string $key, mixed $value): static
{
if ($value === null) {
unset($this->options[$key]);
Expand All @@ -101,9 +98,8 @@ public function setOption(string $key, $value)

/**
* Returns user-specific option.
* @return mixed
*/
public function getOption(string $key, $default = null)
public function getOption(string $key, $default = null): mixed
{
return $this->options[$key] ?? $default;
}
Expand Down
Loading

0 comments on commit 8c40db5

Please sign in to comment.