123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * @package Grav\Common\Page
- *
- * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Common\Page\Medium;
- use Grav\Common\Grav;
- use Grav\Common\Media\Interfaces\MediaCollectionInterface;
- use Grav\Common\Media\Interfaces\MediaObjectInterface;
- use Grav\Common\Page\Page;
- use Grav\Common\Utils;
- use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
- use RocketTheme\Toolbox\ArrayTraits\Countable;
- use RocketTheme\Toolbox\ArrayTraits\Export;
- use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
- use RocketTheme\Toolbox\ArrayTraits\Iterator;
- abstract class AbstractMedia implements ExportInterface, MediaCollectionInterface
- {
- use ArrayAccess;
- use Countable;
- use Iterator;
- use Export;
- protected $items = [];
- protected $path;
- protected $images = [];
- protected $videos = [];
- protected $audios = [];
- protected $files = [];
- protected $media_order;
- /**
- * Return media path.
- *
- * @return string
- */
- public function getPath()
- {
- return $this->path;
- }
- public function setPath(?string $path)
- {
- $this->path = $path;
- }
- /**
- * Get medium by filename.
- *
- * @param string $filename
- * @return Medium|null
- */
- public function get($filename)
- {
- return $this->offsetGet($filename);
- }
- /**
- * Call object as function to get medium by filename.
- *
- * @param string $filename
- * @return mixed
- */
- public function __invoke($filename)
- {
- return $this->offsetGet($filename);
- }
- /**
- * Set file modification timestamps (query params) for all the media files.
- *
- * @param string|int|null $timestamp
- * @return $this
- */
- public function setTimestamps($timestamp = null)
- {
- /** @var Medium $instance */
- foreach ($this->items as $instance) {
- $instance->setTimestamp($timestamp);
- }
- return $this;
- }
- /**
- * Get a list of all media.
- *
- * @return MediaObjectInterface[]
- */
- public function all()
- {
- $this->items = $this->orderMedia($this->items);
- return $this->items;
- }
- /**
- * Get a list of all image media.
- *
- * @return MediaObjectInterface[]
- */
- public function images()
- {
- $this->images = $this->orderMedia($this->images);
- return $this->images;
- }
- /**
- * Get a list of all video media.
- *
- * @return MediaObjectInterface[]
- */
- public function videos()
- {
- $this->videos = $this->orderMedia($this->videos);
- return $this->videos;
- }
- /**
- * Get a list of all audio media.
- *
- * @return MediaObjectInterface[]
- */
- public function audios()
- {
- $this->audios = $this->orderMedia($this->audios);
- return $this->audios;
- }
- /**
- * Get a list of all file media.
- *
- * @return MediaObjectInterface[]
- */
- public function files()
- {
- $this->files = $this->orderMedia($this->files);
- return $this->files;
- }
- /**
- * @param string $name
- * @param MediaObjectInterface $file
- */
- public function add($name, $file)
- {
- if (!$file) {
- return;
- }
- $this->offsetSet($name, $file);
- switch ($file->type) {
- case 'image':
- $this->images[$name] = $file;
- break;
- case 'video':
- $this->videos[$name] = $file;
- break;
- case 'audio':
- $this->audios[$name] = $file;
- break;
- default:
- $this->files[$name] = $file;
- }
- }
- /**
- * Order the media based on the page's media_order
- *
- * @param array $media
- * @return array
- */
- protected function orderMedia($media)
- {
- if (null === $this->media_order) {
- /** @var Page $page */
- $page = Grav::instance()['pages']->get($this->getPath());
- if ($page && isset($page->header()->media_order)) {
- $this->media_order = array_map('trim', explode(',', $page->header()->media_order));
- }
- }
- if (!empty($this->media_order) && is_array($this->media_order)) {
- $media = Utils::sortArrayByArray($media, $this->media_order);
- } else {
- ksort($media, SORT_NATURAL | SORT_FLAG_CASE);
- }
- return $media;
- }
- /**
- * Get filename, extension and meta part.
- *
- * @param string $filename
- * @return array
- */
- protected function getFileParts($filename)
- {
- if (preg_match('/(.*)@(\d+)x\.(.*)$/', $filename, $matches)) {
- $name = $matches[1];
- $extension = $matches[3];
- $extra = (int) $matches[2];
- $type = 'alternative';
- if ($extra === 1) {
- $type = 'base';
- $extra = null;
- }
- } else {
- $fileParts = explode('.', $filename);
- $name = array_shift($fileParts);
- $extension = null;
- $extra = null;
- $type = 'base';
- while (($part = array_shift($fileParts)) !== null) {
- if ($part !== 'meta' && $part !== 'thumb') {
- if (null !== $extension) {
- $name .= '.' . $extension;
- }
- $extension = $part;
- } else {
- $type = $part;
- $extra = '.' . $part . '.' . implode('.', $fileParts);
- break;
- }
- }
- }
- return array($name, $extension, $type, $extra);
- }
- }
|