[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Categories/ -> CategoryFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Categories;
  11  
  12  use Joomla\Database\DatabaseAwareInterface;
  13  use Joomla\Database\DatabaseAwareTrait;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('_JEXEC') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Option based categories factory.
  21   *
  22   * @since  3.10.0
  23   */
  24  class CategoryFactory implements CategoryFactoryInterface
  25  {
  26      use DatabaseAwareTrait;
  27  
  28      /**
  29       * The namespace to create the categories from.
  30       *
  31       * @var    string
  32       * @since  4.0.0
  33       */
  34      private $namespace;
  35  
  36      /**
  37       * The namespace must be like:
  38       * Joomla\Component\Content
  39       *
  40       * @param   string  $namespace  The namespace
  41       *
  42       * @since   4.0.0
  43       */
  44      public function __construct($namespace)
  45      {
  46          $this->namespace = $namespace;
  47      }
  48  
  49      /**
  50       * Creates a category.
  51       *
  52       * @param   array   $options  The options
  53       * @param   string  $section  The section
  54       *
  55       * @return  CategoryInterface
  56       *
  57       * @since   3.10.0
  58       *
  59       * @throws  SectionNotFoundException
  60       */
  61      public function createCategory(array $options = [], string $section = ''): CategoryInterface
  62      {
  63          $className = trim($this->namespace, '\\') . '\\Site\\Service\\' . ucfirst($section) . 'Category';
  64  
  65          if (!class_exists($className)) {
  66              throw new SectionNotFoundException();
  67          }
  68  
  69          $category = new $className($options);
  70  
  71          if ($category instanceof DatabaseAwareInterface) {
  72              $category->setDatabase($this->getDatabase());
  73          }
  74  
  75          return $category;
  76      }
  77  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer