You are here

class QPList in QueryPath 6

Same name and namespace in other branches
  1. 7.3 QueryPath/Extension/QPList.php \QPList
  2. 7.2 QueryPath/Extension/QPList.php \QPList

Hierarchy

Expanded class hierarchy of QPList

1 string reference to 'QPList'
QPList.php in QueryPath/Extension/QPList.php

File

QueryPath/Extension/QPList.php, line 5

View source
class QPList implements QueryPathExtension {
  const UL = 'ul';
  const OL = 'ol';
  const DL = 'dl';
  protected $qp = NULL;
  public function __construct(QueryPath $qp) {
    $this->qp = $qp;
  }
  public function appendTable($items, $options = array()) {
    $opts = $options + array(
      'table class' => 'qptable',
    );
    $base = '<?xml version="1.0"?>
    <table>
    <tbody>
      <tr></tr>
    </tbody>
    </table>';
    $qp = qp($base, 'table')
      ->addClass($opts['table class'])
      ->find('tr');
    if ($items instanceof TableAble) {
      $headers = $items
        ->getHeaders();
      $rows = $items
        ->getRows();
    }
    elseif ($items instanceof Traversable) {
      $headers = array();
      $rows = $items;
    }
    else {
      $headers = $items['headers'];
      $rows = $items['rows'];
    }
    foreach ($headers as $header) {
      $qp
        ->append('<th>' . $header . '</th>');
    }
    $qp
      ->top()
      ->find('tr:last');
    foreach ($rows as $row) {
      $qp
        ->after('<tr/>')
        ->next();
      foreach ($row as $cell) {
        $qp
          ->append('<td>' . $cell . '</td>');
      }
    }
    $this->qp
      ->append($qp
      ->top());
    return $this->qp;
  }
  public function appendList($items, $type = self::UL, $options = array()) {
    $opts = $options + array(
      'list class' => 'qplist',
    );
    if ($type == self::DL) {
      $q = qp('<?xml version="1.0"?><dl></dl>', 'dl')
        ->addClass($opts['list class']);
      foreach ($items as $dt => $dd) {
        $q
          ->append('<dt>' . $dt . '</dt><dd>' . $dd . '</dd>');
      }
      $q
        ->appendTo($this->qp);
    }
    else {
      $q = $this
        ->listImpl($items, $type, $opts);
      $this->qp
        ->append($q
        ->find(':root'));
    }
    return $this->qp;
  }
  protected function listImpl($items, $type, $opts, $q = NULL) {
    $ele = '<' . $type . '/>';
    if (!isset($q)) {
      $q = qp()
        ->append($ele)
        ->addClass($opts['list class']);
    }
    foreach ($items as $li) {
      if ($li instanceof QueryPath) {
        $q = $this
          ->listImpl($li
          ->get(), $type, $opts, $q);
      }
      elseif (is_array($li) || $li instanceof Traversable) {
        $q
          ->append('<li><ul/></li>')
          ->find('li:last > ul');
        $q = $this
          ->listImpl($li, $type, $opts, $q);
        $q
          ->parent();
      }
      else {
        $q
          ->append('<li>' . $li . '</li>');
      }
    }
    return $q;
  }
  protected function isAssoc($array) {
    return count(array_diff_key($array, range(0, count($array) - 1))) != 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QPList::$qp protected property
QPList::appendList public function
QPList::appendTable public function
QPList::DL constant
QPList::isAssoc protected function
QPList::listImpl protected function
QPList::OL constant
QPList::UL constant
QPList::__construct public function Overrides QueryPathExtension::__construct