Skip to content

Commit

Permalink
Add test for multiple categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Ožana committed Sep 11, 2020
1 parent 6b0bb8d commit 1b7c1bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 65 deletions.
94 changes: 34 additions & 60 deletions src/IcalParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
*/
class IcalParser {

const MULTIPLE_VALUES_KEY_MAPPING = [
'ATTACH' => 'ATTACHMENTS',
'EXDATE' => 'EXDATES',
'RDATE' => 'RDATES',
'CATEGORIES' => 'CATEGORIES',
'X-CATEGORIES' => 'X-CATEGORIES',
];

/** @var DateTimeZone */
public $timezone;

Expand All @@ -42,13 +34,11 @@ public function __construct() {

/**
* @param string $file
* @param null $callback
* @param callable|null $callback
* @return array|null
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws Exception
*/
public function parseFile($file, $callback = null): array {
public function parseFile(string $file, callable $callback = null): array {
if (!$handle = fopen($file, 'r')) {
throw new RuntimeException('Can\'t open file' . $file . ' for reading');
}
Expand All @@ -59,13 +49,12 @@ public function parseFile($file, $callback = null): array {

/**
* @param string $string
* @param null $callback
* @param callable $callback
* @param boolean $add if true the parsed string is added to existing data
* @return array|null
* @throws InvalidArgumentException
* @throws Exception
*/
public function parseString($string, $callback = null, $add = false): ?array {
public function parseString(string $string, callable $callback = null, bool $add = false): ?array {
if ($add === false) {
// delete old data
$this->data = [];
Expand Down Expand Up @@ -154,14 +143,27 @@ public function parseString($string, $callback = null, $add = false): ?array {
// a new one specifically for the array of values.

if ($newKey = $this->isMultipleKey($key)) {
foreach ((array)$value as $v) {
$this->data[$section][$this->counters[$section]][$newKey][] = $v;
}
$this->data[$section][$this->counters[$section]][$newKey][] = $value;
}

if ($key !== $newKey) {
// CATEGORIES can be multiple also but there is special case that there are comma separated categories

if ($this->isMultipleKeyWithCommaSeparation($key)) {

if (strpos($value, ',') !== false) {
$values = array_map('trim', preg_split('/(?<![^\\\\]\\\\),/', $value));
} else {
$values = [$value];
}

foreach ($values as $value) {
$this->data[$section][$this->counters[$section]][$key][] = $value;
}

} else {
$this->data[$section][$this->counters[$section]][$key] = $value;
}

}

}
Expand Down Expand Up @@ -237,10 +239,6 @@ public function parseRecurrences($event): array {
return $recurrences;
}

/**
* @param $row
* @return array
*/
private function parseRow($row): array {
preg_match('#^([\w-]+);?([\w-]+="[^"]*"|.*?):(.*)$#i', $row, $matches);

Expand Down Expand Up @@ -338,17 +336,6 @@ private function parseRow($row): array {
}
}

if ($key === 'CATEGORIES' || $key === 'X-CATEGORIES') {

// split comma separated CATEGORIES
if (strpos($value, ',') !== false) {
$value = array_map('trim', preg_split('/(?<![^\\\\]\\\\),/', $value));
}

// return categories array
$value = $value;
}

return [$key, $middle, $value];
}

Expand All @@ -358,43 +345,36 @@ private function parseRow($row): array {
* @param string $zone
* @return mixed|null
*/
private function toTimezone($zone) {
return isset($this->windowsTimezones[$zone]) ? $this->windowsTimezones[$zone] : $zone;
private function toTimezone(string $zone) {
return $this->windowsTimezones[$zone] ?? $zone;
}

public function isMultipleKey(string $key): ?string {
return (['ATTACH' => 'ATTACHMENTS', 'EXDATE' => 'EXDATES', 'RDATE' => 'RDATES'])[$key] ?? null;
}

/**
* @param string $key
* @param $key
* @return string|null
*/
public function isMultipleKey($key) {
return isset(self::MULTIPLE_VALUES_KEY_MAPPING[$key]) ? self::MULTIPLE_VALUES_KEY_MAPPING[$key] : null;
public function isMultipleKeyWithCommaSeparation($key): ?string {
return (['X-CATEGORIES' => 'X-CATEGORIES', 'CATEGORIES' => 'CATEGORIES'])[$key] ?? null;
}

/**
* @return array
*/
public function getAlarms() {
return isset($this->data['VALARM']) ? $this->data['VALARM'] : [];
public function getAlarms(): array {
return $this->data['VALARM'] ?? [];
}

/**
* @return array
*/
public function getTimezone() {
public function getTimezone(): array {
return $this->getTimezones();
}

/**
* @return array
*/
public function getTimezones(): array {
return isset($this->data['VTIMEZONE']) ? $this->data['VTIMEZONE'] : [];
return $this->data['VTIMEZONE'] ?? [];
}

/**
* Return sorted event list as array
*
* @return array
*/
public function getSortedEvents(): array {
if ($events = $this->getEvents()) {
Expand All @@ -408,9 +388,6 @@ public function getSortedEvents(): array {
return [];
}

/**
* @return array
*/
public function getEvents(): array {
$events = [];
if (isset($this->data['VEVENT'])) {
Expand Down Expand Up @@ -487,9 +464,6 @@ public function getEvents(): array {
return $events;
}

/**
* @return array
*/
public function getReverseSortedEvents(): array {
if ($events = $this->getEvents()) {
usort(
Expand Down
8 changes: 3 additions & 5 deletions tests/multiple_categories.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ $results = $cal->parseFile(__DIR__ . '/cal/multiple_categories.ics');
$events = $cal->getSortedEvents();

foreach ($events as $event) {
var_dump($event['CATEGORIES']);
}
//var_dump($results);

Assert::true(true);
Assert::type('array', $event['CATEGORIES']);
Assert::same(['one', 'two', 'three'], $event['CATEGORIES']);
}

0 comments on commit 1b7c1bb

Please sign in to comment.