Skip to content

Commit 990ad50

Browse files
committed
Minor adjustments.
1 parent e0307ef commit 990ad50

File tree

4 files changed

+125
-67
lines changed

4 files changed

+125
-67
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,79 @@ The calendar table command accepts two optional parameters. If no **start** year
5050
> Note: If the table has been pre-filled you will be given the option to truncate.
5151
5252
```
53-
php artisan calendar:table --start=1990 --end=2030
53+
php artisan calendar:table --start=2000 --end=2030
5454
```
5555

56+
Sure, here's a README.md section that explains the configuration options for your Laravel Calendar Table package:
57+
58+
## Configuration
59+
60+
The Laravel Calendar Table package provides several configuration options that you can adjust to suit your needs. You can find these options in the `config.php` file.
61+
62+
### Table Name
63+
64+
The `table_name` option allows you to define a custom table name for the database records. By default, it is set to `'date_dimension'`.
65+
66+
```php
67+
'table_name' => 'date_dimension',
68+
```
69+
70+
### Seasons
71+
72+
The `seasons` array allows you to define the start month for each season. By default, it is configured for the meteorological seasons of the Northern Hemisphere:
73+
74+
- Spring starts in March
75+
- Summer starts in June
76+
- Autumn starts in September
77+
- Winter starts in December
78+
79+
If you are in the Southern Hemisphere, you should reconfigure the seasons to start approximately six months later:
80+
81+
- Spring starts in September
82+
- Summer starts in December
83+
- Autumn starts in March
84+
- Winter starts in June
85+
86+
```php
87+
'seasons' => [
88+
'Spring' => 3,
89+
'Summer' => 6,
90+
'Autumn' => 9,
91+
'Winter' => 12,
92+
],
93+
```
94+
95+
### Fiscal Year Start Month
96+
97+
The `fiscal_year_start_month` option allows you to define the start month of the fiscal year. The value should be an integer between 1 (January) and 12 (December). By default, it is set to 10, meaning the fiscal year starts in October. Adjust this setting to match your own fiscal year.
98+
99+
```php
100+
'fiscal_year_start_month' => 10,
101+
```
102+
103+
### Date Range
104+
105+
The `date_range` array allows you to define the max start and end year range for populating the calendar table.
106+
107+
- `start_year`: This option defines the earliest year for the calendar table. Defaults to 20 years before the current year.
108+
109+
- `end_year`: This option defines the latest year for the calendar table. Defaults to 20 years after the current year.
110+
111+
```php
112+
'date_range' => [
113+
'start_year' => Carbon\Carbon::now()->subYears(20)->year,
114+
'end_year' => Carbon\Carbon::now()->addYears(20)->year,
115+
],
116+
```
117+
118+
## Requirements
119+
120+
The package is compatible with Laravel 10 or later.
121+
122+
## Support
123+
124+
If you have any issues or questions please send a pull request.
125+
56126
## License
57127

58128
The MIT License (MIT). Please see [License](LICENSE) for more information.

config/config.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
<?php
22

3-
/**
4-
* Configuration file for the Laravel Calendar Table package.
5-
*/
63
return [
74
/**
8-
* User defined custom table name for database records.
5+
* Defines the database table name.
96
*/
107
'table_name' => 'date_dimension',
118

129
/**
13-
* The 'seasons' array defines the start month for each season.
14-
*
15-
* This configuration assumes the meteorological seasons of the Northern Hemisphere, where the seasons are defined as follows:
16-
* - Spring starts in March (3rd month)
17-
* - Summer starts in June (6th month)
18-
* - Autumn starts in September (9th month)
19-
* - Winter starts in December (12th month)
20-
*
21-
* Users in the Southern Hemisphere should reconfigure the seasons approximately six months offset from those of the Northern Hemisphere:
22-
* - Spring starts in September
23-
* - Summer starts in December
24-
* - Autumn starts in March
25-
* - Winter starts in June
10+
* Defines the start month for each season.
2611
*/
2712
'seasons' => [
2813
'Spring' => 3,
@@ -32,13 +17,15 @@
3217
],
3318

3419
/**
35-
* The 'fiscal_year_start_month' option defines the start month of the fiscal year.
36-
*
37-
* Months are represented as numbers between 1 (January) and 12 (December).
38-
*
39-
* In this case, the fiscal year starts in October (10th month). This might be the case for a company or government that operates on a fiscal year that begins on October 1 and ends on September 30 of the following year.
40-
*
41-
* Users should adjust this setting to match their own fiscal year. For example, if the fiscal year starts in July, they would set this option to 7.
20+
* Defines the start month of the fiscal year.
4221
*/
4322
'fiscal_year_start_month' => 10,
23+
24+
/**
25+
* Defines the valid year range to populate the table.
26+
*/
27+
'date_range' => [
28+
'start_year' => Carbon\Carbon::now()->subYears(20)->year,
29+
'end_year' => Carbon\Carbon::now()->addYears(20)->year,
30+
],
4431
];

src/Commands/CalendarTableCommand.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,24 @@ class CalendarTableCommand extends Command
2626
protected $description = 'A Laravel command to sequence calendar table dates.';
2727

2828
/**
29-
* The database table name.
29+
* The table name to be used.
3030
*/
31-
protected string $tableName;
31+
protected string $tableName = 'date_dimension';
32+
33+
/**
34+
* The fiscal year start month.
35+
*/
36+
protected int $fiscalYearStartMonth = 10;
37+
38+
/**
39+
* The seasons start month array.
40+
*/
41+
protected array $seasons = [
42+
'Spring' => 3,
43+
'Summer' => 6,
44+
'Autumn' => 9,
45+
'Winter' => 12,
46+
];
3247

3348
/**
3449
* Create a new command instance.
@@ -39,7 +54,7 @@ public function __construct()
3954
{
4055
parent::__construct();
4156

42-
$this->tableName = config('calendar-table.table_name', 'date_dimension');
57+
$this->tableName = config('calendar-table.table_name', $this->tableName);
4358
}
4459

4560
/**
@@ -62,19 +77,25 @@ public function handle(): void
6277
$endYear = (int) $endYear;
6378

6479
if (! $this->isValidYear($startYear)) {
65-
$this->error('Invalid start year format.');
80+
$this->error("Invalid start year: {$startYear}");
81+
82+
return;
6683
}
6784

6885
if (! $this->isValidYear($endYear)) {
69-
$this->error('Invalid end year format.');
86+
$this->error("Invalid end year: {$endYear}");
87+
88+
return;
7089
}
7190

7291
if ($startYear > $endYear) {
7392
$this->error('Starting year is greater than ending.');
93+
94+
return;
7495
}
7596

7697
if ($this->count()) {
77-
if ($this->confirm('Table is currently filled would you like to run the truncate command')) {
98+
if ($this->confirm('Table is currently filled would you like to truncate the table?')) {
7899
$this->truncate();
79100
} else {
80101
return;
@@ -112,7 +133,7 @@ public function truncate(): void
112133
*
113134
* @return int The number of records in the table.
114135
*/
115-
public function count()
136+
public function count(): int
116137
{
117138
return DB::table($this->tableName)->count();
118139
}
@@ -153,52 +174,35 @@ public function insert(int $startYear, int $endYear): void
153174
}
154175

155176
/**
156-
* Checks if the input is a valid year.
177+
* Check if the year is valid.
157178
*
158-
* This function checks if the input is a numeric value and if it falls within the range of 1000 to 9999,
159-
* which covers all valid 4-digit years. If both conditions are met, the function returns true;
160-
* otherwise, it returns false.
179+
* This function checks if the year is within the valid range.
161180
*
162-
* @param int $input The input to be validated.
163-
* @return bool Returns true if the input is a valid year, false otherwise.
181+
* @param int $year The year to be checked.
182+
* @return bool True if the year is valid, false otherwise.
164183
*/
165-
public function isValidYear(int $input)
184+
public function isValidYear(int $year): bool
166185
{
167-
// Check if the input is a numeric value
168-
if (is_numeric($input)) {
169-
// Convert the input to an integer
170-
$year = intval($input);
171-
172-
// Check if the year is in the valid range
173-
if ($year >= 1000 && $year <= 9999) {
174-
return true;
175-
}
186+
$startYear = config('calendar-table.date_range.start_year', Carbon::now()->subYears(20)->year);
187+
$endYear = config('calendar-table.date_range.end_year', Carbon::now()->addYears(20)->year);
188+
if ($year >= $startYear && $year <= $endYear) {
189+
return true;
176190
}
177191

178192
return false;
179193
}
180194

181195
/**
182-
* Determines the season for a given date.
196+
* Determine the season for a given date.
183197
*
184-
* This function determines the season for a given date based on the start month of each season.
185-
* The function iterates through the seasons array and returns the first season where the start month is less than or equal to the current month.
186-
* If no season is found, the function returns 'Winter' as the default season.
198+
* This function determines the season for a given date based on the start month of the season.
187199
*
188200
* @param Carbon $date The date for which the season is to be determined.
189201
* @return string The season for the given date.
190202
*/
191203
public function determineSeason(Carbon $date): string
192204
{
193-
$seasons = config('calendar-table.seasons', [
194-
'Spring' => 3,
195-
'Summer' => 6,
196-
'Autumn' => 9,
197-
'Winter' => 12,
198-
]);
199-
200-
// Reverse the seasons array so we can find the first season where the start month is less than or equal to the current month
201-
$seasons = array_reverse($seasons, true);
205+
$seasons = array_reverse(config('calendar-table.seasons', $this->seasons), true);
202206

203207
foreach ($seasons as $season => $startMonth) {
204208
if ($date->month >= $startMonth) {
@@ -210,12 +214,9 @@ public function determineSeason(Carbon $date): string
210214
}
211215

212216
/**
213-
* Determines the fiscal year and quarter for a given date.
217+
* Determine the fiscal year and quarter for a given date.
214218
*
215219
* This function determines the fiscal year and quarter for a given date based on the start month of the fiscal year.
216-
* The function calculates the fiscal year based on the month of the date and the start month of the fiscal year.
217-
* If the month of the date is greater than or equal to the start month of the fiscal year, the fiscal year is incremented by 1.
218-
* The fiscal quarter is calculated based on the month of the date and the start month of the fiscal year.
219220
*
220221
* @param Carbon $date The date for which the fiscal year and quarter are to be determined.
221222
* @return array An array containing the fiscal year and quarter for the given date.
@@ -225,7 +226,7 @@ public function fiscalYearQuarter(Carbon $date): array
225226
$fiscalYear = $date->year;
226227
$fiscalQuarter = ceil($date->month / 3);
227228

228-
$fiscalYearStartMonth = config('calendar-table.fiscal_year_start_month', 10);
229+
$fiscalYearStartMonth = config('calendar-table.fiscal_year_start_month', $this->fiscalYearStartMonth);
229230
if ($date->month >= $fiscalYearStartMonth) {
230231
$fiscalYear++;
231232
$fiscalQuarter = ceil(($date->month - $fiscalYearStartMonth + 1) / 3);

tests/CommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function test_it_asks_to_truncate_database_when_filled()
6464

6565
// Arrange: Insert data into the database expect confirmation
6666
$this->artisan($this->consoleCommand)
67-
->expectsQuestion('Table is currently filled would you like to run the truncate command', 'yes')
67+
->expectsQuestion('Table is currently filled would you like to truncate the table?', 'yes')
6868
->assertExitCode(0);
6969
}
7070
}

0 commit comments

Comments
 (0)