You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
class SqlResultProvider { /** * @var array */ private $result_items;
private $result_items_base;
/** * @param $result_items array */ public function __construct($result_items) { $this->result_items = $result_items; }
public function assoc($field, $assoc_as_array = false, $field_assoc_in_array = null) { if (is_null($this->result_items_base)) { $this->result_items_base = $this->result_items; } $result_items_assoc = array(); foreach ($this->result_items_base as $item) { if (!isset($item->{$field})) { throw new ErrorException('Undefined field. ' . $field); } if ($assoc_as_array) { if (!isset($result_items_assoc[$item->{$field}])) { $result_items_assoc[$item->{$field}] = array(); } $result_items_assoc[$item->{$field}][] = $item; } else { if (isset($result_items_assoc[$item->{$field}])) { throw new ErrorException('Field not unique. May be use assoc_as_array. ' . $field); } $result_items_assoc[$item->{$field}] = $item; } } // Ассоциирование внутри каждого элемента массива
// $field_assoc_in_array - ассоциирование произовдится по этому полю (вернет ошибку, если этого поля нет в результатх выборки)
if ($assoc_as_array && $field_assoc_in_array) { foreach ($result_items_assoc as &$value) { $sql_result_provider = new SqlResultProvider($value); $value = $sql_result_provider->assoc($field_assoc_in_array)->fetchAll(); } } $this->result_items = $result_items_assoc; return $this; }
public function getKeys() { return array_keys($this->result_items); }
public function fetchAll() { return $this->result_items; }
public function fetchKey($key) { return $this->result_items[$key]; }
public function fetch() { return current($this->result_items); } }
|