|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Eluceo\iCal; |
|
|
|
|
|
use Eluceo\iCal\Util\ComponentUtil; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract class Component |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $components = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $componentsBuildOrder = ['VTIMEZONE', 'DAYLIGHT', 'STANDARD']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract public function getType(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract public function buildPropertyBag(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function addComponent(self $component, $key = null) |
|
|
{ |
|
|
if (null == $key) { |
|
|
$this->components[] = $component; |
|
|
} else { |
|
|
$this->components[$key] = $component; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setComponents(array $components) |
|
|
{ |
|
|
$this->components = $components; |
|
|
|
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function build() |
|
|
{ |
|
|
$lines = []; |
|
|
|
|
|
$lines[] = sprintf('BEGIN:%s', $this->getType()); |
|
|
|
|
|
|
|
|
foreach ($this->buildPropertyBag() as $property) { |
|
|
foreach ($property->toLines() as $l) { |
|
|
$lines[] = $l; |
|
|
} |
|
|
} |
|
|
|
|
|
$this->buildComponents($lines); |
|
|
|
|
|
$lines[] = sprintf('END:%s', $this->getType()); |
|
|
|
|
|
$ret = []; |
|
|
|
|
|
foreach ($lines as $line) { |
|
|
foreach (ComponentUtil::fold($line) as $l) { |
|
|
$ret[] = $l; |
|
|
} |
|
|
} |
|
|
|
|
|
return $ret; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function render() |
|
|
{ |
|
|
return implode("\r\n", $this->build()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __toString() |
|
|
{ |
|
|
return $this->render(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function buildComponents(array &$lines) |
|
|
{ |
|
|
$componentsByType = []; |
|
|
|
|
|
|
|
|
foreach ($this->components as $component) { |
|
|
$type = $component->getType(); |
|
|
if (!isset($componentsByType[$type])) { |
|
|
$componentsByType[$type] = []; |
|
|
} |
|
|
$componentsByType[$type][] = $component; |
|
|
} |
|
|
|
|
|
|
|
|
foreach ($this->componentsBuildOrder as $type) { |
|
|
if (!isset($componentsByType[$type])) { |
|
|
continue; |
|
|
} |
|
|
foreach ($componentsByType[$type] as $component) { |
|
|
$this->addComponentLines($lines, $component); |
|
|
} |
|
|
unset($componentsByType[$type]); |
|
|
} |
|
|
|
|
|
|
|
|
foreach ($componentsByType as $components) { |
|
|
foreach ($components as $component) { |
|
|
$this->addComponentLines($lines, $component); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function addComponentLines(array &$lines, self $component) |
|
|
{ |
|
|
foreach ($component->build() as $l) { |
|
|
$lines[] = $l; |
|
|
} |
|
|
} |
|
|
} |
|
|
|