You are here

class CitationStyler in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 src/CitationStyler.php \Drupal\bibcite\CitationStyler

Render CSL data to bibliographic citation.

Hierarchy

Expanded class hierarchy of CitationStyler

1 string reference to 'CitationStyler'
bibcite.services.yml in ./bibcite.services.yml
bibcite.services.yml
1 service uses CitationStyler
bibcite.citation_styler in ./bibcite.services.yml
Drupal\bibcite\CitationStyler

File

src/CitationStyler.php, line 15

Namespace

Drupal\bibcite
View source
class CitationStyler implements CitationStylerInterface {

  /**
   * Processor plugin.
   *
   * @var \Drupal\bibcite\Plugin\BibCiteProcessorInterface
   */
  protected $processor;

  /**
   * Manager of processor plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * Service configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $configuration;

  /**
   * Language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Storage of CSL style entity.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $cslStorage;

  /**
   * CSL style entity.
   *
   * @var \Drupal\bibcite\Entity\CslStyleInterface
   */
  protected $style;

  /**
   * Language code.
   *
   * @var string
   */
  protected $langCode;

  /**
   * Styler constructor.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   Manager of processor plugins.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   Language manager service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   */
  public function __construct(PluginManagerInterface $plugin_manager, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager) {
    $this->pluginManager = $plugin_manager;
    $this->configuration = $config_factory
      ->get('bibcite.settings');
    $this->languageManager = $language_manager;
    $this->cslStorage = $entity_type_manager
      ->getStorage('bibcite_csl_style');
  }

  /**
   * {@inheritdoc}
   */
  public function render($data) {
    $csl = $this
      ->getStyle()
      ->getCslText();
    $lang = $this
      ->getLanguageCode();
    return $this
      ->getProcessor()
      ->render($data, $csl, $lang);
  }

  /**
   * {@inheritdoc}
   */
  public function setProcessor(BibCiteProcessorInterface $processor) {
    $this->processor = $processor;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setProcessorById($processor_id) {
    $this->processor = $this->pluginManager
      ->createInstance($processor_id);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getProcessor() {
    if (!$this->processor) {
      $this
        ->setProcessorById($this->configuration
        ->get('processor'));
    }
    return $this->processor;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableProcessors() {
    return $this->pluginManager
      ->getDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableStyles() {
    return $this->cslStorage
      ->loadMultiple();
  }

  /**
   * {@inheritdoc}
   */
  public function getStyle() {
    if (!$this->style) {
      $this
        ->setStyleById($this->configuration
        ->get('default_style'));
    }
    return $this->style;
  }

  /**
   * {@inheritdoc}
   */
  public function setStyle($csl_style) {
    $this->style = $csl_style;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setStyleById($style_id) {
    $this->style = $this->cslStorage
      ->load($style_id);
    if (!$this->style) {
      throw new \UnexpectedValueException("CSL style '{$style_id}' does not exist.");
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getLanguageCode() {
    if (!$this->langCode) {
      $this->langCode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
    }
    return $this->langCode;
  }

  /**
   * {@inheritdoc}
   */
  public function setLanguageCode($lang_code) {

    // @todo M? $this->langCode maybe?
    $this->lang = $lang_code;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CitationStyler::$configuration protected property Service configuration.
CitationStyler::$cslStorage protected property Storage of CSL style entity.
CitationStyler::$langCode protected property Language code.
CitationStyler::$languageManager protected property Language manager service.
CitationStyler::$pluginManager protected property Manager of processor plugins.
CitationStyler::$processor protected property Processor plugin.
CitationStyler::$style protected property CSL style entity.
CitationStyler::getAvailableProcessors public function Get all available processors plugins. Overrides CitationStylerInterface::getAvailableProcessors
CitationStyler::getAvailableStyles public function Get list of available bibliographic styles. Overrides CitationStylerInterface::getAvailableStyles
CitationStyler::getLanguageCode public function Get current used language code. Overrides CitationStylerInterface::getLanguageCode
CitationStyler::getProcessor public function Get current processor plugin. Overrides CitationStylerInterface::getProcessor
CitationStyler::getStyle public function Get current CSL style. Overrides CitationStylerInterface::getStyle
CitationStyler::render public function Render CSL data to bibliographic citation. Overrides CitationStylerInterface::render
CitationStyler::setLanguageCode public function Set language code. Overrides CitationStylerInterface::setLanguageCode
CitationStyler::setProcessor public function Set processor plugin. Overrides CitationStylerInterface::setProcessor
CitationStyler::setProcessorById public function Load plugin object by identifier. Overrides CitationStylerInterface::setProcessorById
CitationStyler::setStyle public function Set CSL style. Overrides CitationStylerInterface::setStyle
CitationStyler::setStyleById public function Load and set style by identifier. Overrides CitationStylerInterface::setStyleById
CitationStyler::__construct public function Styler constructor.