You are here

class csl_label in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/CiteProc/CSL.inc \csl_label
  2. 7.2 modules/CiteProc/CSL.inc \csl_label

Hierarchy

Expanded class hierarchy of csl_label

1 string reference to 'csl_label'
csl_names::render in modules/CiteProc/CSL.inc

File

modules/CiteProc/CSL.inc, line 1536
CiteProc-PHP.

View source
class csl_label extends csl_format {
  private $pluralization;

  /**
   *
   */
  public function render($data, $mode = NULL) {
    $text = '';
    $pluralization = NULL;
    $form = ($form = $this->form) ? $form : 'long';

    // There are two possible locations for a label the first is within a names element which is a bit simpler than the general label case
    // within csl_names element case.
    if (isset($this->in_names)) {
      if ($term = $this->citeproc
        ->get_locale('term', $this->variable, $form, $this->plural)) {
        $text = $term;
      }
    }
    else {
      $variables = explode(' ', $this->variable);
      switch ($this->plural) {
        case 'never':
          $pluralization = 'single';
          break;
        case 'always':
          $pluralization = 'multiple';
          break;
        case 'contextual':
        default:
      }
      foreach ($variables as $variable) {
        $field = $this->citeproc
          ->map_field($variable);
        if (isset($data->{$field}) && !empty($data->{$field})) {
          if (!isset($this->plural) && empty($pluralization) && is_array($data->{$field})) {
            $count = count($data->{$field});
            if ($count == 1) {
              $pluralization = 'single';
            }
            elseif ($count > 1) {
              $pluralization = 'multiple';
            }
          }
          else {
            $pluralization = $this
              ->evaluateStringPluralism($data, $variable);
          }
          if ($term = $this->citeproc
            ->get_locale('term', $variable, $form, $pluralization)) {
            $text = $term;
            break;
          }
        }
      }
    }
    if (empty($text)) {
      return;
    }
    if ($this->{'strip-periods'}) {
      $text = str_replace('.', '', $text);
    }
    return $this
      ->format($text);
  }

  /**
   *
   */
  public function evaluateStringPluralism($data, $variable) {
    $field = $this->citeproc
      ->map_field($variable);
    $str = $data->{$field};
    $plural = 'single';
    if (!empty($str)) {

      // $regex = '/(?:[0-9],\s*[0-9]|\s+and\s+|&|([0-9]+)\s*[\-\x2013]\s*([0-9]+))/';.
      switch ($variable) {
        case 'page':
          $page_regex = "/([a-zA-Z]*)([0-9]+)\\s*(?:–|-)\\s*([a-zA-Z]*)([0-9]+)/";
          $err = preg_match($page_regex, $str, $m);
          if ($err !== FALSE && count($m) == 0) {
            $plural = 'single';
          }
          elseif ($err !== FALSE && count($m)) {
            $plural = 'multiple';
          }
          break;
        default:
      }
    }
    return $plural;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
csl_collection::$elements protected property
csl_collection::add_element public function
csl_element::$attributes protected property
csl_element::$citeproc protected property
csl_element::get_attributes public function
csl_element::get_hier_attributes public function
csl_element::init public function 5
csl_element::name public function
csl_element::set_attributes public function
csl_element::__get public function
csl_element::__isset public function
csl_element::__set public function
csl_element::__unset public function
csl_format::$format protected property 1
csl_format::$no_op protected property
csl_format::format public function Overrides csl_collection::format 1
csl_format::init_formatting public function 5
csl_format::__construct public function Overrides csl_element::__construct 2
csl_label::$pluralization private property
csl_label::evaluateStringPluralism public function
csl_label::render public function Overrides csl_rendering_element::render