Skip to content

Commit

Permalink
feat: Combine all week days in "Every days" message instead of concat…
Browse files Browse the repository at this point in the history
… all days names, PHP return types
  • Loading branch information
ambroisemaupate committed Nov 3, 2022
1 parent 90d260f commit a912b32
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 47 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"php": "^7.0 || ^8.0",
"php": "^7.4 || ^8.0 || ^8.1",
"twig/twig": "1.* || 2.* || 3.*"
},
"autoload": {
Expand All @@ -29,9 +29,9 @@
}
},
"require-dev": {
"satooshi/php-coveralls": "^2.0",
"phpstan/phpstan": "^0.12.43",
"squizlabs/php_codesniffer": "^3.5",
"atoum/atoum": "^3.4"
"satooshi/php-coveralls": "*",
"phpstan/phpstan": "*",
"squizlabs/php_codesniffer": "*",
"atoum/atoum": "*"
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
excludes_analyse:
excludePaths:
- */node_modules/*
- */bower_components/*
- */static/*
Expand Down
10 changes: 5 additions & 5 deletions src/EnglishFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use RZ\OpeningHours\Helpers\Lang;

class EnglishFormatter
class EnglishFormatter implements FormatterInterface
{
/**
* @inheritDoc
Expand All @@ -19,7 +19,7 @@ public function getLocale(): string
* @param string $hour
* @return string
*/
public function formatHour($hour) : string
public function formatHour(string $hour) : string
{
$format = "g:iA";
if ("00" == substr(trim($hour), -2)) {
Expand All @@ -34,19 +34,19 @@ public function formatHour($hour) : string
* @param array $options
* @return string
*/
public function formatDay($day, $options = []) : string
public function formatDay(string $day, array $options = []) : string
{
$day = Lang::t(mb_strtolower($day), [], $this->getLocale());

return ucfirst($day);
return ucfirst($day);
}

/**
* @param string $text
* @param array $options
* @return string
*/
public function formatText($text, $options = []) : string
public function formatText(string $text, array $options = []) : string
{
$text = Lang::t($text, [], $this->getLocale());

Expand Down
6 changes: 3 additions & 3 deletions src/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ interface FormatterInterface
* @param string $hour
* @return string
*/
public function formatHour($hour) : string;
public function formatHour(string $hour) : string;

/**
* @param string $day
* @param array $options
* @return string
*/
public function formatDay($day, $options = []) : string;
public function formatDay(string $day, array $options = []) : string;

/**
* @param string $text
* @param array $options
* @return string
*/
public function formatText($text, $options = []) : string;
public function formatText(string $text, array $options = []) : string;
}
8 changes: 4 additions & 4 deletions src/FrenchFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use RZ\OpeningHours\Helpers\Lang;

class FrenchFormatter
class FrenchFormatter implements FormatterInterface
{
/**
* @inheritDoc
Expand All @@ -19,7 +19,7 @@ public function getLocale(): string
* @param string $hour
* @return string
*/
public function formatHour($hour) : string
public function formatHour(string $hour) : string
{
$format = "H:i";
if ("00" == substr(trim($hour), -2)) {
Expand All @@ -35,7 +35,7 @@ public function formatHour($hour) : string
* @param array $options
* @return string
*/
public function formatDay($day, $options = []) : string
public function formatDay(string $day, array $options = []) : string
{
$day = Lang::t(mb_strtolower($day), [], $this->getLocale());

Expand All @@ -47,7 +47,7 @@ public function formatDay($day, $options = []) : string
* @param array $options
* @return string
*/
public function formatText($text, $options = []) : string
public function formatText(string $text, array $options = []) : string
{
$text = Lang::t($text, [], $this->getLocale());

Expand Down
7 changes: 4 additions & 3 deletions src/Helpers/DataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RZ\OpeningHours\Helpers;

use RZ\OpeningHours\Exceptions\InvalidFormatterByLocale;
use RZ\OpeningHours\FormatterInterface;

trait DataTrait
{
Expand Down Expand Up @@ -39,18 +40,18 @@ protected function getDefaultOptions(): array
* @param array $options
* @return self
*/
protected function setOptions($options)
protected function setOptions(array $options)
{
$this->options = array_merge($this->getDefaultOptions(), $options);
return $this;
}

/**
* @param string $locale
* @return mixed
* @return FormatterInterface
* @throws InvalidFormatterByLocale
*/
protected function getFormatter(string $locale)
protected function getFormatter(string $locale): FormatterInterface
{
if (isset(static::$formatters[$locale])) {
$formatterClass = static::$formatters[$locale];
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Lang
* @param string $string
* @param array $args
* @param string $locale
* @return mixed|string
* @return string
*/
public static function t($string, $args = [], $locale = "fr")
public static function t(string $string, array $args = [], string $locale = "fr"): string
{
global $translation;
$message = null;
Expand Down
53 changes: 29 additions & 24 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use RZ\OpeningHours\Helpers\DataTrait;

/**
* Class OpeningHours
* @package RZ\OpeningHours
*/
class OpeningHours
{
/**
* @var array
* @var array<string, class-string<FormatterInterface>>
*/
public static $formatters = [
public static array $formatters = [
'fr' => FrenchFormatter::class,
'en' => EnglishFormatter::class,
];
Expand All @@ -40,10 +39,9 @@ class OpeningHours
];

/**
* OpeningHours constructor.
* @param array $data
*/
public function __construct($data)
public function __construct(array $data)
{
$this->data = $data;
}
Expand All @@ -59,6 +57,9 @@ protected function buildData()
}

foreach ($this->getData() as $openingHourData) {
if (!is_string($openingHourData)) {
throw new \Exception("Single OpeningHours row data must be a string", 1);
}
$openingHourData = explode(" ", $openingHourData);
$days = $openingHourData[0] ?? "";
$times = $openingHourData[1] ?? "";
Expand Down Expand Up @@ -98,12 +99,14 @@ protected function exploderTime(string $times)

/**
* @param string $times
* @return mixed
* @return array
* @throws \Exception
*/
private function parseTimes(string $times)
private function parseTimes(string $times): array
{
$hours = false;
$hours = [
'hours' => []
];
$times = explode(",", $times);

if (isset($times[0])) {
Expand Down Expand Up @@ -142,8 +145,7 @@ protected function associateDayHours(string $days = null, string $times = null)
if (!Day::isValid($day)) {
throw InvalidDayName::invalidDayName($day);
}
$tempDay[$day] = ['hours' => $timesParser['hours']
];
$tempDay[$day] = ['hours' => $timesParser['hours']];
}
$this->openingDay = array_merge($this->openingDay, $tempDay);
} else {
Expand Down Expand Up @@ -261,7 +263,7 @@ protected function transformDaysAsHtml(array $allDayFound): string
foreach ($allDayFound as $singleDayToCombine => $dataHoursToCombine) {
//search all day for same hour
$labelHour = $labelClose;
if (null !== $dataHoursToCombine && !empty($dataHoursToCombine)) {
if (!empty($dataHoursToCombine)) {
$labelHour = "";
foreach ($dataHoursToCombine['hours'] as $hour) {
$labelHour .= $hour['opensAt'] . " - " . $hour['closesAt'] . ", ";
Expand Down Expand Up @@ -314,11 +316,18 @@ protected function transformDaysAsHtml(array $allDayFound): string
foreach ($combinedDaysReordering as $combinedDays) {
foreach ($combinedDays as $labelHour => $daysCombined) {
$labelDay = "";
/** @var string $day */
foreach ($daysCombined as $day) {
$labelDay .= $this->normalizeDayName($day) . ", ";
if (count($daysCombined) === 7) {
/*
* Do not display all 7 days names, just every_days
*/
$labelDay = $this->getFormatter($this->options['locale'])->formatDay('every_days', $this->options);
} else {
/** @var string $day */
foreach ($daysCombined as $day) {
$labelDay .= $this->normalizeDayName($day) . ", ";
}
$labelDay = substr(trim($labelDay), 0, -1);
}
$labelDay = substr(trim($labelDay), 0, -1);
$stringHtml .=
'<span class="oh-group"><span class="oh-days">' . $labelDay . '</span> <span class="'.$combinedDaysClass[$labelHour].'">' . $labelHour . '</span></span>'.PHP_EOL;
}
Expand All @@ -336,7 +345,7 @@ protected function transformDaysAsHtml(array $allDayFound): string
foreach ($allDayFound as $singleDay => $dataHours) {
$labelHour = $labelClose;
$classHour = 'oh-status';
if (null !== $dataHours && !empty($dataHours)) {
if (!empty($dataHours)) {
$labelHour = "";
foreach ($dataHours['hours'] as $hour) {
$labelHour .= $hour['opensAt'] . " - " . $hour['closesAt'] . ", ";
Expand All @@ -354,10 +363,10 @@ protected function transformDaysAsHtml(array $allDayFound): string

/**
* @param string $day
* @return mixed
* @return string
* @throws \Exception
*/
protected function normalizeDayName($day)
protected function normalizeDayName(string $day): string
{
$formatter = $this->getFormatter($this->options['locale']);
if (!Day::isValid($day)) {
Expand All @@ -371,16 +380,12 @@ protected function normalizeDayName($day)
* @param \DateTime $date
* @return bool
*/
public function isOpenedAt(\DateTime $date)
public function isOpenedAt(\DateTime $date): bool
{
return $this->dataOpeningOneDay($date);
}

/**
* @param \DateTime $date
* @return bool
*/
protected function dataOpeningOneDay(\DateTime $date)
protected function dataOpeningOneDay(\DateTime $date): bool
{
$allDay = $this->getAllDaysAsArray();
$day = $date->format('D');
Expand Down
1 change: 1 addition & 0 deletions src/lang/en_US/message.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
return [
'Hello %name' => 'Hello %name',
'every_days' => 'Every days',
'mo' => 'Monday',
'tu' => 'Tuesday',
'we' => 'wednesday',
Expand Down
1 change: 1 addition & 0 deletions src/lang/fr_FR/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
return [
'Email' => 'Courriel',
'Name' => 'Nom',
'every_days' => 'Tous les jours',
'Hello %name' => 'Bonjour %name',
'mo' => 'Lundi',
'tu' => 'Mardi',
Expand Down
50 changes: 50 additions & 0 deletions tests/units/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,56 @@ public function testGetAllDaysAsHtmlAndCombined()
;
}

/**
* @throws \Exception
*/
public function testGetEveryDaysAsHtmlAndCombined()
{
$this
// creation of a new instance of the class to test OpeningHours
->given($openingHours = new BaseOpeningHours([
// Monday is closed and not mentioned
'Mo,Tu,We,Th,Fr,Sa,Su 12:00-19:00',
]))
->object($openingHours)
->isInstanceOf(BaseOpeningHours::class)
->string($openingHours->getAllDaysAsHtml([
'combinedDays' => true,
'capitalize' => false,
'locale' => 'fr'
]))
->isNotEmpty()
// Monday is closed like friday and saturday, but some days are opened between them
// so we split them even if combinedDays is set to TRUE
->isEqualTo(trim(<<<EOT
<span class="oh-group"><span class="oh-days">tous les jours</span> <span class="oh-hours">12h - 19h</span></span>
EOT
))
// --- capitalize option
->string($openingHours->getAllDaysAsHtml([
'combinedDays' => true,
'capitalize' => true,
'locale' => 'fr'
]))
->isNotEmpty()
->isEqualTo(trim(<<<EOT
<span class="oh-group"><span class="oh-days">Tous les jours</span> <span class="oh-hours">12h - 19h</span></span>
EOT
))
// --- capitalize option
->string($openingHours->getAllDaysAsHtml([
'combinedDays' => true,
'capitalize' => true,
'locale' => 'en'
]))
->isNotEmpty()
->isEqualTo(trim(<<<EOT
<span class="oh-group"><span class="oh-days">Every days</span> <span class="oh-hours">12PM - 7PM</span></span>
EOT
))
;
}

/**
* @throws \Exception
*/
Expand Down

0 comments on commit a912b32

Please sign in to comment.