Cheetah
GraphNodeFactory.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\GraphNodes;
25 
28 
44 {
48  const BASE_GRAPH_NODE_CLASS = '\Facebook\GraphNodes\GraphNode';
49 
53  const BASE_GRAPH_EDGE_CLASS = '\Facebook\GraphNodes\GraphEdge';
54 
58  const BASE_GRAPH_OBJECT_PREFIX = '\Facebook\GraphNodes\\';
59 
63  protected $response;
64 
68  protected $decodedBody;
69 
76  {
77  $this->response = $response;
78  $this->decodedBody = $response->getDecodedBody();
79  }
80 
90  public function makeGraphNode($subclassName = null)
91  {
92  $this->validateResponseAsArray();
94 
95  return $this->castAsGraphNodeOrGraphEdge($this->decodedBody, $subclassName);
96  }
97 
105  public function makeGraphAchievement()
106  {
107  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphAchievement');
108  }
109 
117  public function makeGraphAlbum()
118  {
119  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphAlbum');
120  }
121 
129  public function makeGraphPage()
130  {
131  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphPage');
132  }
133 
141  public function makeGraphSessionInfo()
142  {
143  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphSessionInfo');
144  }
145 
153  public function makeGraphUser()
154  {
155  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphUser');
156  }
157 
165  public function makeGraphEvent()
166  {
167  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphEvent');
168  }
169 
177  public function makeGraphGroup()
178  {
179  return $this->makeGraphNode(static::BASE_GRAPH_OBJECT_PREFIX . 'GraphGroup');
180  }
181 
192  public function makeGraphEdge($subclassName = null, $auto_prefix = true)
193  {
194  $this->validateResponseAsArray();
196 
197  if ($subclassName && $auto_prefix) {
198  $subclassName = static::BASE_GRAPH_OBJECT_PREFIX . $subclassName;
199  }
200 
201  return $this->castAsGraphNodeOrGraphEdge($this->decodedBody, $subclassName);
202  }
203 
209  public function validateResponseAsArray()
210  {
211  if (!is_array($this->decodedBody)) {
212  throw new FacebookSDKException('Unable to get response from Graph as array.', 620);
213  }
214  }
215 
222  {
223  if (isset($this->decodedBody['data']) && static::isCastableAsGraphEdge($this->decodedBody['data'])) {
224  throw new FacebookSDKException(
225  'Unable to convert response from Graph to a GraphNode because the response looks like a GraphEdge. Try using GraphNodeFactory::makeGraphEdge() instead.',
226  620
227  );
228  }
229  }
230 
237  {
238  if (!(isset($this->decodedBody['data']) && static::isCastableAsGraphEdge($this->decodedBody['data']))) {
239  throw new FacebookSDKException(
240  'Unable to convert response from Graph to a GraphEdge because the response does not look like a GraphEdge. Try using GraphNodeFactory::makeGraphNode() instead.',
241  620
242  );
243  }
244  }
245 
256  public function safelyMakeGraphNode(array $data, $subclassName = null)
257  {
258  $subclassName = $subclassName ?: static::BASE_GRAPH_NODE_CLASS;
259  static::validateSubclass($subclassName);
260 
261  // Remember the parent node ID
262  $parentNodeId = isset($data['id']) ? $data['id'] : null;
263 
264  $items = [];
265 
266  foreach ($data as $k => $v) {
267  // Array means could be recurable
268  if (is_array($v)) {
269  // Detect any smart-casting from the $graphObjectMap array.
270  // This is always empty on the GraphNode collection, but subclasses can define
271  // their own array of smart-casting types.
272  $graphObjectMap = $subclassName::getObjectMap();
273  $objectSubClass = isset($graphObjectMap[$k])
274  ? $graphObjectMap[$k]
275  : null;
276 
277  // Could be a GraphEdge or GraphNode
278  $items[$k] = $this->castAsGraphNodeOrGraphEdge($v, $objectSubClass, $k, $parentNodeId);
279  } else {
280  $items[$k] = $v;
281  }
282  }
283 
284  return new $subclassName($items);
285  }
286 
299  public function castAsGraphNodeOrGraphEdge(array $data, $subclassName = null, $parentKey = null, $parentNodeId = null)
300  {
301  if (isset($data['data'])) {
302  // Create GraphEdge
303  if (static::isCastableAsGraphEdge($data['data'])) {
304  return $this->safelyMakeGraphEdge($data, $subclassName, $parentKey, $parentNodeId);
305  }
306  // Sometimes Graph is a weirdo and returns a GraphNode under the "data" key
307  $data = $data['data'];
308  }
309 
310  // Create GraphNode
311  return $this->safelyMakeGraphNode($data, $subclassName);
312  }
313 
326  public function safelyMakeGraphEdge(array $data, $subclassName = null, $parentKey = null, $parentNodeId = null)
327  {
328  if (!isset($data['data'])) {
329  throw new FacebookSDKException('Cannot cast data to GraphEdge. Expected a "data" key.', 620);
330  }
331 
332  $dataList = [];
333  foreach ($data['data'] as $graphNode) {
334  $dataList[] = $this->safelyMakeGraphNode($graphNode, $subclassName);
335  }
336 
337  $metaData = $this->getMetaData($data);
338 
339  // We'll need to make an edge endpoint for this in case it's a GraphEdge (for cursor pagination)
340  $parentGraphEdgeEndpoint = $parentNodeId && $parentKey ? '/' . $parentNodeId . '/' . $parentKey : null;
341  $className = static::BASE_GRAPH_EDGE_CLASS;
342 
343  return new $className($this->response->getRequest(), $dataList, $metaData, $parentGraphEdgeEndpoint, $subclassName);
344  }
345 
353  public function getMetaData(array $data)
354  {
355  unset($data['data']);
356 
357  return $data;
358  }
359 
367  public static function isCastableAsGraphEdge(array $data)
368  {
369  if ($data === []) {
370  return true;
371  }
372 
373  // Checks for a sequential numeric array which would be a GraphEdge
374  return array_keys($data) === range(0, count($data) - 1);
375  }
376 
384  public static function validateSubclass($subclassName)
385  {
386  if ($subclassName == static::BASE_GRAPH_NODE_CLASS || is_subclass_of($subclassName, static::BASE_GRAPH_NODE_CLASS)) {
387  return;
388  }
389 
390  throw new FacebookSDKException('The given subclass "' . $subclassName . '" is not valid. Cannot cast to an object that is not a GraphNode subclass.', 620);
391  }
392 }
Facebook\GraphNodes\GraphNodeFactory\$decodedBody
$decodedBody
Definition: GraphNodeFactory.php:68
Facebook\GraphNodes\GraphNodeFactory\validateResponseCastableAsGraphEdge
validateResponseCastableAsGraphEdge()
Definition: GraphNodeFactory.php:236
Facebook\GraphNodes\GraphNodeFactory\validateSubclass
static validateSubclass($subclassName)
Definition: GraphNodeFactory.php:384
Facebook\GraphNodes\GraphNodeFactory\$response
$response
Definition: GraphNodeFactory.php:63
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\GraphNodes\GraphNodeFactory\makeGraphAchievement
makeGraphAchievement()
Definition: GraphNodeFactory.php:105
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
Facebook\GraphNodes\GraphNodeFactory\makeGraphPage
makeGraphPage()
Definition: GraphNodeFactory.php:129
php
Facebook\FacebookResponse
Definition: FacebookResponse.php:36
Facebook\GraphNodes\GraphNodeFactory\validateResponseCastableAsGraphNode
validateResponseCastableAsGraphNode()
Definition: GraphNodeFactory.php:221
Facebook\GraphNodes\GraphNodeFactory\getMetaData
getMetaData(array $data)
Definition: GraphNodeFactory.php:353
Facebook\GraphNodes\GraphNodeFactory\BASE_GRAPH_EDGE_CLASS
const BASE_GRAPH_EDGE_CLASS
Definition: GraphNodeFactory.php:53
Facebook\GraphNodes\GraphNodeFactory\makeGraphSessionInfo
makeGraphSessionInfo()
Definition: GraphNodeFactory.php:141
Facebook\GraphNodes\GraphNodeFactory
Definition: GraphNodeFactory.php:44
Facebook\GraphNodes\GraphNodeFactory\isCastableAsGraphEdge
static isCastableAsGraphEdge(array $data)
Definition: GraphNodeFactory.php:367
Facebook\GraphNodes\GraphNodeFactory\safelyMakeGraphNode
safelyMakeGraphNode(array $data, $subclassName=null)
Definition: GraphNodeFactory.php:256
Facebook\GraphNodes\GraphNodeFactory\makeGraphEvent
makeGraphEvent()
Definition: GraphNodeFactory.php:165
Facebook\GraphNodes\GraphNodeFactory\BASE_GRAPH_NODE_CLASS
const BASE_GRAPH_NODE_CLASS
Definition: GraphNodeFactory.php:48
Facebook\GraphNodes\GraphNodeFactory\__construct
__construct(FacebookResponse $response)
Definition: GraphNodeFactory.php:75
Facebook\GraphNodes\GraphNodeFactory\makeGraphUser
makeGraphUser()
Definition: GraphNodeFactory.php:153
Facebook\GraphNodes\GraphNodeFactory\validateResponseAsArray
validateResponseAsArray()
Definition: GraphNodeFactory.php:209
Facebook\GraphNodes\GraphNodeFactory\makeGraphNode
makeGraphNode($subclassName=null)
Definition: GraphNodeFactory.php:90
Facebook\GraphNodes\GraphNodeFactory\makeGraphEdge
makeGraphEdge($subclassName=null, $auto_prefix=true)
Definition: GraphNodeFactory.php:192
Facebook\GraphNodes\GraphNodeFactory\BASE_GRAPH_OBJECT_PREFIX
const BASE_GRAPH_OBJECT_PREFIX
Definition: GraphNodeFactory.php:58
Facebook\GraphNodes\GraphNodeFactory\makeGraphAlbum
makeGraphAlbum()
Definition: GraphNodeFactory.php:117
Facebook\GraphNodes\GraphNodeFactory\safelyMakeGraphEdge
safelyMakeGraphEdge(array $data, $subclassName=null, $parentKey=null, $parentNodeId=null)
Definition: GraphNodeFactory.php:326
Facebook\GraphNodes\GraphNodeFactory\makeGraphGroup
makeGraphGroup()
Definition: GraphNodeFactory.php:177
Facebook\GraphNodes
Definition: Birthday.php:24
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\GraphNodes\GraphNodeFactory\castAsGraphNodeOrGraphEdge
castAsGraphNodeOrGraphEdge(array $data, $subclassName=null, $parentKey=null, $parentNodeId=null)
Definition: GraphNodeFactory.php:299