diff --git a/src/Core/InlinePresentation.php b/src/Core/InlinePresentation.php new file mode 100644 index 00000000..13ce502d --- /dev/null +++ b/src/Core/InlinePresentation.php @@ -0,0 +1,52 @@ +. + */ + +namespace BigBlueButton\Core; + +use BigBlueButton\Util\SimpleXMLElementExtended; + +class InlinePresentation extends Presentation +{ + public function __construct(private readonly string $content, string $filename) + { + $this->filename = $filename; + } + + public function getArrayKey(): string + { + return $this->filename; + } + + public function addDocumentToXML(SimpleXMLElementExtended $module): ?SimpleXMLElementExtended + { + $document = parent::addDocumentToXML($module); + + /* @phpstan-ignore-next-line */ + $document[0] = base64_encode($this->content); + + if (isset($this->filename)) { + $document->addAttribute('name', $this->filename); + } + + return $document; + } +} diff --git a/src/Core/Presentation.php b/src/Core/Presentation.php new file mode 100644 index 00000000..06b074a2 --- /dev/null +++ b/src/Core/Presentation.php @@ -0,0 +1,105 @@ +. + */ + +namespace BigBlueButton\Core; + +use BigBlueButton\Util\SimpleXMLElementExtended; + +abstract class Presentation +{ + protected ?string $filename = null; + + protected ?bool $current = null; + + protected ?bool $downloadable = null; + + protected ?bool $removable = null; + + public function addDocumentToXML(SimpleXMLElementExtended $module): ?SimpleXMLElementExtended + { + $document = $module->addChild('document'); + + if (\is_bool($this->downloadable)) { + $document->addAttribute('downloadable', $this->downloadable ? 'true' : 'false'); + } + + if (\is_bool($this->removable)) { + $document->addAttribute('removable', $this->removable ? 'true' : 'false'); + } + + if (\is_bool($this->current)) { + $document->addAttribute('current', $this->current ? 'true' : 'false'); + } + + return $document; + } + + abstract public function getArrayKey(): string; + + public function getFilename(): ?string + { + return $this->filename; + } + + public function setFilename(string $filename): self + { + $this->filename = $filename; + + return $this; + } + + public function getCurrent(): ?bool + { + return $this->current; + } + + public function setCurrent(bool $current): self + { + $this->current = $current; + + return $this; + } + + public function getDownloadable(): ?bool + { + return $this->downloadable; + } + + public function setDownloadable(bool $downloadable): self + { + $this->downloadable = $downloadable; + + return $this; + } + + public function getRemovable(): ?bool + { + return $this->removable; + } + + public function setRemovable(bool $removable): self + { + $this->removable = $removable; + + return $this; + } +} diff --git a/src/Core/UrlPresentation.php b/src/Core/UrlPresentation.php new file mode 100644 index 00000000..896fec4c --- /dev/null +++ b/src/Core/UrlPresentation.php @@ -0,0 +1,49 @@ +. + */ + +namespace BigBlueButton\Core; + +use BigBlueButton\Util\SimpleXMLElementExtended; + +class UrlPresentation extends Presentation +{ + public function __construct(private readonly string $url) + { + } + + public function getArrayKey(): string + { + return $this->url; + } + + public function addDocumentToXML(SimpleXMLElementExtended $module): ?SimpleXMLElementExtended + { + $document = parent::addDocumentToXML($module); + $document->addAttribute('url', $this->url); + + if (isset($this->filename)) { + $document->addAttribute('filename', $this->filename); + } + + return $document; + } +} diff --git a/src/Parameters/CreateMeetingParameters.php b/src/Parameters/CreateMeetingParameters.php index 8e00d7ae..3fff5d7a 100644 --- a/src/Parameters/CreateMeetingParameters.php +++ b/src/Parameters/CreateMeetingParameters.php @@ -22,6 +22,9 @@ namespace BigBlueButton\Parameters; +use BigBlueButton\Core\InlinePresentation; +use BigBlueButton\Core\Presentation; +use BigBlueButton\Core\UrlPresentation; use BigBlueButton\Enum\Feature; use BigBlueButton\Enum\GuestPolicy; use BigBlueButton\Enum\MeetingLayout; @@ -247,9 +250,7 @@ class CreateMeetingParameters extends MetaParameters protected ?bool $allowOverrideClientSettingsOnCreateCall = null; protected ?string $clientSettingsOverride = null; - /** - * @var array - */ + /** @var array */ private array $presentations = []; public function __construct(protected string $meetingID, protected string $name) @@ -351,12 +352,30 @@ public function setGuestPolicyAlwaysAccept(): self return $this; } - public function addPresentation(string $nameOrUrl, ?string $content = null, ?string $filename = null): self + /** + * @return $this + */ + public function addPresentation(string|Presentation $nameOrUrlOrPresentation, ?string $content = null, ?string $filename = null): self { - if (!$filename) { - $this->presentations[$nameOrUrl] = !$content ?: base64_encode($content); + if ($nameOrUrlOrPresentation instanceof Presentation) { + $this->presentations[$nameOrUrlOrPresentation->getArrayKey()] = $nameOrUrlOrPresentation; + + return $this; + } + + @trigger_error(\sprintf('Calling addPresentation in "%s" with any parameters other than a single Presentation object is deprecated and will throw an exception in 7.0.', self::class), \E_USER_DEPRECATED); + + if ($content) { + $presentation = new InlinePresentation($content, $nameOrUrlOrPresentation); + $this->presentations[$presentation->getArrayKey()] = $presentation; } else { - $this->presentations[$nameOrUrl] = $filename; + $presentation = new UrlPresentation($nameOrUrlOrPresentation); + + if ($filename != null) { + $presentation->setFilename($filename); + } + + $this->presentations[$presentation->getArrayKey()] = $presentation; } return $this; @@ -382,7 +401,7 @@ public function addBreakoutRoomsGroup(string $id, ?string $name, array $roster): return $this; } - /** @return array */ + /** @return array */ public function getPresentations(): array { return $this->presentations; @@ -423,18 +442,9 @@ public function addPresentationsModule(SimpleXMLElementExtended $xml): void $module = $xml->addChild('module'); $module->addAttribute('name', 'presentation'); - foreach ($this->presentations as $nameOrUrl => $content) { - if (str_starts_with($nameOrUrl, 'http')) { - $presentation = $module->addChild('document'); - $presentation->addAttribute('url', $nameOrUrl); - if (\is_string($content)) { - $presentation->addAttribute('filename', $content); - } - } else { - $document = $module->addChild('document'); - $document->addAttribute('name', $nameOrUrl); - /* @phpstan-ignore-next-line */ - $document[0] = $content; + foreach ($this->presentations as $data) { + if ($data instanceof Presentation) { + $data->addDocumentToXML($module); } } } diff --git a/src/Parameters/InsertDocumentParameters.php b/src/Parameters/InsertDocumentParameters.php index 538266e6..1d799895 100644 --- a/src/Parameters/InsertDocumentParameters.php +++ b/src/Parameters/InsertDocumentParameters.php @@ -21,26 +21,46 @@ namespace BigBlueButton\Parameters; +use BigBlueButton\Core\Presentation; +use BigBlueButton\Core\UrlPresentation; +use BigBlueButton\Util\SimpleXMLElementExtended; + /** * @method string getMeetingID() * @method $this setMeetingID(string $id) */ final class InsertDocumentParameters extends MetaParameters { - /** @var array */ + /** @var array */ private array $presentations = []; public function __construct(protected string $meetingID) { } - public function addPresentation(string $url, string $filename, ?bool $downloadable = null, ?bool $removable = null): self + public function addPresentation(string|Presentation $urlOrPresentation, ?string $filename = null, ?bool $downloadable = null, ?bool $removable = null): self { - $this->presentations[$url] = [ - 'filename' => $filename, - 'downloadable' => $downloadable, - 'removable' => $removable, - ]; + if ($urlOrPresentation instanceof Presentation) { + $this->presentations[$urlOrPresentation->getArrayKey()] = $urlOrPresentation; + + return $this; + } + + @trigger_error(\sprintf('Calling addPresentation in "%s" with any parameters other than a single Presentation object is deprecated and will throw an exception in 7.0.', self::class), \E_USER_DEPRECATED); + + $presentation = new UrlPresentation($urlOrPresentation); + + if ($filename !== null) { + $presentation->setFilename($filename); + } + if ($downloadable !== null) { + $presentation->setDownloadable($downloadable); + } + if ($removable !== null) { + $presentation->setRemovable($removable); + } + + $this->presentations[$presentation->getArrayKey()] = $presentation; return $this; } @@ -57,21 +77,13 @@ public function getPresentationsAsXML(): string|false $result = ''; if (!empty($this->presentations)) { - $xml = new \SimpleXMLElement(''); + $xml = new SimpleXMLElementExtended(''); $module = $xml->addChild('module'); $module->addAttribute('name', 'presentation'); - foreach ($this->presentations as $url => $content) { - $presentation = $module->addChild('document'); - $presentation->addAttribute('url', $url); - $presentation->addAttribute('filename', $content['filename']); - - if (\is_bool($content['downloadable'])) { - $presentation->addAttribute('downloadable', $content['downloadable'] ? 'true' : 'false'); - } - - if (\is_bool($content['removable'])) { - $presentation->addAttribute('removable', $content['removable'] ? 'true' : 'false'); + foreach ($this->presentations as $content) { + if ($content instanceof Presentation) { + $content->addDocumentToXML($module); } } $result = $xml->asXML();