Skip to content

Commit

Permalink
style: fix code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-frey authored and github-actions[bot] committed Sep 5, 2024
1 parent bbfc48b commit 2930e8e
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 114 deletions.
5 changes: 3 additions & 2 deletions database/factories/SequencePeriodFactory.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Guava\Sequence\Models\SequencePeriod;
use Illuminate\Database\Eloquent\Factories\Factory;

class SequencePeriodFactory extends Factory
{
Expand All @@ -23,7 +24,7 @@ class SequencePeriodFactory extends Factory
public function definition()
{
return [
'date' => $this->faker->dateTimeBetween('-1 year')->format('Y-m-d'),
'date' => $this->faker->dateTimeBetween('-1 year')->format('Y-m-d'),
'ordinal_number' => $this->faker->numberBetween(1, 100),
];
}
Expand Down
18 changes: 6 additions & 12 deletions database/factories/SequenceRuleFactory.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use InvalidArgumentException;
use Guava\Sequence\Enums\ResetFrequency;
use Guava\Sequence\Models\SequenceRule;
use Illuminate\Database\Eloquent\Factories\Factory;
use InvalidArgumentException;

class SequenceRuleFactory extends Factory
{
Expand All @@ -25,9 +26,9 @@ class SequenceRuleFactory extends Factory
public function definition()
{
return [
'type' => $this->faker->unique()->word(),
'type' => $this->faker->unique()->word(),
'reset_frequency' => $this->faker->randomElement(ResetFrequency::getValues()),
'pattern' => function (array $attributes) {
'pattern' => function (array $attributes) {
return $this->faker->randomElement(
$this->examplePatterns($attributes['reset_frequency'])
);
Expand All @@ -38,8 +39,7 @@ public function definition()
/**
* Get example patterns for given reset frequency
*
* @param string $resetFrequency
* @return array<string>
* @return array<string>
*/
private function examplePatterns(string $resetFrequency): array
{
Expand All @@ -59,8 +59,6 @@ private function examplePatterns(string $resetFrequency): array

/**
* Indicate that sequence should resets daily.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function resetsDaily(): Factory
{
Expand All @@ -73,8 +71,6 @@ public function resetsDaily(): Factory

/**
* Indicate that sequence should resets monthly.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function resetsMonthly(): Factory
{
Expand All @@ -87,8 +83,6 @@ public function resetsMonthly(): Factory

/**
* Indicate that sequence should resets yearly.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function resetsYearly(): Factory
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
Expand All @@ -17,7 +18,8 @@ public function up()
Schema::create('sequence_periods', function (Blueprint $table) {
$table->id();
$table->foreignId('rule_id')->constrained('sequence_rules')
->cascadeOnUpdate()->restrictOnDelete();
->cascadeOnUpdate()->restrictOnDelete()
;
$table->date('date');
$table->unsignedInteger('ordinal_number');
$table->timestamps();
Expand Down
2 changes: 1 addition & 1 deletion src/Enums/ResetFrequency.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence\Enums;


enum ResetFrequency: string
{
case Yearly = 'yearly';
Expand Down
11 changes: 6 additions & 5 deletions src/Models/SequencePeriod.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence\Models;

use Guava\Sequence\Database\Factories\SequencePeriodFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Guava\Sequence\Database\Factories\SequencePeriodFactory;

/**
* @property string $date
* @property int $ordinal_number
* @property string $date
* @property int $ordinal_number
*/
class SequencePeriod extends Model
{
Expand All @@ -28,15 +29,15 @@ protected static function newFactory()
}

/**
* @var array<string>
* @var array<string>
*/
protected $fillable = [
'date',
'ordinal_number',
];

/**
* @var array<string, string>
* @var array<string, string>
*/
protected $casts = [
'ordinal_number' => 'integer',
Expand Down
21 changes: 7 additions & 14 deletions src/Models/SequenceRule.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence\Models;

use Guava\Sequence\Database\Factories\SequenceRuleFactory;
use Guava\Sequence\Enums\ResetFrequency;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Guava\Sequence\Database\Factories\SequenceRuleFactory;
use Guava\Sequence\Enums\ResetFrequency;

/**
* @property string $pattern
* @property string $reset_frequency
* @property string $pattern
* @property string $reset_frequency
*/
class SequenceRule extends Model
{
Expand All @@ -20,7 +21,7 @@ class SequenceRule extends Model
public $table = 'sequence_rules';

/**
* @var array<string>
* @var array<string>
*/
protected $fillable = [
'type',
Expand All @@ -40,8 +41,6 @@ protected static function newFactory()

/**
* Get the related periods.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function periods(): HasMany
{
Expand All @@ -50,8 +49,6 @@ public function periods(): HasMany

/**
* Decide whether ordinal number needs to be reset yearly.
*
* @return bool
*/
public function needsYearlyReset(): bool
{
Expand All @@ -64,8 +61,6 @@ public function needsYearlyReset(): bool

/**
* Decide whether ordinal number needs to be reset monthly.
*
* @return bool
*/
public function needsMonthlyReset(): bool
{
Expand All @@ -77,13 +72,11 @@ public function needsMonthlyReset(): bool

/**
* Decide whether ordinal number needs to be reset daily.
*
* @return bool
*/
public function needsDailyReset(): bool
{
return in_array(ResetFrequency::tryFrom($this->reset_frequency), [
ResetFrequency::Daily
ResetFrequency::Daily,
]);
}
}
47 changes: 18 additions & 29 deletions src/Sequence.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence;
Expand All @@ -12,30 +13,26 @@ class Sequence
/**
* The period instance to use on sequence.
*
* @var \Guava\Sequence\Models\SequencePeriod
* @var \Guava\Sequence\Models\SequencePeriod
*/
protected $period;

/**
* The date to use on sequence.
*
* @var DateTimeInterface
* @var DateTimeInterface
*/
protected $date;

/**
* The number pattern to use on sequence.
*
* @var string
* @var string
*/
protected $pattern;

/**
* The sequence constructor.
*
* @param \Guava\Sequence\Models\SequencePeriod $period
* @param string $pattern
* @param DateTimeInterface $date
*/
public function __construct(SequencePeriod $period, string $pattern, DateTimeInterface $date)
{
Expand All @@ -46,29 +43,27 @@ public function __construct(SequencePeriod $period, string $pattern, DateTimeInt

/**
* Get the sequence number.
*
* @return string
*/
public function getNumber(bool $increment = false): string
{
// dd(str($this->getPattern())
// ->pipe(fn(Stringable $str) => str(strtr($str->toString(), [
// '{number}' => $this->getOrdinalNumber(),
// '{day}' => $this->date->format('d'),
// '{month}' => $this->date->format('m'),
// '{year}' => $this->date->format('Y'),
// '{day_short}' => $this->date->format('j'),
// '{month_short}' => $this->date->format('n'),
// '{year_short}' => $this->date->format('y'),
// ]))));
// dd(str($this->getPattern())
// ->pipe(fn(Stringable $str) => str(strtr($str->toString(), [
// '{number}' => $this->getOrdinalNumber(),
// '{day}' => $this->date->format('d'),
// '{month}' => $this->date->format('m'),
// '{year}' => $this->date->format('Y'),
// '{day_short}' => $this->date->format('j'),
// '{month_short}' => $this->date->format('n'),
// '{year_short}' => $this->date->format('y'),
// ]))));
$result = strtr(
str($this->getPattern())
->replaceMatches(
'/\{number:(\d+)\}/',
fn($matches) => str_pad(
(string)$this->getOrdinalNumber(),
(int)$matches[1],
"0",
fn ($matches) => str_pad(
(string) $this->getOrdinalNumber(),
(int) $matches[1],
'0',
STR_PAD_LEFT
)
)
Expand All @@ -93,8 +88,6 @@ public function getNumber(bool $increment = false): string

/**
* Get the ordinal number of sequence.
*
* @return int
*/
public function getOrdinalNumber(bool $increment = false): int
{
Expand All @@ -109,8 +102,6 @@ public function getOrdinalNumber(bool $increment = false): int

/**
* Get the pattern of sequence.
*
* @return string
*/
public function getPattern(): string
{
Expand All @@ -119,8 +110,6 @@ public function getPattern(): string

/**
* Increment ordinal number of period.
*
* @return void
*/
public function increment(): void
{
Expand Down
9 changes: 2 additions & 7 deletions src/SequenceFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Guava\Sequence;
Expand All @@ -11,14 +12,12 @@ class SequenceFactory
/**
* The instance of sequence query helper.
*
* @var \Guava\Sequence\SequenceQuery
* @var \Guava\Sequence\SequenceQuery
*/
protected $query;

/**
* The sequence factory constructor.
*
* @param \Guava\Sequence\SequenceQuery $query
*/
public function __construct(SequenceQuery $query)
{
Expand All @@ -27,10 +26,6 @@ public function __construct(SequenceQuery $query)

/**
* Create instance of sequence.
*
* @param string $type
* @param DateTimeInterface $date
* @return \Guava\Sequence\Sequence
*/
public function create(string $type, DateTimeInterface $date): Sequence
{
Expand Down
Loading

0 comments on commit 2930e8e

Please sign in to comment.