Skip to content

Commit

Permalink
Merge pull request #5 from sssurii/develop
Browse files Browse the repository at this point in the history
Fix constants file missing - add package config file
  • Loading branch information
sssurii authored Dec 17, 2023
2 parents 1694ab6 + 3881005 commit 67ede9a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.env
.phpunit.result.cache
composer.lock
.idea/
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Install using composer:

`composer require sssurii/laravel-ics`

To publish the config, run the vendor publish command:

`php artisan vendor:publish --provider="INSAN\ICS\ICSServiceProvider" --tag=config`


## Usage
1. For Laravel 5.6 or above, simply load class:
Expand All @@ -21,13 +25,13 @@ Install using composer:
INSAN\ICS\ICSServiceProvider::class,
```
Then simply load class as above case above.
Then simply load class as in step 1.

3. Use package class as below:

Set various event details, pass properties as array while initializing class:
```
$properities = [
$event_properties = [
'uid' => uniqid(),
'sequence' => 0,
'description' => 'Event Invitation via email.',
Expand All @@ -36,8 +40,8 @@ Set various event details, pass properties as array while initializing class:
'summary' => 'This is an event invitation sent through email.',
'location' => 'VR Punjab, S.A.S Nagar, Chandigarh',
'url' => 'www.example.com',
]
$ics_file = new ICS($properities);
];
$ics_file = new ICS($event_properties);
return $ics_file->toString();
```
Some optional properties can be set as below, like set organizer details
Expand Down
26 changes: 26 additions & 0 deletions config/ics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return [
/*
* Set DAY_LIGHT_SAVING to true if you are sending invite from Day Light Saving region. Else keep it false.
*/
'DAY_LIGHT_SAVING' => env('DAY_LIGHT_SAVING', false),

/*
* Set DAY_LIGHT_SAVING_START_MONTH to the month number in which day light saving starts in your region.
* For example, in USA it is 3 for March.
*/
'DAY_LIGHT_SAVING_START_MONTH' => env('DAY_LIGHT_SAVING_START_MONTH', '03'),

/*
* Set DAY_LIGHT_SAVING_END_MONTH to the month number in which day light saving ends in your region.
* For example, in USA it is 11 for November.
*/
'DAY_LIGHT_SAVING_END_MONTH' => env('DAY_LIGHT_SAVING_END_MONTH', '10'),

/*
* Set DAY_LIGHT_SAVING_OFFSET to the offset in hours for day light saving.
* For example, in USA it is 1 hour.
*/
'DAY_LIGHT_SAVING_OFFSET' => '1 hours',
];
57 changes: 31 additions & 26 deletions src/ICS.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
class ICS
{
protected const DATETIME_FORMAT = 'Ymd\THis\Z';
protected const DATE_FORMAT = 'Y-m-d';

protected $properties = [];
protected array $properties = [];

private $organiser = '';
private string $organiser = '';

private $available_properties = [
private array $available_properties = [
'categories',
'priority',
'description',
Expand All @@ -25,7 +26,7 @@ class ICS
'sequence',
];

private $header_properties = [
private array $header_properties = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//hacksw/handcal//NONSGML v1.0//EN',
Expand All @@ -52,13 +53,13 @@ public function set($properties, $value)
}
}

public function toString()
public function toString(): string
{
$rows = $this->buildICSProperties();
return implode("\r\n", $rows);
}

private function buildICSProperties()
private function buildICSProperties(): array
{
$ics_properties = $this->getDefaultHeaderProperties();

Expand All @@ -72,8 +73,9 @@ private function buildICSProperties()
return $ics_properties;
}

private function sanitizeValue($value, $attribute) {
switch($attribute) {
private function sanitizeValue(string $value, string $attribute): string
{
switch ($attribute) {
case 'dtend':
case 'dtstamp':
case 'dtstart':
Expand All @@ -85,38 +87,41 @@ private function sanitizeValue($value, $attribute) {
return $value;
}

private function formatTimestamp($timestamp) {
private function formatTimestamp(string $timestamp)
{
$day_light_start = strtotime('last sunday of ' . date('Y') . '-'
. config('constants.DAY_LIGHT_SAVING_START_MONTH'));
. config('ics.DAY_LIGHT_SAVING_START_MONTH'));
$day_light_end = strtotime('last sunday of ' . date('Y') . '-'
. config('constants.DAY_LIGHT_SAVING_END_MONTH'));
. config('ics.DAY_LIGHT_SAVING_END_MONTH'));

$datetime = new DateTime($timestamp);
if (env('DAY_LIGHT_SAVING', false)
&& $datetime->format(config('constants.DATE')) >= date(config('constants.DATE'), $day_light_start)
&& $datetime->format(config('constants.DATE')) <= date(config('constants.DATE'), $day_light_end)
if (
config('ics.DAY_LIGHT_SAVING')
&& $datetime->format(self::DATE_FORMAT) >= date(self::DATE_FORMAT, $day_light_start)
&& $datetime->format(self::DATE_FORMAT) <= date(self::DATE_FORMAT, $day_light_end)
) {
$datetime->modify('-' . config('constants.DAY_LIGHT_SAVING_OFFSET'));
$datetime->modify('-' . config('ics.DAY_LIGHT_SAVING_OFFSET'));
}

return $datetime->format(self::DATETIME_FORMAT);
}

private function escapeString($str) {
private function escapeString(string $str): string
{
return preg_replace('/([\,;])/', '\\\$1', $str);
}

public function setOrganizer($name, $email)
public function setOrganizer(string $name, string $email): void
{
$this->organiser = 'ORGANIZER;CN='. $name .':MAILTO:' . $email;
$this->organiser = 'ORGANIZER;CN=' . $name . ':MAILTO:' . $email;
}

public function getOrganizer()
public function getOrganizer(): string
{
return $this->organiser;
}

public function markEventCancel()
public function markEventCancel(): void
{
$method_key = array_search('METHOD:REQUEST', $this->header_properties);

Expand All @@ -125,12 +130,12 @@ public function markEventCancel()
}
}

private function getDefaultHeaderProperties()
private function getDefaultHeaderProperties(): array
{
return $this->header_properties;
}

private function appendProperties(array $ics_properties)
private function appendProperties(array $ics_properties): array
{
$properties = $this->buildProperties();

Expand All @@ -141,10 +146,10 @@ private function appendProperties(array $ics_properties)
return $ics_properties;
}

private function buildProperties()
private function buildProperties(): array
{
$properties = [];
foreach($this->properties as $attribute => $value) {
foreach ($this->properties as $attribute => $value) {
$properties[strtoupper($attribute)] = $value;
}

Expand All @@ -153,11 +158,11 @@ private function buildProperties()
return $properties;
}

private function addDefaultFooterProperties(array $ics_properties)
private function addDefaultFooterProperties(array $ics_properties): array
{
$ics_properties[] = 'END:VEVENT';
$ics_properties[] = 'END:VCALENDAR';

return $ics_properties;
}
}
}
8 changes: 5 additions & 3 deletions src/ICSServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ICSServiceProvider extends ServiceProvider
*
* @return void
*/
public function register()
public function register(): void
{
$this->app->make('INSAN\ICS\ICS');
}
Expand All @@ -21,8 +21,10 @@ public function register()
*
* @return void
*/
public function boot()
public function boot(): void
{
//
$this->publishes([
__DIR__.'/../config/ics.php' => config_path('ics.php'),
], 'config');
}
}

0 comments on commit 67ede9a

Please sign in to comment.