Guest User

Untitled

a guest
Apr 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <?php
  2.  
  3. class CountableResults implements Countable {
  4. public function count() {}
  5. }
  6.  
  7. class NimbleResult extends CountableResults implements Iterator {
  8. private $array = array();
  9.  
  10. public function __construct($array, $options = array())
  11.  
  12. {
  13. if(is_array($array)) {
  14. $this->array = $array;
  15. $this->length = $this->count();
  16. }
  17.  
  18. if(!empty($options)) {
  19. foreach($options as $key => $value) {
  20. $this->$key = $value;
  21. }
  22. }
  23.  
  24. }
  25.  
  26. public function clear() {
  27. if(isset($this->key)) {
  28. NimbleRecord::remove_from_cache($this->key);
  29. }
  30. unset($this->array);
  31. unset($this->length);
  32. unset($this);
  33. }
  34.  
  35. public function first() {
  36. return $this->array[0];
  37. }
  38.  
  39. public function to_xml($options = array()) {
  40. $klass = $this->array[0]->class_name();
  41. $plural = Inflector::pluralize($klass);
  42. $xw = new xmlWriter();
  43. $xw->openMemory();
  44. $xw->startDocument('1.0','UTF-8');
  45. $xw->startElement(strtolower($plural));
  46. foreach($this->array as $value) {
  47. $xw->startElement(strtolower($klass));
  48. $xw->writeRaw($value->to_xml(false));
  49. $xw->endElement();
  50. }
  51. $xw->endElement();
  52. $xw->endDtd();
  53. return $xw->outputMemory(true);
  54. }
  55.  
  56. public function to_json($options = array()) {
  57. $out = array();
  58.  
  59. foreach($this->array as $obj) {
  60. $out[] = $obj->row;
  61. }
  62.  
  63. $json = json_encode($out);
  64. return $json;
  65. }
  66.  
  67.  
  68. public function __toString() {
  69. return '';
  70. }
  71.  
  72.  
  73. public function keys(){
  74. return array_keys($this->array);
  75. }
  76.  
  77. public function columns(){
  78. if($this->length > 0) {
  79. return array_keys($this->first()->row);
  80. }
  81. return array();
  82. }
  83.  
  84. public function rewind() {
  85. reset($this->array);
  86. }
  87.  
  88. public function current() {
  89. $array = current($this->array);
  90. return $array;
  91. }
  92.  
  93. public function key() {
  94. $array = key($this->array);
  95. return $array;
  96. }
  97.  
  98. public function next() {
  99. $array = next($this->array);
  100. return $array;
  101. }
  102.  
  103. public function valid() {
  104. $array = $this->current() !== false;
  105. return $array;
  106. }
  107.  
  108. public function count() {
  109. return count($this->array);
  110. }
  111.  
  112. public function length() {
  113. return $this->length;
  114. }
  115.  
  116.  
  117. }
  118.  
  119. ?>
Add Comment
Please, Sign In to add comment