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.

60 lines
1.2 KiB

11 years ago
  1. <?php namespace Majestic\Model;
  2. use Illuminate\Contracts\Support\Arrayable;
  3. class SqlResultCollection extends \ArrayIterator implements iSqlResultItems, Arrayable
  4. {
  5. private $items;
  6. public function __construct($items)
  7. {
  8. $this->items = $items;
  9. foreach ($items as $item) {
  10. parent::append($item);
  11. }
  12. }
  13. /**
  14. * @return DbStatement[]
  15. */
  16. public function fetchAll()
  17. {
  18. return (array) $this;
  19. }
  20. /**
  21. * @param $field
  22. * @return mixed
  23. */
  24. public function fetchField($field)
  25. {
  26. $item = $this->offsetGet(0);
  27. return $item->{$field};
  28. }
  29. /**
  30. * @return mixed
  31. */
  32. public function fetch()
  33. {
  34. return $this->offsetGet(0);
  35. }
  36. public function assoc($field, $assoc_as_array = false) {
  37. $sql_result_provider = new SqlResultProvider($this->items);
  38. return $sql_result_provider->assoc($field, $assoc_as_array);
  39. }
  40. /**
  41. * Get the collection of items as a plain array.
  42. *
  43. * @return array
  44. */
  45. public function toArray()
  46. {
  47. return array_map(function($value)
  48. {
  49. return $value instanceof ArrayableInterface ? $value->toArray() : $value;
  50. }, $this->items);
  51. }
  52. }