Skip to content

Commit

Permalink
Add alarm component
Browse files Browse the repository at this point in the history
  • Loading branch information
Uweb95 committed Dec 4, 2017
1 parent 3a46412 commit f2de4b9
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 32 deletions.
11 changes: 11 additions & 0 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,17 @@ public function addRecurrencePeriods(array $periods, array $parameters = null) {
public function setRecurrenceRule(RecurrenceRule $rule) {
$this->instance->add($this->calendar->createProperty('RRULE', $rule->toString()));
}

/**
* Add alarm component to current entity
*
* @see https://tools.ietf.org/html/rfc5545#section-3.6.6
*
* @return Alarm
*/
public function addAlarm() {
return new Alarm($this->calendar);
}

/**
* Creates a new date time property including check for floating date time
Expand Down
154 changes: 154 additions & 0 deletions src/Entity/Alarm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* (c) Brainformatik GmbH [[email protected]]
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brainformatik\CalDAV\Entity;

use Brainformatik\CalDAV\Enum\Action;
use Brainformatik\CalDAV\Helper\TimeZone;
use Brainformatik\CalDAV\Type\Attendee;
use Brainformatik\CalDAV\Type\Duration;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VAlarm;

class Alarm {

/**
* @var VAlarm
*/
protected $instance;

/**
* @var VCalendar
*/
protected $calendar;

public function __construct(VCalendar $calendar) {
$this->calendar = $calendar;
$this->instance = $this->calendar->add('VALARM');
}

/**
* This property specifies a trigger
*
* @param Duration $duration
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.2.5
*/
public function setTrigger(Duration $duration, array $parameters = null) {
$this->instance->add($this->calendar->createProperty('TRIGGER', $duration->toString(), $parameters));
}

/**
* This property provides the capability to associate a document object
*
* Currently only a URI is supported!
*
* @param string $attachmentPath
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.1.1
*/
public function addAttachment($attachmentPath, array $parameters = null) {

if (!empty($attachmentPath)) {
$this->instance->add($this->calendar->createProperty('ATTACH', $attachmentPath, $parameters));
}
}

/**
* Defines action for alarm
*
* @param Action $action
* @param array|null $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.6.1
*/
public function setAction($action, array $parameters = null) {

if (!Action::has($action)) {
throw new \InvalidArgumentException('This action is not allowed for current entity!');
}

$this->instance->add($this->calendar->createProperty('ACTION', $action, $parameters));
}

/**
* This property provides a more complete description
*
* @param string $description
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.1.5
*/
public function setDescription($description, array $parameters = null) {

if (!empty($description)) {
$this->instance->add($this->calendar->createProperty('DESCRIPTION', $description, $parameters));
}
}

/**
* This property specifies a duration of time
*
* @param Duration $duration
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.2.5
*/
public function setDuration(Duration $duration, array $parameters = null) {
$this->instance->add($this->calendar->createProperty('DURATION', $duration->toString(), $parameters));
}

/**
* This property specifies the repeat count
*
* @param int $repeatCount
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.6.2
*/
public function setRepeatCount($repeatCount, array $parameters = null) {
if (!is_int($repeatCount)) {
throw new \InvalidArgumentException('repeat count should be an integer value!');
}

if (0 >= $repeatCount) {
throw new \OutOfRangeException('repeat count should be greater than 0!');
}

$this->instance->add($this->calendar->createProperty('REPEAT', $repeatCount, $parameters));
}

/**
* This property defines an "Attendee"
*
* @param Attendee $attendee
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.4.1
*/
public function addAttendee(Attendee $attendee) {
$this->instance->add($this->calendar->createProperty('ATTENDEE', $attendee->getAddress(), $attendee->getParameters()));
}

/**
* This property defines a short summary or subject
*
* @param string $summary
* @param array $parameters
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.1.12
*/
public function setSummary($summary, array $parameters = null) {

if (!empty($summary)) {
$this->instance->add($this->calendar->createProperty('SUMMARY', $summary, $parameters));
}
}

}
22 changes: 22 additions & 0 deletions src/Enum/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* (c) Brainformatik GmbH [[email protected]]
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brainformatik\CalDAV\Enum;

/**
* Class Action
*
* @see https://tools.ietf.org/html/rfc5545#section-3.8.6.1
*
* @package Brainformatik\CalDAV\Enum
*/
class Action extends AbstractEnum {
const AUDIO = 'AUDIO';
const DISPLAY = 'DISPLAY';
const EMAIL = 'EMAIL';
}
2 changes: 1 addition & 1 deletion src/Helper/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function assertException(callable $callback, $expectedException = 'Exc

try {
$callback();
} catch (\Exception $e) {
} catch (\Throwable $e) {
$className = get_class($e);

$this->assertInstanceOf($expectedException, $e);
Expand Down
29 changes: 27 additions & 2 deletions src/Type/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Duration implements StringTypeInterface {
* @var int
*/
protected $week = 0;

/**
* @var bool
*/
protected $negative = false;

/**
* Returns the string representation of the object
Expand All @@ -55,8 +60,12 @@ public function toString() {
if (0 === $this->day && 0 === $this->hour && 0 === $this->minute && 0 === $this->second && 0 === $this->week) {
throw new \UnexpectedValueException('At least one duration value must be set!');
}

$duration = 'P';

if ($this->negative) {
$duration = '-P';
} else {
$duration = 'P';
}

if (0 !== $this->week) {
$duration .= $this->week . 'W';
Expand Down Expand Up @@ -165,4 +174,20 @@ public function setWeek($week) {

return $this;
}

/**
* @param bool $negative
*
* @return Duration
*/
public function setNegative($negative = true) {

if (!is_bool($negative)) {
throw new \InvalidArgumentException('Value for negative must be a boolean');
}

$this->negative = $negative;

return $this;
}
}
Loading

0 comments on commit f2de4b9

Please sign in to comment.