Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime Period #9

Merged
merged 8 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php-psr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4' , '8.0' , '8.1' ]
php: [ '8.1' ]
steps:
- uses: actions/checkout@v2
name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1']
php: ['8.1']
steps:
- uses: actions/checkout@v2
name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "GPL-3.0-or-later",
"require": {
"php": "7.4.* | 8.0.* | 8.1.* ",
"php": "8.1.* ",
"nette/utils": "^v3.2.0",
"nette/di": "^3.0.0",
"nette/application": "^3.1.0",
Expand Down
61 changes: 61 additions & 0 deletions src/DateTime/Period.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\DateTime;

class Period
{
public function __construct(
public readonly \DateTimeImmutable $begin,
public readonly \DateTimeImmutable $end,
) {
if ($this->begin > $this->end) {
throw new \LogicException();
}
}

public function isBefore(?\DateTimeInterface $dateTime = null): bool
{
return $this->begin > ($dateTime ?? new \DateTimeImmutable());
}

public function isAfter(?\DateTimeInterface $dateTime = null): bool
{
return $this->end < ($dateTime ?? new \DateTimeImmutable());
}

public function isOnGoing(?\DateTimeInterface $dateTime = null): bool
{
return $this->begin <= ($dateTime ?? new \DateTimeImmutable())
&& $this->end >= ($dateTime ?? new \DateTimeImmutable());
}

public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool
{
return match ($period) {
Phase::before => $this->isBefore($dateTime),
Phase::after => $this->isAfter($dateTime),
Phase::onGoing => $this->isOnGoing($dateTime)
};
}

public function getPhase(?\DateTimeInterface $dateTime = null): Phase
{
if ($this->isBefore($dateTime)) {
return Phase::before;
}
if ($this->isAfter($dateTime)) {
return Phase::after;
}
if ($this->isOnGoing($dateTime)) {
return Phase::onGoing;
}
throw new \LogicException();
}

public function duration(): \DateInterval
{
return $this->end->diff($this->begin);
}
}
12 changes: 12 additions & 0 deletions src/DateTime/Phase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\DateTime;

enum Phase
{
case before;
case after;
case onGoing;
}
2 changes: 1 addition & 1 deletion src/Localization/GettextTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getSupportedLanguages(): array
* @phpstan-param array<TLang,TValue>|LangMap<TLang,TValue> $map
* @phpstan-return TValue
*/
public function getVariant($map)
public function getVariant($map): mixed
{
if ($map instanceof LangMap) {
return $map->get($this->lang);
Expand Down
4 changes: 2 additions & 2 deletions src/Localization/LangMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(array $variants)
* @phpstan-param TLang $lang
* @phpstan-return TValue
*/
public function __get(string $lang)
public function __get(string $lang): mixed
{
return $this->get($lang);
}
Expand All @@ -36,7 +36,7 @@ public function __get(string $lang)
* @phpstan-param TLang $lang
* @phpstan-return TValue
*/
public function get(string $lang)
public function get(string $lang): mixed
{
return $this->variants[$lang] ?? null;
}
Expand Down
25 changes: 5 additions & 20 deletions src/Logging/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,14 @@

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 MessageLevel $level
) {
}

/**
Expand All @@ -36,7 +21,7 @@ public function __toArray(): array
{
return [
'text' => ($this->text instanceof Html) ? $this->text->toHtml() : $this->text,
'level' => $this->level,
'level' => $this->level->value,
];
}
}
14 changes: 14 additions & 0 deletions src/Logging/MessageLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\Logging;

enum MessageLevel: string
{
case Error = 'danger';
case Warning = 'warning';
case Success = 'success';
case Info = 'info';
case Primary = 'primary';
}
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 'Kč';
}
return '';
return match ($this) {
self::EUR => '€',
self::CZK => 'Kč',
};
}

public function format(float $amount): string
Expand Down
2 changes: 1 addition & 1 deletion src/Price/MultiCurrencyPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function add(self $multiPrice): void

public function __get(string $name): ?Price
{
return $this->getPrice(Currency::from($name));
return $this->getPrice(Currency::from(strtoupper($name)));
}

public function __set(string $name, Price $value): void
Expand Down
19 changes: 6 additions & 13 deletions src/Price/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@

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;
}

/**
* @throws \LogicException
*/
public function add(Price $price): void
{
if ($this->currency->value !== $price->getCurrency()->value) {
if ($this->currency !== $price->getCurrency()) {
throw new \LogicException('Currencies are not a same');
}
$this->amount += $price->getAmount();
Expand All @@ -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;
}
}
16 changes: 6 additions & 10 deletions src/UI/PageTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@

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
Loading
Loading