upgrade to laravel 8.x

This commit is contained in:
2021-05-30 08:20:41 +00:00
parent 41b78a0599
commit 3ad6ed8bfa
2333 changed files with 113904 additions and 59058 deletions

View File

@@ -0,0 +1 @@
github: [lorisleiva]

View File

@@ -1,4 +1,5 @@
vendor
composer.lock
.phpunit.result.cache
build
build
.idea

View File

@@ -0,0 +1,126 @@
<?php
namespace Lorisleiva\CronTranslator;
class CronExpression
{
/** @var string */
public $raw;
/** @var MinutesField */
public $minute;
/** @var HoursField */
public $hour;
/** @var DaysOfMonthField */
public $day;
/** @var MonthsField */
public $month;
/** @var DaysOfWeekField */
public $weekday;
/** @var string */
public $locale;
/** @var bool */
public $timeFormat24hours;
/** @var array */
public $translations;
public function __construct(string $cron, string $locale = 'en', bool $timeFormat24hours = false)
{
$this->raw = $cron;
$fields = explode(' ', $cron);
$this->minute = new MinutesField($this, $fields[0]);
$this->hour = new HoursField($this, $fields[1]);
$this->day = new DaysOfMonthField($this, $fields[2]);
$this->month = new MonthsField($this, $fields[3]);
$this->weekday = new DaysOfWeekField($this, $fields[4]);
$this->locale = $locale;
$this->timeFormat24hours = $timeFormat24hours;
$this->ensureLocaleExists();
$this->loadTranslations();
}
public function getFields()
{
return [
$this->minute,
$this->hour,
$this->day,
$this->month,
$this->weekday,
];
}
public function langCountable(string $type, int $number)
{
$array = $this->translations[$type];
$value = isset($array[$number])
? $array[$number]
: ($array['default'] ?: '');
return str_replace(':number', $number, $value);
}
public function lang(string $key, array $replacements = [])
{
$translation = $this->getArrayDot($this->translations['fields'], $key);
foreach ($replacements as $key => $value) {
$translation = str_replace(':' . $key, $value, $translation);
}
return $translation;
}
protected function ensureLocaleExists(string $fallbackLocale = 'en')
{
if (! is_dir($this->getTranslationDirectory())) {
$this->locale = $fallbackLocale;
}
}
protected function loadTranslations()
{
$this->translations = [
'days' => $this->loadTranslationFile('days'),
'fields' => $this->loadTranslationFile('fields'),
'months' => $this->loadTranslationFile('months'),
'ordinals' => $this->loadTranslationFile('ordinals'),
'times' => $this->loadTranslationFile('times'),
];
}
protected function loadTranslationFile(string $file)
{
$filename = sprintf('%s/%s.php', $this->getTranslationDirectory(), $file);
if (! is_file($filename)) {
throw new TranslationFileMissingException($this->locale, $file);
}
return include $filename;
}
protected function getTranslationDirectory()
{
return __DIR__ . '/lang/' . $this->locale;
}
protected function getArrayDot(array $array, string $key)
{
$keys = explode('.', $key);
foreach ($keys as $key) {
$array = $array[$key];
}
return $array;
}
}

View File

@@ -2,6 +2,8 @@
namespace Lorisleiva\CronTranslator;
use Throwable;
class CronTranslator
{
private static $extendedMap = [
@@ -13,41 +15,27 @@ class CronTranslator
'@hourly' => '0 * * * *'
];
public static function translate($cron)
public static function translate(string $cron, string $locale = 'en', bool $timeFormat24hours = false)
{
if (isset(self::$extendedMap[$cron])) {
$cron = self::$extendedMap[$cron];
}
try {
$fields = static::parseFields($cron);
$orderedFields = static::orderFields($fields);
$fieldsAsObject = static::getFieldsAsObject($fields);
$expression = new CronExpression($cron, $locale, $timeFormat24hours);
$orderedFields = static::orderFields($expression->getFields());
$translations = array_map(function ($field) use ($fieldsAsObject) {
return $field->translate($fieldsAsObject);
$translations = array_map(function (Field $field) {
return $field->translate();
}, $orderedFields);
return ucfirst(implode(' ', array_filter($translations)));
} catch (\Throwable $th) {
} catch (Throwable $th) {
throw new CronParsingException($cron);
}
}
protected static function parseFields($cron)
{
$fields = explode(' ', $cron);
return [
new MinutesField($fields[0]),
new HoursField($fields[1]),
new DaysOfMonthField($fields[2]),
new MonthsField($fields[3]),
new DaysOfWeekField($fields[4]),
];
}
protected static function orderFields($fields)
protected static function orderFields(array $fields)
{
// Group fields by CRON types.
$onces = static::filterType($fields, 'Once');
@@ -62,6 +50,7 @@ class CronTranslator
// Mark fields that will not be displayed as dropped.
// This allows other fields to check whether some
// information is missing and adapt their translation.
/** @var Field $field */
foreach (array_slice($everys, $numberOfEverysKept) as $field) {
$field->dropped = true;
}
@@ -78,21 +67,10 @@ class CronTranslator
);
}
protected static function filterType($fields, ...$types)
protected static function filterType(array $fields, ...$types)
{
return array_filter($fields, function ($field) use ($types) {
return array_filter($fields, function (Field $field) use ($types) {
return $field->hasType(...$types);
});
}
protected static function getFieldsAsObject($fields)
{
return (object) [
'minute' => $fields[0],
'hour' => $fields[1],
'day' => $fields[2],
'month' => $fields[3],
'weekday' => $fields[4],
];
}
}

View File

@@ -8,12 +8,19 @@ class CronType
'Every', 'Increment', 'Multiple', 'Once',
];
/** @var string */
public $type;
/** @var ?int */
public $value;
/** @var ?int */
public $count;
/** @var ?int */
public $increment;
private function __construct($type, $value = null, $count = null, $increment = null)
private function __construct(string $type, ?int $value = null, ?int $count = null, ?int $increment = null)
{
$this->type = $type;
$this->value = $value;
@@ -26,22 +33,22 @@ class CronType
return new static('Every');
}
public static function increment($increment, $count = 1)
public static function increment(int $increment, int $count = 1)
{
return new static('Increment', null, $count, $increment);
}
public static function multiple($count)
public static function multiple(int $count)
{
return new static('Multiple', null, $count);
}
public static function once($value)
public static function once(int $value)
{
return new static('Once', $value);
}
public static function parse($expression)
public static function parse(string $expression)
{
// Parse "*".
if ($expression === '*') {
@@ -61,8 +68,8 @@ class CronType
// Parse ranges of selected values like "1-5".
if (preg_match("/^([0-9]+)\-([0-9]+)$/", $expression, $matches)) {
$count = $matches[2] - $matches[1] + 1;
return $count > 1
? static::multiple($count)
return $count > 1
? static::multiple($count)
: static::once((int) $matches[1]);
}

View File

@@ -6,60 +6,63 @@ class DaysOfMonthField extends Field
{
public $position = 2;
public function translateEvery($fields)
public function translateEvery()
{
if ($fields->weekday->hasType('Once')) {
return "every {$fields->weekday->format()}";
if ($this->expression->weekday->hasType('Once')) {
return $this->lang('days_of_week.every', [
'weekday' => $this->expression->weekday->format(),
]);
}
return 'every day';
return $this->lang('days_of_month.every');
}
public function translateIncrement()
{
if ($this->count > 1) {
return "{$this->count} days out of {$this->increment}";
if ($this->getCount() > 1) {
return $this->lang('days_of_month.multiple_per_increment', [
'count' => $this->getCount(),
'increment' => $this->getIncrement(),
]);
}
return "every {$this->increment} days";
return $this->lang('days_of_month.increment', [
'increment' => $this->getIncrement(),
]);
}
public function translateMultiple()
{
return "{$this->count} days a month";
return $this->lang('days_of_month.multiple_per_month', [
'count' => $this->getCount(),
]);
}
public function translateOnce($fields)
public function translateOnce()
{
if ($fields->month->hasType('Once')) {
$month = $this->expression->month;
if ($month->hasType('Once')) {
return; // MonthsField adapts to "On January the 1st".
}
if ($fields->month->hasType('Every') && ! $fields->month->dropped) {
if ($month->hasType('Every') && ! $month->dropped) {
return; // MonthsField adapts to "The 1st of every month".
}
if ($fields->month->hasType('Every') && $fields->month->dropped) {
return 'on the ' . $this->format() . ' of every month';
if ($month->hasType('Every') && $month->dropped) {
return $this->lang('days_of_month.every_on_day', [
'day' => $this->format()
]);
}
return 'on the ' . $this->format();
return $this->lang('days_of_month.once_on_day', [
'day' => $this->format()
]);
}
public function format()
{
if (in_array($this->value, [1, 21, 31])) {
return $this->value . 'st';
}
if (in_array($this->value, [2, 22])) {
return $this->value . 'nd';
}
if (in_array($this->value, [3, 23])) {
return $this->value . 'rd';
}
return $this->value . 'th';
return $this->langCountable('ordinals', $this->getValue());
}
}

View File

@@ -8,47 +8,49 @@ class DaysOfWeekField extends Field
public function translateEvery()
{
return 'every year';
return $this->lang('years.every');
}
public function translateIncrement()
{
if ($this->count > 1) {
return "{$this->count} days of the week out of {$this->increment}";
if ($this->getCount() > 1) {
return $this->lang('days_of_week.multiple_per_increment', [
'count' => $this->getCount(),
'increment' => $this->getIncrement(),
]);
}
return "every {$this->increment} days of the week";
return $this->lang('days_of_week.increment', [
'increment' => $this->getIncrement(),
]);
}
public function translateMultiple()
{
return "{$this->count} days a week";
return $this->lang('days_of_week.multiple_days_a_week', [
'count' => $this->getCount(),
]);
}
public function translateOnce($fields)
public function translateOnce()
{
if ($fields->day->hasType('Every') && ! $fields->day->dropped) {
if ($this->expression->day->hasType('Every') && ! $this->expression->day->dropped) {
return; // DaysOfMonthField adapts to "Every Sunday".
}
return "on {$this->format()}s";
return $this->lang('days_of_week.once_on_day', [
'day' => $this->format()
]);
}
public function format()
{
if ($this->value < 0 || $this->value > 7) {
throw new \Exception();
$weekday = $this->getValue() === 0 ? 7 : $this->getValue();
if ($weekday < 1 || $weekday > 7) {
throw new CronParsingException($this->expression->raw);
}
return [
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday',
][$this->value];
return $this->langCountable('days', $weekday);
}
}

View File

@@ -4,44 +4,69 @@ namespace Lorisleiva\CronTranslator;
abstract class Field
{
/** @var CronExpression */
public $expression;
/** @var string */
public $rawField;
/** @var CronType */
public $type;
public $value;
public $count;
public $increment;
/** @var bool */
public $dropped = false;
/** @var int */
public $position;
public function __construct($expression)
public function __construct(CronExpression $expression, string $rawField)
{
$this->expression = $expression;
$cronType = CronType::parse($expression);
$this->type = $cronType->type;
$this->value = $cronType->value;
$this->count = $cronType->count;
$this->increment = $cronType->increment;
$this->rawField = $rawField;
$this->type = CronType::parse($rawField);
}
public function translate($fields)
public function translate()
{
foreach (CronType::TYPES as $type) {
if ($this->hasType($type) && method_exists($this, "translate{$type}")) {
return $this->{"translate{$type}"}($fields);
}
$method = 'translate' . $this->type->type;
if (method_exists($this, $method)) {
return $this->{$method}();
}
}
public function hasType()
{
return in_array($this->type, func_get_args());
return $this->type->hasType(...func_get_args());
}
public function times($count)
public function getValue()
{
switch ($count) {
case 1: return 'once';
case 2: return 'twice';
default: return "{$count} times";
}
return $this->type->value;
}
public function getCount()
{
return $this->type->count;
}
public function getIncrement()
{
return $this->type->increment;
}
public function getTimes()
{
return $this->langCountable('times', $this->getCount());
}
protected function langCountable(string $key, int $value)
{
return $this->expression->langCountable($key, $value);
}
protected function lang(string $key, array $replacements = [])
{
return $this->expression->lang($key, $replacements);
}
}

View File

@@ -6,56 +6,80 @@ class HoursField extends Field
{
public $position = 1;
public function translateEvery($fields)
public function translateEvery()
{
if ($fields->minute->hasType('Once')) {
return 'once an hour';
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.once_an_hour');
}
return 'every hour';
return $this->lang('hours.every');
}
public function translateIncrement($fields)
public function translateIncrement()
{
if ($fields->minute->hasType('Once')) {
return $this->times($this->count) . " every {$this->increment} hours";
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.times_per_increment', [
'times' => $this->getTimes(),
'increment' => $this->getIncrement(),
]);
}
if ($this->count > 1) {
return "{$this->count} hours out of {$this->increment}";
if ($this->getCount() > 1) {
return $this->lang('hours.multiple_per_increment', [
'count' => $this->getCount(),
'increment' => $this->getIncrement(),
]);
}
if ($fields->minute->hasType('Every')) {
return "of every {$this->increment} hours";
if ($this->expression->minute->hasType('Every')) {
return $this->lang('hours.increment_chained', [
'increment' => $this->getIncrement(),
]);
}
return "every {$this->increment} hours";
}
public function translateMultiple($fields)
{
if ($fields->minute->hasType('Once')) {
return $this->times($this->count) . " a day";
}
return "{$this->count} hours a day";
}
public function translateOnce($fields)
{
return 'at ' . $this->format(
$fields->minute->hasType('Once') ? $fields->minute : null
);
return $this->lang('hours.increment', [
'increment' => $this->getIncrement(),
]);
}
public function format($minute = null)
public function translateMultiple()
{
$amOrPm = $this->value < 12 ? 'am' : 'pm';
$hour = $this->value === 0 ? 12 : $this->value;
$hour = $hour > 12 ? $hour - 12 : $hour;
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.times_per_day', [
'times' => $this->getTimes(),
]);
}
return $minute
? "{$hour}:{$minute->format()}{$amOrPm}"
return $this->lang('hours.multiple_per_day', [
'count' => $this->getCount(),
]);
}
public function translateOnce()
{
$minute = $this->expression->minute->hasType('Once')
? $this->expression->minute
: null;
return $this->lang('hours.once_at_time', [
'time' => $this->format($minute)
]);
}
public function format(?MinutesField $minute = null)
{
if ($this->expression->timeFormat24hours) {
$hour = $this->getValue();
return $minute ? "{$hour}:{$minute->format()}" : "{$hour}:00";
}
$amOrPm = $this->getValue() < 12 ? 'am' : 'pm';
$hour = $this->getValue() % 12;
$hour = $hour === 0 ? 12 : $hour;
return $minute
? "{$hour}:{$minute->format()}{$amOrPm}"
: "{$hour}{$amOrPm}";
}
}

View File

@@ -8,25 +8,32 @@ class MinutesField extends Field
public function translateEvery()
{
return 'every minute';
return $this->lang('minutes.every');
}
public function translateIncrement()
{
if ($this->count > 1) {
return $this->times($this->count) . " every {$this->increment} minutes";
if ($this->getCount() > 1) {
return $this->lang('minutes.times_per_increment', [
'times' => $this->getTimes(),
'increment' => $this->getIncrement(),
]);
}
return "every {$this->increment} minutes";
return $this->lang('minutes.increment', [
'increment' => $this->getIncrement(),
]);
}
public function translateMultiple()
{
return $this->times($this->count) . " an hour";
return $this->lang('minutes.multiple', [
'times' => $this->getTimes(),
]);
}
public function format()
{
return ($this->value < 10 ? '0' : '') . $this->value;
{
return ($this->getValue() < 10 ? '0' : '') . $this->getValue();
}
}

View File

@@ -6,57 +6,58 @@ class MonthsField extends Field
{
public $position = 3;
public function translateEvery($fields)
public function translateEvery()
{
if ($fields->day->hasType('Once')) {
return 'the ' . $fields->day->format() . ' of every month';
if ($this->expression->day->hasType('Once')) {
return $this->lang('months.every_on_day', [
'day' => $this->expression->day->format(),
]);
}
return 'every month';
return $this->lang('months.every');
}
public function translateIncrement()
{
if ($this->count > 1) {
return "{$this->count} months out of {$this->increment}";
if ($this->getCount() > 1) {
return $this->lang('months.multiple_per_increment', [
'count' => $this->getCount(),
'increment' => $this->getCount(),
]);
}
return "every {$this->increment} months";
return $this->lang('months.increment', [
'increment' => $this->getIncrement(),
]);
}
public function translateMultiple()
{
return "{$this->count} months a year";
return $this->lang('months.multiple_per_year', [
'count' => $this->getCount(),
]);
}
public function translateOnce($fields)
public function translateOnce()
{
if ($fields->day->hasType('Once')) {
return "on {$this->format()} the {$fields->day->format()}";
if ($this->expression->day->hasType('Once')) {
return $this->lang('months.once_on_day', [
'month' => $this->format(),
'day' => $this->expression->day->format(),
]);
}
return "on {$this->format()}";
return $this->lang('months.once_on_month', [
'month' => $this->format()
]);
}
public function format()
{
if ($this->value < 1 || $this->value > 12) {
throw new \Exception();
if ($this->getValue() < 1 || $this->getValue() > 12) {
throw new CronParsingException($this->expression->raw);
}
return [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
][$this->value];
return $this->langCountable('months', $this->getValue());
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Lorisleiva\CronTranslator;
use Exception;
class TranslationFileMissingException extends Exception
{
public function __construct($locale, $file)
{
parent::__construct("Failed to load the translation file [{$file}] from the locale [{$locale}].");
}
}

View File

@@ -0,0 +1,11 @@
<?php
return [
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday',
];

View File

@@ -0,0 +1,48 @@
<?php
return [
'minutes' => [
'every' => 'every minute',
'increment' => 'every :increment minutes',
'times_per_increment' => ':times every :increment minutes',
'multiple' => ':times an hour',
],
'hours' => [
'every' => 'every hour',
'once_an_hour' => 'once an hour',
'increment' => 'every :increment hours',
'multiple_per_increment' => ':count hours out of :increment',
'times_per_increment' => ':times every :increment hours',
'increment_chained' => 'of every :increment hours',
'multiple_per_day' => ':count hours a day',
'times_per_day' => ':times a day',
'once_at_time' => 'at :time',
],
'days_of_month' => [
'every' => 'every day',
'increment' => 'every :increment days',
'multiple_per_increment' => ':count days out of :increment',
'multiple_per_month' => ':count days a month',
'once_on_day' => 'on the :day',
'every_on_day' => 'on the :day of every month',
],
'months' => [
'every' => 'every month',
'every_on_day' => 'the :day of every month',
'increment' => 'every :increment months',
'multiple_per_increment' => ':count months out of :increment',
'multiple_per_year' => ':count months a year',
'once_on_month' => 'on :month',
'once_on_day' => 'on :month the :day',
],
'days_of_week' => [
'every' => 'every :weekday',
'increment' => 'every :increment days of the week',
'multiple_per_increment' => ':count days of the week out of :increment',
'multiple_days_a_week' => ':count days a week',
'once_on_day' => 'on :days',
],
'years' => [
'every' => 'every year',
],
];

View File

@@ -0,0 +1,16 @@
<?php
return [
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December',
];

View File

@@ -0,0 +1,12 @@
<?php
return [
'default' => ':numberth',
1 => '1st',
2 => '2nd',
3 => '3rd',
21 => '21st',
22 => '22nd',
23 => '23rd',
31 => '31st',
];

View File

@@ -0,0 +1,7 @@
<?php
return [
'default' => ':number times',
1 => 'once',
2 => 'twice',
];

View File

@@ -0,0 +1,11 @@
<?php
return [
1 => 'lundi',
2 => 'mardi',
3 => 'mercredi',
4 => 'jeudi',
5 => 'vendredi',
6 => 'samedi',
7 => 'dimanche',
];

View File

@@ -0,0 +1,48 @@
<?php
return [
'minutes' => [
'every' => 'chaque minute',
'increment' => 'toutes les :increment minutes',
'times_per_increment' => ':times toutes les :increment minutes',
'multiple' => ':times par heure',
],
'hours' => [
'every' => 'chaque heure',
'once_an_hour' => 'une fois par heure',
'increment' => 'toutes les :increment heures',
'multiple_per_increment' => ':count heures au-delà de :increment',
'times_per_increment' => ':times toutes les :increment heures',
'increment_chained' => 'toutes les :increment heures',
'multiple_per_day' => ':count heures par jour',
'times_per_day' => ':times fois par jour',
'once_at_time' => 'à :time',
],
'days_of_month' => [
'every' => 'tous les jours',
'increment' => 'tous les :increment jours',
'multiple_per_increment' => ':count jours sur :increment',
'multiple_per_month' => ':count jours par mois',
'once_on_day' => 'le :day',
'every_on_day' => 'le :day de chaque mois',
],
'months' => [
'every' => 'chaque mois',
'every_on_day' => 'le :day de chaque mois',
'increment' => 'tous les :increment mois',
'multiple_per_increment' => ':count mois sur :increment',
'multiple_per_year' => ':count mois par an',
'once_on_month' => 'en :month',
'once_on_day' => 'le :day :month',
],
'days_of_week' => [
'every' => 'chaque :weekday',
'increment' => 'tous les :increment jours de la semaine',
'multiple_per_increment' => ':count jours de la semaine sur :increment',
'multiple_days_a_week' => ':count jours par semaine',
'once_on_day' => 'les :days',
],
'years' => [
'every' => 'chaque année',
],
];

View File

@@ -0,0 +1,16 @@
<?php
return [
1 => 'janvier',
2 => 'février',
3 => 'mars',
4 => 'avril',
5 => 'mai',
6 => 'juin',
7 => 'juillet',
8 => 'août',
9 => 'septembre',
10 => 'octobre',
11 => 'novembre',
12 => 'décembre',
];

View File

@@ -0,0 +1,6 @@
<?php
return [
'default' => ':number',
1 => '1er',
];

View File

@@ -0,0 +1,7 @@
<?php
return [
'default' => ':number fois',
1 => 'une fois',
2 => 'deux fois',
];

View File

@@ -0,0 +1,11 @@
<?php
return [
1 => 'pirmdien',
2 => 'otrdien',
3 => 'trešdien',
4 => 'ceturtdien',
5 => 'piektdien',
6 => 'sestdien',
7 => 'svētdien',
];

View File

@@ -0,0 +1,48 @@
<?php
return [
'minutes' => [
'every' => 'katru minūti',
'increment' => 'ik pēc :increment minūtēm',
'times_per_increment' => ':times ik pēc :increment minūtēm',
'multiple' => ':times stundā',
],
'hours' => [
'every' => 'katru stundu',
'once_an_hour' => 'vienreiz stundā',
'increment' => 'katras :increment stundas',
'multiple_per_increment' => ':count no :increment stundām',
'times_per_increment' => ':times katras :increment stundas',
'increment_chained' => 'ik pēc :increment stundām',
'multiple_per_day' => ':count stundas dienā',
'times_per_day' => ':times dienā',
'once_at_time' => 'plkst. :time',
],
'days_of_month' => [
'every' => 'katru dienu',
'increment' => 'ik pēc :increment dienām',
'multiple_per_increment' => ':count dienas no :increment',
'multiple_per_month' => ':count dienas mēnesī',
'once_on_day' => ':day. datumā',
'every_on_day' => 'katra mēneša :day. datumā',
],
'months' => [
'every' => 'katru mēnesi',
'every_on_day' => 'katra mēneša :day. datumā',
'increment' => 'katrus :increment mēnešus',
'multiple_per_increment' => ':count mēnešus no :increment',
'multiple_per_year' => ':count mēnešus gadā',
'once_on_month' => ':month',
'once_on_day' => ':day. :month',
],
'days_of_week' => [
'every' => 'katru :weekdayu',
'increment' => 'ik pēc :increment nedēļas dienām',
'multiple_per_increment' => ':count nedēļas dienas no :increment',
'multiple_days_a_week' => ':count dienas nedēļā',
'once_on_day' => ':dayās',
],
'years' => [
'every' => 'katru gadu',
],
];

View File

@@ -0,0 +1,16 @@
<?php
return [
1 => 'janvārī',
2 => 'februārī',
3 => 'martā',
4 => 'aprīlī',
5 => 'maijā',
6 => 'jūnijā',
7 => 'jūlijā',
8 => 'augustā',
9 => 'septembrī',
10 => 'oktobrī',
11 => 'novembrī',
12 => 'decembrī',
];

View File

@@ -0,0 +1,5 @@
<?php
return [
'default' => ':number',
];

View File

@@ -0,0 +1,7 @@
<?php
return [
'default' => ':number reizes',
1 => 'vienreiz',
2 => 'divreiz',
];

View File

@@ -125,6 +125,36 @@ class CronTranslatorTest extends TestCase
$this->assertCronThrowsParsingError('* * * * 8');
}
/** @test */
public function it_can_translate_in_different_languages()
{
$this->assertCronTranslateTo('Chaque minute', '* * * * *', 'fr');
$this->assertCronTranslateTo('Chaque minute les dimanches', '* * * * 0', 'fr');
$this->assertCronTranslateTo('Chaque minute toutes les 2 heures', '* */2 * * *', 'fr');
$this->assertCronTranslateTo('Chaque minute toutes les 3 heures le 2 de chaque mois', '* 1/3 2 * *', 'fr');
$this->assertCronTranslateTo('Chaque année le 1er janvier à 1:01am', '1 1 1 1 *', 'fr');
$this->assertCronTranslateTo('Chaque mercredi à 10:00am', '0 10 * * 3', 'fr');
$this->assertCronTranslateTo('Les mardis le 2 février à 2:02am', '2 2 2 2 2', 'fr');
}
/** @test */
public function it_can_format_the_time_in_12_and_24_hours()
{
$this->assertCronTranslateTo('Every day at 10:30pm', '30 22 * * *', 'en', false);
$this->assertCronTranslateTo('Every day at 22:30', '30 22 * * *', 'en', true);
$this->assertCronTranslateTo('Every minute at 6am', '* 6 * * *', 'en', false);
$this->assertCronTranslateTo('Every minute at 6:00', '* 6 * * *', 'en', true);
}
/** @test */
public function it_can_translate_in_different_languages_and_different_time_format()
{
$this->assertCronTranslateTo('Tous les jours à 10:30pm', '30 22 * * *', 'fr', false);
$this->assertCronTranslateTo('Tous les jours à 22:30', '30 22 * * *', 'fr', true);
$this->assertCronTranslateTo('Chaque minute à 6am', '* 6 * * *', 'fr', false);
$this->assertCronTranslateTo('Chaque minute à 6:00', '* 6 * * *', 'fr', true);
}
/**
* @skip
* @doesNotPerformAssertions

View File

@@ -8,9 +8,9 @@ use Lorisleiva\CronTranslator\CronParsingException;
class TestCase extends BaseTestCase
{
public function assertCronTranslateTo($expected, $actual)
public function assertCronTranslateTo($expected, $actual, $locale = 'en', $timeFormat24hours = false)
{
$this->assertEquals($expected, CronTranslator::translate($actual));
$this->assertEquals($expected, CronTranslator::translate($actual, $locale, $timeFormat24hours));
}
public function assertCronThrowsParsingError($cron)
@@ -24,7 +24,7 @@ class TestCase extends BaseTestCase
$this->fail("Expected CronParsingError exception for [$cron]");
}
public function generateCombinationsFromMatrix($matrix)
public function generateCombinationsFromMatrix($matrix, $locale = 'en', $timeFormat24hours = false)
{
function combinations($matrix, $acc = []) {
if (empty($matrix)) {
@@ -42,7 +42,7 @@ class TestCase extends BaseTestCase
}
foreach (combinations($matrix) as $cron) {
echo "\n" . $cron . "\t=> " . CronTranslator::translate($cron);
echo "\n" . $cron . "\t=> " . CronTranslator::translate($cron, $locale, $timeFormat24hours);
}
}
}