Skip to content
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
8 changes: 8 additions & 0 deletions resources/locales/it/cake_essentials.po
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ msgstr ""
"Language: it_IT\n"
"X-Generator: Lokalize 22.12.3\n"

msgctxt "from day to day"
msgid "From {0} to {1}"
msgstr "Dal {0} al {1}"

msgctxt "from hour to hour"
msgid "From {0} to {1}"
msgstr "Dalle {0} alle {1}"

msgid "It cannot contain the reserved word `{0}`"
msgstr "Non può contenere la parola riservata `{0}`"

Expand Down
55 changes: 55 additions & 0 deletions src/View/Helper/BeautifierHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace Cake\Essentials\View\Helper;

use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Cake\View\Helper;
use Stringable;
use function Cake\I18n\__dx as __dx;

/**
* A helper that provides methods to "beautify" the output of some specific entities and values.
Expand Down Expand Up @@ -35,6 +38,58 @@ class BeautifierHelper extends Helper
],
];

/**
* Generates a string representation of a date range in the format "From {Start} to {End}".
*
* @param \Cake\I18n\Date|\Cake\I18n\DateTime $from The starting date or datetime
* @param \Cake\I18n\Date|\Cake\I18n\DateTime $to The ending date or datetime
* @param array<string, mixed> $options Additional formatting options. Supports 'format' for date formatting
* @return string A formatted string representing the date range
*/
public function fromDayToDay(Date|DateTime $from, Date|DateTime $to, array $options = []): string
{
/** @var string|int|null $format */
$format = $options['format'] ?? 'dd/MM';
unset($options['format']);

return $this->time(
text: __dx(
'cake/essentials',
'from day to day',
'From {0} to {1}',
$from->i18nFormat($format),
$to->i18nFormat($format),
),
options: $options,
);
}

/**
* Generates a formatted string representing the time range between two DateTime instances.
*
* @param \Cake\I18n\DateTime $from The starting time of the range
* @param \Cake\I18n\DateTime $to The ending time of the range
* @param array<string, mixed> $options Optional settings for output formatting. The `format` key defines the time format (default is 'HH:mm')
* @return string A formatted string representing the time range
*/
public function fromHourToHour(DateTime $from, DateTime $to, array $options = []): string
{
/** @var string|int|null $format */
$format = $options['format'] ?? 'HH:mm';
unset($options['format']);

return $this->time(
text: __dx(
'cake/essentials',
'from hour to hour',
'From {0} to {1}',
$from->i18nFormat($format),
$to->i18nFormat($format),
),
options: $options,
);
}

/**
* Returns a nice representation of an IP address.
*
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/FlashHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function render(string $key = 'flash', array $options = []): ?string
}

if (!is_array($stack)) {
throw new UnexpectedValueException(sprintf('Value for flash setting key "%s" must be an array', $key));
throw new UnexpectedValueException("Value for flash setting key \"$key\" must be an array");
}

if (isset($stack['element'])) {
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/View/Helper/BeautifierHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Cake\Essentials\View\Helper\BeautifierHelper;
use Cake\Essentials\View\Helper\HtmlHelper;
use Cake\Essentials\View\View;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -31,6 +33,37 @@ protected function setUp(): void
$this->Beautifier = new BeautifierHelper(new View());
}

/**
* @link \Cake\Essentials\View\Helper\BeautifierHelper::fromDayToDay()
*/
#[Test]
#[TestWith(['From 02/03 to 03/03', new DateTime('2025-03-02 18:36:00'), new DateTime('2025-03-03 18:36:00')])]
#[TestWith(['From 02/03 to 03/03', new Date('2025-03-02'), new Date('2025-03-03')])]
#[TestWith(['From 02/03/2025 to 03/03/2025', new Date('2025-03-02'), new Date('2025-03-03'), 'dd/MM/yyyy'])]
public function testFromDayToDay(string $expectedText, Date|DateTime $Start, Date|DateTime $End, ?string $format = null): void
{
$expected = '<time><i class="bi bi-clock"></i> ' . $expectedText . '</time>';
$result = $this->Beautifier->fromDayToDay(from: $Start, to: $End, options: compact('format'));
$this->assertSame($expected, $result);
}

/**
* @link \Cake\Essentials\View\Helper\BeautifierHelper::fromHourToHour()
*/
#[Test]
#[TestWith(['From 18:36 to 19:36'])]
#[TestWith(['From 18:36:12 to 19:36:13', 'HH:mm:ss'])]
public function testFromHourToHour(string $expectedText, ?string $format = null): void
{
$expected = '<time><i class="bi bi-clock"></i> ' . $expectedText . '</time>';
$result = $this->Beautifier->fromHourToHour(
from: new DateTime('2025-03-02 18:36:12'),
to: new DateTime('2025-03-02 19:36:13'),
options: compact('format'),
);
$this->assertSame($expected, $result);
}

/**
* @param string $expected
* @param array<string, mixed> $options
Expand Down
Loading