vendor/coreshop/resource/Metadata/Metadata.php line 110

Open in your IDE?
  1. <?php
  2. /**
  3.  * CoreShop.
  4.  *
  5.  * This source file is subject to the GNU General Public License version 3 (GPLv3)
  6.  * For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
  7.  * files that are distributed with this source code.
  8.  *
  9.  * @copyright  Copyright (c) 2015-2020 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
  10.  * @license    https://www.coreshop.org/license     GNU General Public License version 3 (GPLv3)
  11.  */
  12. namespace CoreShop\Component\Resource\Metadata;
  13. use Doctrine\Common\Inflector\Inflector;
  14. final class Metadata implements MetadataInterface
  15. {
  16.     /**
  17.      * @var string
  18.      */
  19.     private $name;
  20.     /**
  21.      * @var string
  22.      */
  23.     private $applicationName;
  24.     /**
  25.      * @var string
  26.      */
  27.     private $driver;
  28.     /**
  29.      * @var string
  30.      */
  31.     private $templatesNamespace;
  32.     /**
  33.      * @var array
  34.      */
  35.     private $parameters;
  36.     /**
  37.      * @param string $name
  38.      * @param string $applicationName
  39.      * @param array  $parameters
  40.      */
  41.     private function __construct($name$applicationName, array $parameters)
  42.     {
  43.         $this->name $name;
  44.         $this->applicationName $applicationName;
  45.         $this->driver $parameters['driver'];
  46.         $this->templatesNamespace array_key_exists('templates'$parameters) ? $parameters['templates'] : null;
  47.         $this->parameters $parameters;
  48.     }
  49.     /**
  50.      * @param string $alias
  51.      * @param array  $parameters
  52.      *
  53.      * @return self
  54.      */
  55.     public static function fromAliasAndConfiguration($alias, array $parameters)
  56.     {
  57.         list($applicationName$name) = self::parseAlias($alias);
  58.         return new self($name$applicationName$parameters);
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getAlias()
  64.     {
  65.         return $this->applicationName '.' $this->name;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getApplicationName()
  71.     {
  72.         return $this->applicationName;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function getName()
  78.     {
  79.         return $this->name;
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function getHumanizedName()
  85.     {
  86.         return trim(strtolower(preg_replace(['/([A-Z])/''/[_\s]+/'], ['_$1'' '], $this->name)));
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function getPluralName()
  92.     {
  93.         return Inflector::pluralize($this->name);
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function getDriver()
  99.     {
  100.         return $this->driver;
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function getTemplatesNamespace()
  106.     {
  107.         return $this->templatesNamespace;
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function getParameter($name)
  113.     {
  114.         if (!$this->hasParameter($name)) {
  115.             throw new \InvalidArgumentException(sprintf('Parameter "%s" is not configured for resource "%s".'$name$this->getAlias()));
  116.         }
  117.         return $this->parameters[$name];
  118.     }
  119.     /**
  120.      * {@inheritdoc}
  121.      */
  122.     public function hasParameter($name)
  123.     {
  124.         return array_key_exists($name$this->parameters);
  125.     }
  126.     /**
  127.      * {@inheritdoc}
  128.      */
  129.     public function getParameters()
  130.     {
  131.         return $this->parameters;
  132.     }
  133.     /**
  134.      * {@inheritdoc}
  135.      */
  136.     public function getClass($name)
  137.     {
  138.         if (!$this->hasClass($name)) {
  139.             throw new \InvalidArgumentException(sprintf('Class "%s" is not configured for resource "%s".'$name$this->getAlias()));
  140.         }
  141.         return $this->parameters['classes'][$name];
  142.     }
  143.     /**
  144.      * {@inheritdoc}
  145.      */
  146.     public function hasClass($name)
  147.     {
  148.         return isset($this->parameters['classes'][$name]);
  149.     }
  150.     /**
  151.      * {@inheritdoc}
  152.      */
  153.     public function getServiceId($serviceName)
  154.     {
  155.         return sprintf('%s.%s.%s'$this->applicationName$serviceName$this->name);
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function getPermissionCode($permissionName)
  161.     {
  162.         return sprintf('%s.%s.%s'$this->applicationName$this->name$permissionName);
  163.     }
  164.     /**
  165.      * @param string $alias
  166.      *
  167.      * @return array
  168.      */
  169.     private static function parseAlias($alias)
  170.     {
  171.         if (false === strpos($alias'.')) {
  172.             throw new \InvalidArgumentException('Invalid alias supplied, it should conform to the following format "<applicationName>.<name>".');
  173.         }
  174.         return explode('.'$alias);
  175.     }
  176. }