Skip to content

Commit 159b36a

Browse files
committed
Stop using enums
1 parent ece692c commit 159b36a

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class FeatureSeeder extends Seeder
7373
$deployMinutes = Feature::create([
7474
'consumable' => true,
7575
'name' => 'deploy-minutes',
76-
'periodicity_type' => PeriodicityType::Day->name,
76+
'periodicity_type' => PeriodicityType::Day,
7777
'periodicity' => 1,
7878
]);
7979

@@ -85,7 +85,7 @@ class FeatureSeeder extends Seeder
8585
}
8686
```
8787

88-
By saying the `deploy-minutes` is a consumable feature, we are telling the users can use it a limited number of times (or until a given amount). On the other hand, by passing `PeriodicityType::Day->name` and 1 as its `periodicity_type` and `periodicity` respectively, we said that it should be renewed everyday. So a user could spend his minutes today and have it back tomorrow, for instance.
88+
By saying the `deploy-minutes` is a consumable feature, we are telling the users can use it a limited number of times (or until a given amount). On the other hand, by passing `PeriodicityType::Day` and 1 as its `periodicity_type` and `periodicity` respectively, we said that it should be renewed everyday. So a user could spend his minutes today and have it back tomorrow, for instance.
8989

9090
> It is important to keep in mind that both plans and consumable features have its periodicity, so your users can, for instance, have a monthly plan with weekly features.
9191
@@ -110,13 +110,13 @@ class PlanSeeder extends Seeder
110110
{
111111
$silver = Plan::create([
112112
'name' => 'silver',
113-
'periodicity_type' => PeriodicityType::Month->name,
113+
'periodicity_type' => PeriodicityType::Month,
114114
'periodicity' => 1,
115115
]);
116116

117117
$gold = Plan::create([
118118
'name' => 'gold',
119-
'periodicity_type' => PeriodicityType::Month->name,
119+
'periodicity_type' => PeriodicityType::Month,
120120
'periodicity' => 1,
121121
]);
122122
}

database/factories/FeatureFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function definition()
2222
'name' => $this->faker->words(asText: true),
2323
'periodicity' => $this->faker->randomDigitNotZero(),
2424
'periodicity_type' => $this->faker->randomElement([
25-
PeriodicityType::Year->name,
26-
PeriodicityType::Month->name,
27-
PeriodicityType::Week->name,
28-
PeriodicityType::Day->name,
25+
PeriodicityType::Year,
26+
PeriodicityType::Month,
27+
PeriodicityType::Week,
28+
PeriodicityType::Day,
2929
]),
3030
];
3131
}

database/factories/PlanFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function definition()
2121
'name' => $this->faker->words(asText: true),
2222
'periodicity' => $this->faker->randomDigitNotZero(),
2323
'periodicity_type' => $this->faker->randomElement([
24-
PeriodicityType::Year->name,
25-
PeriodicityType::Month->name,
26-
PeriodicityType::Week->name,
27-
PeriodicityType::Day->name,
24+
PeriodicityType::Year,
25+
PeriodicityType::Month,
26+
PeriodicityType::Week,
27+
PeriodicityType::Day,
2828
]),
2929
];
3030
}

src/Enums/PeriodicityType.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
use Illuminate\Support\Carbon;
66
use Illuminate\Support\Str;
77

8-
enum PeriodicityType
8+
class PeriodicityType
99
{
10-
case Year;
11-
case Month;
12-
case Week;
13-
case Day;
10+
public const Year = 'Year';
1411

15-
public static function getDateDifference(Carbon $from, Carbon $to, string|PeriodicityType $unit): int
12+
public const Month = 'Month';
13+
14+
public const Week = 'Week';
15+
16+
public const Day = 'Day';
17+
18+
public static function getDateDifference(Carbon $from, Carbon $to, string $unit): int
1619
{
17-
$unitName = $unit->name ?? $unit;
18-
$unitInPlural = Str::plural($unitName);
20+
$unitInPlural = Str::plural($unit);
1921

2022
$differenceMethodName = 'diffIn' . $unitInPlural;
2123

tests/Models/FeatureTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testModelCalculateYearlyExpiration()
2020

2121
$years = $this->faker->randomDigitNotZero();
2222
$feature = Feature::factory()->create([
23-
'periodicity_type' => PeriodicityType::Year->name,
23+
'periodicity_type' => PeriodicityType::Year,
2424
'periodicity' => $years,
2525
]);
2626

@@ -33,7 +33,7 @@ public function testModelCalculateMonthlyExpiration()
3333

3434
$months = $this->faker->randomDigitNotZero();
3535
$feature = Feature::factory()->create([
36-
'periodicity_type' => PeriodicityType::Month->name,
36+
'periodicity_type' => PeriodicityType::Month,
3737
'periodicity' => $months,
3838
]);
3939

@@ -46,7 +46,7 @@ public function testModelCalculateWeeklyExpiration()
4646

4747
$weeks = $this->faker->randomDigitNotZero();
4848
$feature = Feature::factory()->create([
49-
'periodicity_type' => PeriodicityType::Week->name,
49+
'periodicity_type' => PeriodicityType::Week,
5050
'periodicity' => $weeks,
5151
]);
5252

@@ -59,7 +59,7 @@ public function testModelCalculateDailyExpiration()
5959

6060
$days = $this->faker->randomDigitNotZero();
6161
$feature = Feature::factory()->create([
62-
'periodicity_type' => PeriodicityType::Day->name,
62+
'periodicity_type' => PeriodicityType::Day,
6363
'periodicity' => $days,
6464
]);
6565

@@ -71,7 +71,7 @@ public function testModelcalculateNextRecurrenceEndConsideringRecurrences()
7171
Carbon::setTestNow(now());
7272

7373
$feature = Feature::factory()->create([
74-
'periodicity_type' => PeriodicityType::Week->name,
74+
'periodicity_type' => PeriodicityType::Week,
7575
'periodicity' => 1,
7676
]);
7777

tests/Models/PlanTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testModelCalculateYearlyExpiration()
2020

2121
$years = $this->faker->randomDigitNotZero();
2222
$plan = Plan::factory()->create([
23-
'periodicity_type' => PeriodicityType::Year->name,
23+
'periodicity_type' => PeriodicityType::Year,
2424
'periodicity' => $years,
2525
]);
2626

@@ -33,7 +33,7 @@ public function testModelCalculateMonthlyExpiration()
3333

3434
$months = $this->faker->randomDigitNotZero();
3535
$plan = Plan::factory()->create([
36-
'periodicity_type' => PeriodicityType::Month->name,
36+
'periodicity_type' => PeriodicityType::Month,
3737
'periodicity' => $months,
3838
]);
3939

@@ -46,7 +46,7 @@ public function testModelCalculateWeeklyExpiration()
4646

4747
$weeks = $this->faker->randomDigitNotZero();
4848
$plan = Plan::factory()->create([
49-
'periodicity_type' => PeriodicityType::Week->name,
49+
'periodicity_type' => PeriodicityType::Week,
5050
'periodicity' => $weeks,
5151
]);
5252

@@ -59,7 +59,7 @@ public function testModelCalculateDailyExpiration()
5959

6060
$days = $this->faker->randomDigitNotZero();
6161
$plan = Plan::factory()->create([
62-
'periodicity_type' => PeriodicityType::Day->name,
62+
'periodicity_type' => PeriodicityType::Day,
6363
'periodicity' => $days,
6464
]);
6565

0 commit comments

Comments
 (0)