| 1 |
<?php |
| 2 |
|
| 3 |
require_once ANALYTIFY_LIB_PATH . "Google/Model.php"; |
| 4 |
|
| 5 |
/** |
| 6 |
* Extension to the regular Analytify_Google_Model that automatically |
| 7 |
* exposes the items array for iteration, so you can just |
| 8 |
* iterate over the object rather than a reference inside. |
| 9 |
*/ |
| 10 |
class Analytify_Google_Collection extends Analytify_Google_Model implements Iterator, Countable |
| 11 |
{ |
| 12 |
protected $collection_key = 'items'; |
| 13 |
|
| 14 |
public function rewind() |
| 15 |
{ |
| 16 |
if (isset($this->modelData[$this->collection_key]) |
| 17 |
&& is_array($this->modelData[$this->collection_key])) { |
| 18 |
reset($this->modelData[$this->collection_key]); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public function current() |
| 23 |
{ |
| 24 |
$this->coerceType($this->key()); |
| 25 |
if (is_array($this->modelData[$this->collection_key])) { |
| 26 |
return current($this->modelData[$this->collection_key]); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public function key() |
| 31 |
{ |
| 32 |
if (isset($this->modelData[$this->collection_key]) |
| 33 |
&& is_array($this->modelData[$this->collection_key])) { |
| 34 |
return key($this->modelData[$this->collection_key]); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function next() |
| 39 |
{ |
| 40 |
return next($this->modelData[$this->collection_key]); |
| 41 |
} |
| 42 |
|
| 43 |
public function valid() |
| 44 |
{ |
| 45 |
$key = $this->key(); |
| 46 |
return $key !== null && $key !== false; |
| 47 |
} |
| 48 |
|
| 49 |
public function count() |
| 50 |
{ |
| 51 |
return count($this->modelData[$this->collection_key]); |
| 52 |
} |
| 53 |
|
| 54 |
public function offsetExists ($offset) |
| 55 |
{ |
| 56 |
if (!is_numeric($offset)) { |
| 57 |
return parent::offsetExists($offset); |
| 58 |
} |
| 59 |
return isset($this->modelData[$this->collection_key][$offset]); |
| 60 |
} |
| 61 |
|
| 62 |
public function offsetGet($offset) |
| 63 |
{ |
| 64 |
if (!is_numeric($offset)) { |
| 65 |
return parent::offsetGet($offset); |
| 66 |
} |
| 67 |
$this->coerceType($offset); |
| 68 |
return $this->modelData[$this->collection_key][$offset]; |
| 69 |
} |
| 70 |
|
| 71 |
public function offsetSet($offset, $value) |
| 72 |
{ |
| 73 |
if (!is_numeric($offset)) { |
| 74 |
return parent::offsetSet($offset, $value); |
| 75 |
} |
| 76 |
$this->modelData[$this->collection_key][$offset] = $value; |
| 77 |
} |
| 78 |
|
| 79 |
public function offsetUnset($offset) |
| 80 |
{ |
| 81 |
if (!is_numeric($offset)) { |
| 82 |
return parent::offsetUnset($offset); |
| 83 |
} |
| 84 |
unset($this->modelData[$this->collection_key][$offset]); |
| 85 |
} |
| 86 |
|
| 87 |
private function coerceType($offset) |
| 88 |
{ |
| 89 |
$typeKey = $this->keyType($this->collection_key); |
| 90 |
if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) { |
| 91 |
$type = $this->$typeKey; |
| 92 |
$this->modelData[$this->collection_key][$offset] = |
| 93 |
new $type($this->modelData[$this->collection_key][$offset]); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|