Cheetah
GraphNode.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\GraphNodes;
25 
31 class GraphNode extends Collection
32 {
36  protected static $graphObjectMap = [];
37 
43  public function __construct(array $data = [])
44  {
45  parent::__construct($this->castItems($data));
46  }
47 
58  public function castItems(array $data)
59  {
60  $items = [];
61 
62  foreach ($data as $k => $v) {
63  if ($this->shouldCastAsDateTime($k)
64  && (is_numeric($v)
65  || $this->isIso8601DateString($v))
66  ) {
67  $items[$k] = $this->castToDateTime($v);
68  } elseif ($k === 'birthday') {
69  $items[$k] = $this->castToBirthday($v);
70  } else {
71  $items[$k] = $v;
72  }
73  }
74 
75  return $items;
76  }
77 
84  public function uncastItems()
85  {
86  $items = $this->asArray();
87 
88  return array_map(function ($v) {
89  if ($v instanceof \DateTime) {
90  return $v->format(\DateTime::ISO8601);
91  }
92 
93  return $v;
94  }, $items);
95  }
96 
104  public function asJson($options = 0)
105  {
106  return json_encode($this->uncastItems(), $options);
107  }
108 
120  public function isIso8601DateString($string)
121  {
122  // This insane regex was yoinked from here:
123  // http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
124  // ...and I'm all like:
125  // http://thecodinglove.com/post/95378251969/when-code-works-and-i-dont-know-why
126  $crazyInsaneRegexThatSomehowDetectsIso8601 = '/^([\+-]?\d{4}(?!\d{2}\b))'
127  . '((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?'
128  . '|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d'
129  . '|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])'
130  . '((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d'
131  . '([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/';
132 
133  return preg_match($crazyInsaneRegexThatSomehowDetectsIso8601, $string) === 1;
134  }
135 
143  public function shouldCastAsDateTime($key)
144  {
145  return in_array($key, [
146  'created_time',
147  'updated_time',
148  'start_time',
149  'end_time',
150  'backdated_time',
151  'issued_at',
152  'expires_at',
153  'publish_time'
154  ], true);
155  }
156 
164  public function castToDateTime($value)
165  {
166  if (is_int($value)) {
167  $dt = new \DateTime();
168  $dt->setTimestamp($value);
169  } else {
170  $dt = new \DateTime($value);
171  }
172 
173  return $dt;
174  }
175 
183  public function castToBirthday($value)
184  {
185  return new Birthday($value);
186  }
187 
193  public static function getObjectMap()
194  {
195  return static::$graphObjectMap;
196  }
197 }
Facebook\GraphNodes\GraphNode\__construct
__construct(array $data=[])
Definition: GraphNode.php:43
Facebook\GraphNodes\GraphNode\$graphObjectMap
static $graphObjectMap
Definition: GraphNode.php:36
php
Facebook\GraphNodes\GraphNode\asJson
asJson($options=0)
Definition: GraphNode.php:104
Facebook\GraphNodes\GraphNode\shouldCastAsDateTime
shouldCastAsDateTime($key)
Definition: GraphNode.php:143
Facebook\GraphNodes\GraphNode\getObjectMap
static getObjectMap()
Definition: GraphNode.php:193
Facebook\GraphNodes\GraphNode\castToBirthday
castToBirthday($value)
Definition: GraphNode.php:183
Facebook\GraphNodes\Collection\$items
$items
Definition: Collection.php:46
Facebook\GraphNodes\Collection\asArray
asArray()
Definition: Collection.php:129
Facebook\GraphNodes\GraphNode\isIso8601DateString
isIso8601DateString($string)
Definition: GraphNode.php:120
Facebook\GraphNodes\GraphNode
Definition: GraphNode.php:32
Facebook\GraphNodes\GraphNode\castToDateTime
castToDateTime($value)
Definition: GraphNode.php:164
Facebook\GraphNodes\GraphNode\uncastItems
uncastItems()
Definition: GraphNode.php:84
Facebook\GraphNodes\GraphNode\castItems
castItems(array $data)
Definition: GraphNode.php:58
Facebook\GraphNodes\Collection
Definition: Collection.php:40
Facebook\GraphNodes
Definition: Birthday.php:24
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\GraphNodes\Birthday
Definition: Birthday.php:34