Skip to content

Commit

Permalink
PHP8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro committed Oct 27, 2024
1 parent d1c2035 commit 6937482
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 120 deletions.
17 changes: 4 additions & 13 deletions src/Logging/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@

namespace Fykosak\Utils\Logging;

use Nette\SmartObject;
use Nette\Utils\Html;

class Message
{
use SmartObject;

public const LVL_ERROR = 'danger';
public const LVL_WARNING = 'warning';
public const LVL_SUCCESS = 'success';
public const LVL_INFO = 'info';
public const LVL_PRIMARY = 'primary';

public $text;
public string $level;

/**
* @param string|Html $message
*/
public function __construct($message, string $level)
{
$this->text = $message;
$this->level = $level;
public function __construct(
public readonly string|Html $text,
public readonly string $level
) {
}

/**
Expand Down
61 changes: 7 additions & 54 deletions src/Price/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,20 @@

namespace Fykosak\Utils\Price;

use Nette\NotImplementedException;
use Nette\SmartObject;

/**
* something like enum :)
*/
final class Currency
enum Currency: string
{
use SmartObject;

public const EUR = 'EUR';
public const CZK = 'CZK';

public string $value;

/**
* @throws NotImplementedException
*/
public function __construct(string $currency)
{
$currency = strtoupper($currency);
if (!in_array($currency, [self::EUR, self::CZK])) {
throw new NotImplementedException(sprintf(_('Currency "%s" is not supported'), $currency));
}
$this->value = $currency;
}

/**
* @return self[]
*/
public static function cases(): array
{
return [new self(self::CZK), new self(self::EUR)];
}

public static function tryFrom(string $currency): ?self
{
try {
return new self($currency);
} catch (NotImplementedException $exception) {
return null;
}
}

/**
* @throws NotImplementedException
*/
public static function from(string $currency): self
{
return new self($currency);
}
case EUR = 'EUR';
case CZK = 'CZK';

public function getLabel(): string
{
switch ($this->value) {
case self::EUR:
return '';
case self::CZK:
return '';
}
return '';
return match ($this) {
self::EUR => '',
self::CZK => '',
};
}

public function format(float $amount): string
Expand Down
17 changes: 5 additions & 12 deletions src/Price/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@

namespace Fykosak\Utils\Price;

use Nette\SmartObject;

/**
* @phpstan-type TSerializedPrice array{currency:string,amount:float}
*/
final class Price
{
use SmartObject;

private Currency $currency;
private float $amount;

public function __construct(Currency $currency, ?float $amount = null)
{
public function __construct(
private readonly Currency $currency,
?float $amount = null
) {
$this->amount = $amount ?? 0;
$this->currency = $currency;
}

/**
Expand Down Expand Up @@ -54,7 +47,7 @@ public function __toString(): string
}

/**
* @phpstan-return TSerializedPrice
* @phpstan-return array{currency:string,amount:float}
*/
public function __serialize(): array
{
Expand Down
26 changes: 5 additions & 21 deletions src/UI/Navigation/NavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,19 @@
namespace Fykosak\Utils\UI\Navigation;

use Fykosak\Utils\UI\Title;
use Nette\SmartObject;

class NavItem
{
use SmartObject;

public string $destination;
/** @phpstan-var array<string,scalar> */
public array $linkParams;
public Title $title;
/** @var NavItem[] */
public array $children;
public bool $active;

/**
* @phpstan-param array<string,scalar> $linkParams
* @phpstan-param NavItem[] $children
*/
public function __construct(
Title $title,
string $destination = '#',
array $linkParams = [],
array $children = [],
bool $active = false
public readonly Title $title,
public readonly string $destination = '#',
public readonly array $linkParams = [],
public readonly array $children = [],
public readonly bool $active = false
) {
$this->active = $active;
$this->destination = $destination;
$this->linkParams = $linkParams;
$this->title = $title;
$this->children = $children;
}
}
15 changes: 6 additions & 9 deletions src/UI/PageTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

class PageTitle extends Title
{
/** @var string|Html|null */
public $subTitle;

/**
* @param string|Html $title
* @param string|Html|null $subTitle
*/
public function __construct(?string $id, $title, ?string $icon = null, $subTitle = null)
public function __construct(
?string $id,
string|Html $title,
?string $icon = null,
public string|Html|null $subTitle = null
)
{
parent::__construct($id, $title, $icon);
$this->subTitle = $subTitle;
}

public function toHtml(bool $includeSubTitle = false): Html
Expand Down
17 changes: 6 additions & 11 deletions src/UI/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

class Title
{
/** @var string|Html */
public $title;
public ?string $icon;
public string $id;
public readonly string $id;

/**
* @param string|Html $title
*/
public function __construct(?string $id, $title, ?string $icon = null)
{
$this->title = $title;
$this->icon = $icon;
public function __construct(
?string $id,
public readonly string|Html $title,
public readonly ?string $icon = null
) {
$this->id = $id ?? Random::generate(10, 'a-z');
}

Expand Down

0 comments on commit 6937482

Please sign in to comment.