Cheetah
Collection.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\GraphNodes;
25 
34 use ArrayAccess;
35 use ArrayIterator;
36 use Countable;
37 use IteratorAggregate;
38 
39 class Collection implements ArrayAccess, Countable, IteratorAggregate
40 {
46  protected $items = [];
47 
53  public function __construct(array $items = [])
54  {
55  $this->items = $items;
56  }
57 
66  public function getField($name, $default = null)
67  {
68  if (isset($this->items[$name])) {
69  return $this->items[$name];
70  }
71 
72  return $default;
73  }
74 
86  public function getProperty($name, $default = null)
87  {
88  return $this->getField($name, $default);
89  }
90 
96  public function getFieldNames()
97  {
98  return array_keys($this->items);
99  }
100 
109  public function getPropertyNames()
110  {
111  return $this->getFieldNames();
112  }
113 
119  public function all()
120  {
121  return $this->items;
122  }
123 
129  public function asArray()
130  {
131  return array_map(function ($value) {
132  return $value instanceof Collection ? $value->asArray() : $value;
133  }, $this->items);
134  }
135 
143  public function map(\Closure $callback)
144  {
145  return new static(array_map($callback, $this->items, array_keys($this->items)));
146  }
147 
155  public function asJson($options = 0)
156  {
157  return json_encode($this->asArray(), $options);
158  }
159 
165  public function count()
166  {
167  return count($this->items);
168  }
169 
175  public function getIterator()
176  {
177  return new ArrayIterator($this->items);
178  }
179 
187  public function offsetExists($key)
188  {
189  return array_key_exists($key, $this->items);
190  }
191 
199  public function offsetGet($key)
200  {
201  return $this->items[$key];
202  }
203 
212  public function offsetSet($key, $value)
213  {
214  if (is_null($key)) {
215  $this->items[] = $value;
216  } else {
217  $this->items[$key] = $value;
218  }
219  }
220 
228  public function offsetUnset($key)
229  {
230  unset($this->items[$key]);
231  }
232 
238  public function __toString()
239  {
240  return $this->asJson();
241  }
242 }
Facebook\GraphNodes\Collection\map
map(\Closure $callback)
Definition: Collection.php:143
Facebook\GraphNodes\Collection\getProperty
getProperty($name, $default=null)
Definition: Collection.php:86
Facebook\GraphNodes\Collection\offsetSet
offsetSet($key, $value)
Definition: Collection.php:212
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Facebook\GraphNodes\Collection\__toString
__toString()
Definition: Collection.php:238
Facebook\GraphNodes\Collection\offsetExists
offsetExists($key)
Definition: Collection.php:187
Facebook\GraphNodes\Collection\$items
$items
Definition: Collection.php:46
Facebook\GraphNodes\Collection\asArray
asArray()
Definition: Collection.php:129
Facebook\GraphNodes\Collection\getIterator
getIterator()
Definition: Collection.php:175
Facebook\GraphNodes\Collection\offsetUnset
offsetUnset($key)
Definition: Collection.php:228
Facebook\GraphNodes\Collection\asJson
asJson($options=0)
Definition: Collection.php:155
Facebook\GraphNodes\Collection\__construct
__construct(array $items=[])
Definition: Collection.php:53
Facebook\GraphNodes\Collection\getFieldNames
getFieldNames()
Definition: Collection.php:96
Facebook\GraphNodes\Collection\getPropertyNames
getPropertyNames()
Definition: Collection.php:109
Facebook\GraphNodes\Collection
Definition: Collection.php:40
Facebook\GraphNodes\Collection\all
all()
Definition: Collection.php:119
Facebook\GraphNodes\Collection\offsetGet
offsetGet($key)
Definition: Collection.php:199
Facebook\GraphNodes\Collection\count
count()
Definition: Collection.php:165
Facebook\GraphNodes\Collection\getField
getField($name, $default=null)
Definition: Collection.php:66
Facebook\GraphNodes
Definition: Birthday.php:24