You are here

class ViewsDelimitedList in Views Delimited List 2.x

Logic for separator, delimiters, and conjunctives.

Hierarchy

Expanded class hierarchy of ViewsDelimitedList

1 file declares its use of ViewsDelimitedList
views_delimited_list.module in ./views_delimited_list.module
Views comma-separated list.

File

src/ViewsDelimitedList.php, line 10

Namespace

Drupal\views_delimited_list
View source
class ViewsDelimitedList {

  /**
   * The view.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $view;

  /**
   * The rows.
   *
   * @var array
   */
  protected $rows;

  /**
   * Count of the rows.
   *
   * @var int
   */
  protected $count;

  /**
   * The separator value.
   *
   * @var string
   */
  protected $separator;

  /**
   * Constructs ViewDelimitedList object.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   Views object.
   * @param array $rows
   *   The rows.
   */
  public function __construct(ViewExecutable $view, array $rows) {
    $this->view = $view;
    $this->rows = $rows;
    $this->count = count($rows);
    $this->separator = $this
      ->getSeparator();
  }

  /**
   * Gets the separator value.
   *
   * @return string
   *   The separator value.
   */
  protected function getSeparator() {
    $options = $this->view
      ->getStyle()->options;
    if ($this->count === 2 && $this->count != $options['long_count']) {
      $separator = $options['separator_two'];
    }
    else {
      $separator = $options['separator_long'];
    }
    return $separator;
  }

  /**
   * Check if this needs delimiter.
   *
   * @param int $row_index
   *   The index of the row.
   *
   * @return bool[]
   *   The has delimiter value.
   */
  protected function hasDelimiter($row_index) {
    $current_row_index = $row_index + 1;
    $is_second_last_item = $current_row_index == $this->count - 1;
    $last_delimiter = in_array($this->separator, [
      'both',
      'delimiter',
    ], TRUE);
    if ($current_row_index < $this->count - 1 || $is_second_last_item && $last_delimiter) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Gets the delimiters.
   *
   * @return bool[]
   *   The delimiter setting for the rows.
   */
  public function getDelimiters() {
    $delimiters = [];
    foreach ($this->rows as $row_index => $row) {
      $delimiters[$row_index] = $this
        ->hasDelimiter($row_index);
    }
    return $delimiters;
  }

  /**
   * Gets the conjunctives.
   *
   * @return bool[]
   *   The conjunctive setting for the rows.
   */
  public function getConjunctives() {
    $last_conjunctive = in_array($this->separator, [
      'both',
      'conjunctive',
    ], TRUE);
    $conjunctives = [];
    foreach ($this->rows as $row_index => $row) {
      $current_row_index = $row_index + 1;
      $is_second_last_item = $current_row_index == $this->count - 1;
      $has_conjunctive = FALSE;
      if ($is_second_last_item && $last_conjunctive) {
        $has_conjunctive = TRUE;
      }
      $conjunctives[$row_index] = $has_conjunctive;
    }
    return $conjunctives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewsDelimitedList::$count protected property Count of the rows.
ViewsDelimitedList::$rows protected property The rows.
ViewsDelimitedList::$separator protected property The separator value.
ViewsDelimitedList::$view protected property The view.
ViewsDelimitedList::getConjunctives public function Gets the conjunctives.
ViewsDelimitedList::getDelimiters public function Gets the delimiters.
ViewsDelimitedList::getSeparator protected function Gets the separator value.
ViewsDelimitedList::hasDelimiter protected function Check if this needs delimiter.
ViewsDelimitedList::__construct public function Constructs ViewDelimitedList object.