"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Framework/File/Formatter/AbstractFormatter.php" (8 Jun 2020, 2688 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 "AbstractFormatter.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /**
    6  * @package    Grav\Framework\File\Formatter
    7  *
    8  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    9  * @license    MIT License; see LICENSE file for details.
   10  */
   11 
   12 namespace Grav\Framework\File\Formatter;
   13 
   14 use Grav\Framework\File\Interfaces\FileFormatterInterface;
   15 
   16 /**
   17  * Abstract file formatter.
   18  *
   19  * @package Grav\Framework\File\Formatter
   20  */
   21 abstract class AbstractFormatter implements FileFormatterInterface
   22 {
   23     /** @var array */
   24     private $config;
   25 
   26     /**
   27      * IniFormatter constructor.
   28      * @param array $config
   29      */
   30     public function __construct(array $config = [])
   31     {
   32         $this->config = $config;
   33     }
   34 
   35     /**
   36      * @return string
   37      */
   38     public function serialize(): string
   39     {
   40         return serialize($this->doSerialize());
   41     }
   42 
   43     /**
   44      * @param string $serialized
   45      */
   46     public function unserialize($serialized): void
   47     {
   48         $this->doUnserialize(unserialize($serialized, ['allowed_classes' => false]));
   49     }
   50 
   51     /**
   52      * {@inheritdoc}
   53      * @see FileFormatterInterface::getDefaultFileExtension()
   54      */
   55     public function getDefaultFileExtension(): string
   56     {
   57         $extensions = $this->getSupportedFileExtensions();
   58 
   59         // Call fails on bad configuration.
   60         return reset($extensions);
   61     }
   62 
   63     /**
   64      * {@inheritdoc}
   65      * @see FileFormatterInterface::getSupportedFileExtensions()
   66      */
   67     public function getSupportedFileExtensions(): array
   68     {
   69         $extensions = $this->getConfig('file_extension');
   70 
   71         // Call fails on bad configuration.
   72         return \is_string($extensions) ? [$extensions] : $extensions;
   73     }
   74 
   75     /**
   76      * {@inheritdoc}
   77      * @see FileFormatterInterface::encode()
   78      */
   79     abstract public function encode($data): string;
   80 
   81     /**
   82      * {@inheritdoc}
   83      * @see FileFormatterInterface::decode()
   84      */
   85     abstract public function decode($data);
   86 
   87     /**
   88      * Get either full configuration or a single option.
   89      *
   90      * @param string|null $name Configuration option (optional)
   91      * @return mixed
   92      */
   93     protected function getConfig(string $name = null)
   94     {
   95         if (null !== $name) {
   96             return $this->config[$name] ?? null;
   97         }
   98 
   99         return $this->config;
  100     }
  101 
  102     /**
  103      * @return array
  104      */
  105     protected function doSerialize(): array
  106     {
  107         return ['config' => $this->config];
  108     }
  109 
  110     /**
  111      * Note: if overridden, make sure you call parent::doUnserialize()
  112      *
  113      * @param array $serialized
  114      */
  115     protected function doUnserialize(array $serialized): void
  116     {
  117         $this->config = $serialized['config'];
  118     }
  119 }