<?php
namespace PHPFUI;
class OffCanvas extends \PHPFUI\Base
{
private \PHPFUI\HTML5Element $offCanvas;
private array $offCanvasCollection = [];
private bool $wrapper = false;
public function __construct(private \PHPFUI\HTML5Element $mainContent)
{
$this->offCanvas = new \PHPFUI\HTML5Element('div');
$this->offCanvas->addClass('off-canvas');
$this->offCanvas->addAttribute('data-off-canvas');
}
public function addOff(\PHPFUI\HTML5Element $content, HTML5Element $toggle) : string
{
$toggle->addAttribute('data-toggle', $this->offCanvas->getId());
$id = $content->getId();
$this->offCanvasCollection[$id] = $content;
return $id;
}
public function addWrapper() : static
{
$this->wrapper = true;
return $this;
}
public function setPosition(string $id, string $position) : static
{
$this->validateId($id);
$positions = ['left',
'right',
'top',
'bottom', ];
if (! \in_array($position, $positions))
{
throw new \Exception(__METHOD__ . ": {$position} must be one of " . \implode(',', $positions));
}
$this->offCanvas->addClass("position-{$position}");
return $this;
}
public function setTransition(string $id, string $transition) : static
{
$this->validateId($id);
$transitions = ['over',
'push', ];
if (! \in_array($transition, $transitions))
{
throw new \Exception(__METHOD__ . ": {$transition} must be one of " . \implode(',', $transitions));
}
$this->offCanvas->setAttribute('data-transition', $transition);
return $this;
}
protected function getBody() : string
{
if ($this->wrapper)
{
$wrapper = new \PHPFUI\HTML5Element('div');
$wrapper->addClass('');
$wrapper->addClass('off-canvas-wrapper');
}
else
{
$wrapper = new \PHPFUI\Container();
}
foreach ($this->offCanvasCollection as $id => $off)
{
$this->offCanvas->add($off);
}
$mainContent = new \PHPFUI\HTML5Element('div');
$mainContent->addClass('off-canvas-content');
$mainContent->addAttribute('data-off-canvas-content');
$mainContent->add($this->mainContent);
$wrapper->add($this->offCanvas);
$wrapper->add($mainContent);
return $wrapper;
}
protected function getEnd() : string
{
return '';
}
protected function getStart() : string
{
return '';
}
private function validateId(string $id) : static
{
if (! isset($this->offCanvasCollection[$id]))
{
throw new \Exception(self::class . ": OffCanvas id ({$id}) is not valid");
}
return $this;
}
}