"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Common/Page/Medium/AbstractMedia.php" (8 Jun 2020, 5841 Bytes) of package /linux/www/grav-v1.6.26.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractMedia.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /**
    4  * @package    Grav\Common\Page
    5  *
    6  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    7  * @license    MIT License; see LICENSE file for details.
    8  */
    9 
   10 namespace Grav\Common\Page\Medium;
   11 
   12 use Grav\Common\Grav;
   13 use Grav\Common\Media\Interfaces\MediaCollectionInterface;
   14 use Grav\Common\Media\Interfaces\MediaObjectInterface;
   15 use Grav\Common\Page\Page;
   16 use Grav\Common\Utils;
   17 use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
   18 use RocketTheme\Toolbox\ArrayTraits\Countable;
   19 use RocketTheme\Toolbox\ArrayTraits\Export;
   20 use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
   21 use RocketTheme\Toolbox\ArrayTraits\Iterator;
   22 
   23 abstract class AbstractMedia implements ExportInterface, MediaCollectionInterface
   24 {
   25     use ArrayAccess;
   26     use Countable;
   27     use Iterator;
   28     use Export;
   29 
   30     protected $items = [];
   31     protected $path;
   32     protected $images = [];
   33     protected $videos = [];
   34     protected $audios = [];
   35     protected $files = [];
   36     protected $media_order;
   37 
   38     /**
   39      * Return media path.
   40      *
   41      * @return string
   42      */
   43     public function getPath()
   44     {
   45         return $this->path;
   46     }
   47 
   48     public function setPath(?string $path)
   49     {
   50         $this->path = $path;
   51     }
   52 
   53     /**
   54      * Get medium by filename.
   55      *
   56      * @param string $filename
   57      * @return Medium|null
   58      */
   59     public function get($filename)
   60     {
   61         return $this->offsetGet($filename);
   62     }
   63 
   64     /**
   65      * Call object as function to get medium by filename.
   66      *
   67      * @param string $filename
   68      * @return mixed
   69      */
   70     public function __invoke($filename)
   71     {
   72         return $this->offsetGet($filename);
   73     }
   74 
   75     /**
   76      * Set file modification timestamps (query params) for all the media files.
   77      *
   78      * @param string|int|null $timestamp
   79      * @return $this
   80      */
   81     public function setTimestamps($timestamp = null)
   82     {
   83         /** @var Medium $instance */
   84         foreach ($this->items as $instance) {
   85             $instance->setTimestamp($timestamp);
   86         }
   87 
   88         return $this;
   89     }
   90 
   91     /**
   92      * Get a list of all media.
   93      *
   94      * @return MediaObjectInterface[]
   95      */
   96     public function all()
   97     {
   98         $this->items = $this->orderMedia($this->items);
   99 
  100         return $this->items;
  101     }
  102 
  103     /**
  104      * Get a list of all image media.
  105      *
  106      * @return MediaObjectInterface[]
  107      */
  108     public function images()
  109     {
  110         $this->images = $this->orderMedia($this->images);
  111 
  112         return $this->images;
  113     }
  114 
  115     /**
  116      * Get a list of all video media.
  117      *
  118      * @return MediaObjectInterface[]
  119      */
  120     public function videos()
  121     {
  122         $this->videos = $this->orderMedia($this->videos);
  123 
  124         return $this->videos;
  125     }
  126 
  127     /**
  128      * Get a list of all audio media.
  129      *
  130      * @return MediaObjectInterface[]
  131      */
  132     public function audios()
  133     {
  134         $this->audios = $this->orderMedia($this->audios);
  135 
  136         return $this->audios;
  137     }
  138 
  139     /**
  140      * Get a list of all file media.
  141      *
  142      * @return MediaObjectInterface[]
  143      */
  144     public function files()
  145     {
  146         $this->files = $this->orderMedia($this->files);
  147 
  148         return $this->files;
  149     }
  150 
  151     /**
  152      * @param string $name
  153      * @param MediaObjectInterface $file
  154      */
  155     public function add($name, $file)
  156     {
  157         if (!$file) {
  158             return;
  159         }
  160         $this->offsetSet($name, $file);
  161         switch ($file->type) {
  162             case 'image':
  163                 $this->images[$name] = $file;
  164                 break;
  165             case 'video':
  166                 $this->videos[$name] = $file;
  167                 break;
  168             case 'audio':
  169                 $this->audios[$name] = $file;
  170                 break;
  171             default:
  172                 $this->files[$name] = $file;
  173         }
  174     }
  175 
  176     /**
  177      * Order the media based on the page's media_order
  178      *
  179      * @param array $media
  180      * @return array
  181      */
  182     protected function orderMedia($media)
  183     {
  184         if (null === $this->media_order) {
  185             /** @var Page $page */
  186             $page = Grav::instance()['pages']->get($this->getPath());
  187 
  188             if ($page && isset($page->header()->media_order)) {
  189                 $this->media_order = array_map('trim', explode(',', $page->header()->media_order));
  190             }
  191         }
  192 
  193         if (!empty($this->media_order) && is_array($this->media_order)) {
  194             $media = Utils::sortArrayByArray($media, $this->media_order);
  195         } else {
  196             ksort($media, SORT_NATURAL | SORT_FLAG_CASE);
  197         }
  198 
  199         return $media;
  200     }
  201 
  202     /**
  203      * Get filename, extension and meta part.
  204      *
  205      * @param  string $filename
  206      * @return array
  207      */
  208     protected function getFileParts($filename)
  209     {
  210         if (preg_match('/(.*)@(\d+)x\.(.*)$/', $filename, $matches)) {
  211             $name = $matches[1];
  212             $extension = $matches[3];
  213             $extra = (int) $matches[2];
  214             $type = 'alternative';
  215 
  216             if ($extra === 1) {
  217                 $type = 'base';
  218                 $extra = null;
  219             }
  220         } else {
  221             $fileParts = explode('.', $filename);
  222 
  223             $name = array_shift($fileParts);
  224             $extension = null;
  225             $extra = null;
  226             $type = 'base';
  227 
  228             while (($part = array_shift($fileParts)) !== null) {
  229                 if ($part !== 'meta' && $part !== 'thumb') {
  230                     if (null !== $extension) {
  231                         $name .= '.' . $extension;
  232                     }
  233                     $extension = $part;
  234                 } else {
  235                     $type = $part;
  236                     $extra = '.' . $part . '.' . implode('.', $fileParts);
  237                     break;
  238                 }
  239             }
  240         }
  241 
  242         return array($name, $extension, $type, $extra);
  243     }
  244 }