loadFromString($xml, false); } catch (XmlStorageException $e) { throw new TaoMediaException('Shared stimulus XML cannot be processed.', 0, $e); } return array_merge( $this->processImages($xmlDocument, $processImageIdentifier), $this->processVideos($xmlDocument, $processImageIdentifier) ); } public function withMediaResolver(TaoMediaResolver $resolver) { $this->mediaResolver = $resolver; return $this; } private function processImages(XmlDocument $xmlDocument, callable $processImageIdentifier): array { $mediaImages = []; $images = $xmlDocument->getDocumentComponent()->getComponentsByClassName('img'); foreach ($images as $image) { $this->processMediaSource($image->getSrc(), $processImageIdentifier, $mediaImages); } return $mediaImages; } private function processVideos(XmlDocument $xmlDocument, callable $processImageIdentifier): array { $mediaVideos = []; $videos = $xmlDocument->getDocumentComponent()->getComponentsByClassName('object'); foreach ($videos as $video) { $this->processMediaSource($video->getData(), $processImageIdentifier, $mediaVideos); } return $mediaVideos; } private function processMediaSource(string $uri, callable $processor, array &$matches): void { if (!Base64::isEncoded($uri)) { if (isset(parse_url($uri)['scheme'])) { $asset = $this->getMediaResolver()->resolve($uri); if ($asset->getMediaSource() instanceof MediaSource) { $matches[] = $processor($asset); } } } } private function getMediaResolver(): TaoMediaResolver { if (!$this->mediaResolver) { $this->mediaResolver = new TaoMediaResolver(); } return $this->mediaResolver; } }