Skip to content

Commit 42bff92

Browse files
committed
Added support for future dates.
1 parent 4fc83ad commit 42bff92

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ php artisan migrate
4545

4646
## Console Command
4747

48-
Run the console command to populate the calendar table.
48+
Run the console command to fill the calendar table.
4949

50-
> Note: The console command accepts an optional **starting year** parameter. If none is provided you will be prompted to enter one.
50+
> Note: If no **starting year** parameter is provided you will be prompted to enter one. If no **ending year** is provided it will default to the current year.
5151
5252
```
53-
php artisan calendar:table --year=2020
53+
php artisan calendar:table --start=1990 --end=2030
5454
```
5555

5656
## License

src/Commands/CalendarTableCommand.php

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CalendarTableCommand extends Command
1313
*
1414
* @var string
1515
*/
16-
protected $signature = 'calendar:table {--year=}';
16+
protected $signature = 'calendar:table {--start=} {--end=}';
1717

1818
/**
1919
* The console command description.
@@ -44,16 +44,34 @@ public function __construct()
4444
*/
4545
public function handle()
4646
{
47-
$startYear = $this->option('year');
47+
$startYear = $this->option('start');
48+
$endYear = $this->option('end');
4849

4950
if (! $startYear) {
5051
$startYear = $this->ask('Please enter a starting year');
5152
}
5253

53-
$currentYear = Carbon::now()->year;
54+
if (! $endYear) {
55+
$endYear = Carbon::now()->year;
56+
}
57+
58+
$startYear = (int) $startYear;
59+
$endYear = (int) $endYear;
60+
61+
if (! $this->isValidYear($startYear)) {
62+
$this->error('Invalid start year format.');
63+
}
64+
65+
if (! $this->isValidYear($endYear)) {
66+
$this->error('Invalid end year format.');
67+
}
68+
69+
if ($startYear > $endYear) {
70+
$this->error('Starting period is greater than ending.');
71+
}
5472

5573
if ($this->count()) {
56-
$this->error('Calendar table has already been filled.');
74+
$this->error('Calendar table is not empty.');
5775

5876
if ($this->confirm('Do you wish to truncate the table')) {
5977
$this->truncate();
@@ -62,11 +80,11 @@ public function handle()
6280
}
6381
}
6482

65-
$this->insert($startYear);
83+
$this->insert($startYear, $endYear);
6684

6785
$count = $this->count();
6886

69-
$this->info("Successfully added {$count} records starting from {$startYear} to {$currentYear}.");
87+
$this->info("Successfully added {$count} records starting from {$startYear} to {$endYear}.");
7088
}
7189

7290
/**
@@ -95,12 +113,13 @@ public function count()
95113
/**
96114
* Insert dates from the start year to the current date into the table.
97115
*
98-
* @param string $startYear The start year for the date range.
116+
* @param int $startYear The start year for the date range.
117+
* @param int $endYear The start year for the date range.
99118
*/
100-
public function insert(string $startYear)
119+
public function insert(int $startYear, int $endYear)
101120
{
102121
$startDate = Carbon::createFromDate($startYear, 1, 1);
103-
$endDate = Carbon::now();
122+
$endDate = Carbon::createFromDate($endYear, 12, 31);
104123

105124
for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
106125
DB::table($this->tableName)->insert([
@@ -115,4 +134,30 @@ public function insert(string $startYear)
115134
]);
116135
}
117136
}
137+
138+
/**
139+
* Checks if the input is a valid year.
140+
*
141+
* This function checks if the input is a numeric value and if it falls within the range of 1000 to 9999,
142+
* which covers all valid 4-digit years. If both conditions are met, the function returns true;
143+
* otherwise, it returns false.
144+
*
145+
* @param string $input The input to be validated.
146+
* @return bool Returns true if the input is a valid year, false otherwise.
147+
*/
148+
public function isValidYear($input)
149+
{
150+
// Check if the input is a numeric value
151+
if (is_numeric($input)) {
152+
// Convert the input to an integer
153+
$year = intval($input);
154+
155+
// Check if the year is in the valid range
156+
if ($year >= 1000 && $year <= 9999) {
157+
return true;
158+
}
159+
}
160+
161+
return false;
162+
}
118163
}

tests/CommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setup(): void
1919

2020
$this->tableName = config('calendar-table.table_name');
2121

22-
$this->consoleCommand = "calendar:table --year={$this->startYear}";
22+
$this->consoleCommand = "calendar:table --start={$this->startYear}";
2323

2424
Artisan::call('migrate');
2525
}
@@ -44,7 +44,7 @@ public function it_checks_if_result_count_is_correct_in_database()
4444
$result = DB::table($this->tableName)->count();
4545

4646
// Assert: Check if the result count is correct
47-
$this->assertEquals(1394, $result);
47+
$this->assertEquals(1461, $result);
4848
}
4949

5050
/** @test */

0 commit comments

Comments
 (0)