Skip to content

Commit

Permalink
Merge pull request #3 from rmenner/feature/nthweekday
Browse files Browse the repository at this point in the history
Feature/nthweekday
  • Loading branch information
t1k3 authored Jun 26, 2019
2 parents 94a6068 + 6f607a9 commit 263512b
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ php artisan migrate

## Usage

#### Recurring options
- DAY
- WEEK
- MONTH
- YEAR
- NTHWEEKDAY: nth weekday per month, example 2nd Monday

#### Create CalendarEvent
If you like to attach `User` and/or `Place` then must have:
* configurate `config/calendar-event.php`
Expand Down Expand Up @@ -134,3 +141,6 @@ start_date/start_time - end_date/end_time
### TODO
- [ ] Name conventions, example: `TemplateCalendarEvent::events()` to `TemplateCalendarEvent::calendarEvents()`
- [ ] [Custom validation rule](https://laravel.com/docs/master/validation#custom-validation-rules) to date/time diff

### Special thanks
- [Bit and Pixel](https://bitandpixel.hu)
11 changes: 11 additions & 0 deletions src/Console/Commands/GenerateCalendarEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function handle()
// $endOfRecurring = (clone $date)->addYear();
$diff = $templateCalendarEvent->start_date->diffInYears($endOfRecurring);
$dateNext = $templateCalendarEvent->start_date->addYears($diff);
break;
case RecurringFrequenceType::NTHWEEKDAY:
$diff = $date->firstOfMonth()->diffInMonths($templateCalendarEvent->start_date);

$nextMonth = $templateCalendarEvent->start_date->addMonths($diff);
$weekdays = getWeekdaysInMonth(
$templateCalendarEvent->start_date->format('l'),
$nextMonth
);

$dateNext = $weekdays[$templateCalendarEvent->start_date->weekOfMonth - 1];
}

if ($dateNext !== null
Expand Down
1 change: 1 addition & 0 deletions src/Enums/RecurringFrequenceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ abstract class RecurringFrequenceType
const WEEK = 'week';
const MONTH = 'month';
const YEAR = 'year';
const NTHWEEKDAY = 'nthweekday';
}
10 changes: 10 additions & 0 deletions src/Models/CalendarEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ public static function showPotentialCalendarEventsOfMonth(\DateTimeInterface $da
case RecurringFrequenceType::YEAR:
$diff = $date->firstOfMonth()->diffInYears($calendarEventTmpLast->start_date);
$dateNext = $calendarEventTmpLast->start_date->addYears($diff);
break;
case RecurringFrequenceType::NTHWEEKDAY:
$diff = $date->firstOfMonth()->diffInMonths($calendarEventTmpLast->start_date);
$nextMonth = $calendarEventTmpLast->start_date->addMonths($diff);

$weekdays = getWeekdaysInMonth(
$calendarEventTmpLast->start_date->format('l'),
$nextMonth
);
$dateNext = $weekdays[$calendarEventTmpLast->start_date->weekOfMonth - 1];
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Models/TemplateCalendarEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public function getNextCalendarEventStartDate(\DateTimeInterface $startDate)
break;
case RecurringFrequenceType::YEAR:
$startDate->addYears($this->frequence_number_of_recurring);
break;
case RecurringFrequenceType::NTHWEEKDAY:
$nextMonth = $startDate->copy()->addMonths($this->frequence_number_of_recurring);
$weekdays = getWeekdaysInMonth(
$startDate->format('l'),
$nextMonth
);
$startDate = $weekdays[$startDate->weekOfMonth - 1];
}

if (($this->end_of_recurring !== null
Expand Down
20 changes: 20 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Carbon\Carbon;
use Carbon\CarbonInterval;

if (!function_exists('arrayIsEqualWithDB')) {
/**
Expand Down Expand Up @@ -28,4 +30,22 @@ function arrayIsEqualWithDB(array $array, \Illuminate\Database\Eloquent\Model $m
}
return true;
}
}

if (!function_exists('getWeekdaysInMonth')) {
/**
* @param string $weekday
* @param date $date
* @return collection
*/
function getWeekdaysInMonth($weekday, $date)
{
$next = $date->copy()->addMonths(1);
return collect(new \DatePeriod(

Carbon::parse("first $weekday of $date"),
CarbonInterval::week(),
Carbon::parse("first $weekday of $next")
));
}
}
48 changes: 48 additions & 0 deletions tests/Unit/Console/Command/GenerateCalendarEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,54 @@ public function dataProvider_for_handle_recurring_generated()
Carbon::parse('2017-08-27'),
Carbon::parse('2017-08-28'),
],
[
[
'start_date' => '2019-07-17',
'start_time' => '19:30',
'end_date' => '2019-07-17',
'end_time' => '20:00',
'description' => str_random(32),
'is_recurring' => true,
'frequence_number_of_recurring' => 1,
'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY,
'is_public' => true,
],
Carbon::parse('2019-07-15'),
Carbon::parse('2019-07-17'),
Carbon::parse('2019-07-17'),
],
[
[
'start_date' => '2019-08-21',
'start_time' => '19:30',
'end_date' => '2019-08-21',
'end_time' => '20:00',
'description' => str_random(32),
'is_recurring' => true,
'frequence_number_of_recurring' => 1,
'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY,
'is_public' => true,
],
Carbon::parse('2019-08-19'),
Carbon::parse('2019-08-21'),
Carbon::parse('2019-08-21'),
],
[
[
'start_date' => '2019-09-18',
'start_time' => '19:30',
'end_date' => '2019-09-18',
'end_time' => '20:00',
'description' => str_random(32),
'is_recurring' => true,
'frequence_number_of_recurring' => 1,
'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY,
'is_public' => true,
],
Carbon::parse('2019-09-16'),
Carbon::parse('2019-09-18'),
Carbon::parse('2019-09-18'),
],
];
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Models/CalendarEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,37 @@ public function eventsOfMonth()
$this->assertTrue($isExist);
}
}

/**
* @test
*/
public function getEventsOfMonth_for_nthweekRecurring()
{
$this->calendarEvent->createCalendarEvent([
'start_date' => '2019-06-19',
'start_time' => '19:30',
'end_date' => '2019-06-19',
'end_time' => '20:00',
'description' => str_random(32),
'is_recurring' => true,
'frequence_number_of_recurring' => 1,
'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY,
'is_public' => true,
]);


$calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-07'));
$this->assertEquals(1, $calendarEvents->count());
$this->assertEquals('2019-07-17', $calendarEvents->first()->start_date->format('Y-m-d'));

$calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-08'));
$this->assertEquals(1, $calendarEvents->count());
$this->assertEquals('2019-08-21', $calendarEvents->first()->start_date->format('Y-m-d'));

$calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-09'));
$this->assertEquals(1, $calendarEvents->count());
$this->assertEquals('2019-09-18', $calendarEvents->first()->start_date->format('Y-m-d'));
}

/**
* @test
Expand Down

0 comments on commit 263512b

Please sign in to comment.