ViewsDelimitedList.php in Views Delimited List 2.x
File
src/ViewsDelimitedList.php
View source
<?php
namespace Drupal\views_delimited_list;
use Drupal\views\ViewExecutable;
class ViewsDelimitedList {
protected $view;
protected $rows;
protected $count;
protected $separator;
public function __construct(ViewExecutable $view, array $rows) {
$this->view = $view;
$this->rows = $rows;
$this->count = count($rows);
$this->separator = $this
->getSeparator();
}
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;
}
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;
}
public function getDelimiters() {
$delimiters = [];
foreach ($this->rows as $row_index => $row) {
$delimiters[$row_index] = $this
->hasDelimiter($row_index);
}
return $delimiters;
}
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;
}
}