Skip to content

Commit f7d2363

Browse files
authored
Merge pull request facebookarchive#906 from yguedidi/rework_node
Rework GraphNode API
2 parents fd36796 + 8c1c885 commit f7d2363

File tree

5 files changed

+123
-265
lines changed

5 files changed

+123
-265
lines changed

src/GraphNode/GraphNode.php

Lines changed: 37 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
* @package Facebook
2727
*/
28-
class GraphNode implements \ArrayAccess, \Countable, \IteratorAggregate
28+
class GraphNode
2929
{
3030
/**
3131
* @var array maps object key names to Graph object types
@@ -37,7 +37,7 @@ class GraphNode implements \ArrayAccess, \Countable, \IteratorAggregate
3737
*
3838
* @var array
3939
*/
40-
protected $fields = [];
40+
private $fields = [];
4141

4242
/**
4343
* Init this Graph object.
@@ -81,7 +81,7 @@ public function getFieldNames()
8181
*
8282
* @return array
8383
*/
84-
public function all()
84+
public function getFields()
8585
{
8686
return $this->fields;
8787
}
@@ -103,110 +103,23 @@ public function asArray()
103103
}
104104

105105
/**
106-
* Run a map over each field.
107-
*
108-
* @param \Closure $callback
109-
*
110-
* @return static
111-
*/
112-
public function map(\Closure $callback)
113-
{
114-
return new static(array_map($callback, $this->fields, array_keys($this->fields)));
115-
}
116-
117-
/**
118-
* Get all fields as JSON.
119-
*
120-
* @param int $options
106+
* Convert the collection to its string representation.
121107
*
122108
* @return string
123109
*/
124-
public function asJson($options = 0)
125-
{
126-
return json_encode($this->uncastFields(), $options);
127-
}
128-
129-
/**
130-
* Count the number of fields in the collection.
131-
*
132-
* @return int
133-
*/
134-
public function count()
135-
{
136-
return count($this->fields);
137-
}
138-
139-
/**
140-
* Get an iterator for the fields.
141-
*
142-
* @return \ArrayIterator
143-
*/
144-
public function getIterator()
145-
{
146-
return new \ArrayIterator($this->fields);
147-
}
148-
149-
/**
150-
* Determine if an item exists at an offset.
151-
*
152-
* @param mixed $key
153-
*
154-
* @return bool
155-
*/
156-
public function offsetExists($key)
157-
{
158-
return array_key_exists($key, $this->fields);
159-
}
160-
161-
/**
162-
* Get an item at a given offset.
163-
*
164-
* @param mixed $key
165-
*
166-
* @return mixed
167-
*/
168-
public function offsetGet($key)
169-
{
170-
return $this->fields[$key];
171-
}
172-
173-
/**
174-
* Set the item at a given offset.
175-
*
176-
* @param mixed $key
177-
* @param mixed $value
178-
*
179-
* @return void
180-
*/
181-
public function offsetSet($key, $value)
182-
{
183-
if (is_null($key)) {
184-
$this->fields[] = $value;
185-
} else {
186-
$this->fields[$key] = $value;
187-
}
188-
}
189-
190-
/**
191-
* Unset the item at a given offset.
192-
*
193-
* @param string $key
194-
*
195-
* @return void
196-
*/
197-
public function offsetUnset($key)
110+
public function __toString()
198111
{
199-
unset($this->fields[$key]);
112+
return json_encode($this->uncastFields());
200113
}
201114

202115
/**
203-
* Convert the collection to its string representation.
116+
* Getter for $graphNodeMap.
204117
*
205-
* @return string
118+
* @return array
206119
*/
207-
public function __toString()
120+
public static function getNodeMap()
208121
{
209-
return $this->asJson();
122+
return static::$graphNodeMap;
210123
}
211124

212125
/**
@@ -219,7 +132,7 @@ public function __toString()
219132
*
220133
* @return array
221134
*/
222-
public function castFields(array $data)
135+
private function castFields(array $data)
223136
{
224137
$fields = [];
225138

@@ -245,7 +158,7 @@ public function castFields(array $data)
245158
*
246159
* @return array
247160
*/
248-
public function uncastFields()
161+
private function uncastFields()
249162
{
250163
$fields = $this->asArray();
251164

@@ -258,6 +171,28 @@ public function uncastFields()
258171
}, $fields);
259172
}
260173

174+
/**
175+
* Determines if a value from Graph should be cast to DateTime.
176+
*
177+
* @param string $key
178+
*
179+
* @return bool
180+
*/
181+
private function shouldCastAsDateTime($key)
182+
{
183+
return in_array($key, [
184+
'created_time',
185+
'updated_time',
186+
'start_time',
187+
'stop_time',
188+
'end_time',
189+
'backdated_time',
190+
'issued_at',
191+
'expires_at',
192+
'publish_time'
193+
], true);
194+
}
195+
261196
/**
262197
* Detects an ISO 8601 formatted string.
263198
*
@@ -269,7 +204,7 @@ public function uncastFields()
269204
* @see http://www.cl.cam.ac.uk/~mgk25/iso-time.html
270205
* @see http://en.wikipedia.org/wiki/ISO_8601
271206
*/
272-
public function isIso8601DateString($string)
207+
private function isIso8601DateString($string)
273208
{
274209
// This insane regex was yoinked from here:
275210
// http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
@@ -285,36 +220,14 @@ public function isIso8601DateString($string)
285220
return preg_match($crazyInsaneRegexThatSomehowDetectsIso8601, $string) === 1;
286221
}
287222

288-
/**
289-
* Determines if a value from Graph should be cast to DateTime.
290-
*
291-
* @param string $key
292-
*
293-
* @return bool
294-
*/
295-
public function shouldCastAsDateTime($key)
296-
{
297-
return in_array($key, [
298-
'created_time',
299-
'updated_time',
300-
'start_time',
301-
'stop_time',
302-
'end_time',
303-
'backdated_time',
304-
'issued_at',
305-
'expires_at',
306-
'publish_time'
307-
], true);
308-
}
309-
310223
/**
311224
* Casts a date value from Graph to DateTime.
312225
*
313226
* @param int|string $value
314227
*
315228
* @return \DateTime
316229
*/
317-
public function castToDateTime($value)
230+
private function castToDateTime($value)
318231
{
319232
if (is_int($value)) {
320233
$dt = new \DateTime();
@@ -333,18 +246,8 @@ public function castToDateTime($value)
333246
*
334247
* @return Birthday
335248
*/
336-
public function castToBirthday($value)
249+
private function castToBirthday($value)
337250
{
338251
return new Birthday($value);
339252
}
340-
341-
/**
342-
* Getter for $graphNodeMap.
343-
*
344-
* @return array
345-
*/
346-
public static function getNodeMap()
347-
{
348-
return static::$graphNodeMap;
349-
}
350253
}

tests/FacebookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function testPaginationReturnsProperResponse()
274274
$nextPage = $fb->next($graphEdge);
275275
$this->assertInstanceOf(GraphEdge::class, $nextPage);
276276
$this->assertInstanceOf(GraphUser::class, $nextPage[0]);
277-
$this->assertEquals('Foo', $nextPage[0]['name']);
277+
$this->assertEquals('Foo', $nextPage[0]->getField('name'));
278278

279279
$lastResponse = $fb->getLastResponse();
280280
$this->assertInstanceOf(Response::class, $lastResponse);

tests/GraphNode/GraphEdgeTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,20 @@ public function testCanMapOverNodes()
103103
$graphEdge = new GraphEdge(
104104
$this->request,
105105
[
106-
new GraphNode(['name' => 'dummy']),
107-
new GraphNode(['name' => 'dummy']),
106+
new GraphNode(['name' => 'dummy1']),
107+
new GraphNode(['name' => 'dummy2']),
108108
],
109109
['paging' => $this->pagination],
110110
'/1234567890/likes'
111111
);
112112

113-
$graphEdge = $graphEdge->map(function (GraphNode $node) {
114-
$node['name'] = str_replace('dummy', 'foo', $node['name']);
115-
return $node;
116-
});
113+
$output = '';
117114

118-
$graphEdgeToCompare = new GraphEdge(
119-
$this->request,
120-
[
121-
new GraphNode(['name' => 'foo']),
122-
new GraphNode(['name' => 'foo'])
123-
],
124-
['paging' => $this->pagination],
125-
'/1234567890/likes'
126-
);
115+
$graphEdge->map(function (GraphNode $node) use (&$output) {
116+
$output .= $node->getField('name');
117+
});
127118

128-
$this->assertEquals($graphEdgeToCompare, $graphEdge);
119+
$this->assertEquals('dummy1dummy2', $output);
129120
}
130121

131122
public function testAnExistingPropertyCanBeAccessed()

tests/GraphNode/GraphNodeFactoryTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,19 @@ public function testAGraphEdgeWillBeCastRecursively()
348348

349349
// Story
350350
$storyObject = $graphNode[0];
351-
$this->assertInstanceOf(GraphNode::class, $storyObject['from']);
352-
$this->assertInstanceOf(GraphEdge::class, $storyObject['likes']);
353-
$this->assertInstanceOf(GraphEdge::class, $storyObject['comments']);
351+
$this->assertInstanceOf(GraphNode::class, $storyObject->getField('from'));
352+
$this->assertInstanceOf(GraphEdge::class, $storyObject->getField('likes'));
353+
$this->assertInstanceOf(GraphEdge::class, $storyObject->getField('comments'));
354354

355355
// Story Comments
356-
$storyComments = $storyObject['comments'];
356+
$storyComments = $storyObject->getField('comments');
357357
$firstStoryComment = $storyComments[0];
358-
$this->assertInstanceOf(GraphNode::class, $firstStoryComment['from']);
358+
$this->assertInstanceOf(GraphNode::class, $firstStoryComment->getField('from'));
359359

360360
// Message
361361
$messageObject = $graphNode[1];
362-
$this->assertInstanceOf(GraphEdge::class, $messageObject['to']);
363-
$toUsers = $messageObject['to'];
362+
$this->assertInstanceOf(GraphEdge::class, $messageObject->getField('to'));
363+
$toUsers = $messageObject->getField('to');
364364
$this->assertInstanceOf(GraphNode::class, $toUsers[0]);
365365
}
366366

@@ -422,10 +422,10 @@ public function testAGraphEdgeWillGenerateTheProperParentGraphEdges()
422422
$factory = new GraphNodeFactory($res);
423423
$graphEdge = $factory->makeGraphEdge();
424424
$topGraphEdge = $graphEdge->getParentGraphEdge();
425-
$childGraphEdgeOne = $graphEdge[0]['likes']->getParentGraphEdge();
426-
$childGraphEdgeTwo = $graphEdge[1]['likes']->getParentGraphEdge();
427-
$childGraphEdgeThree = $graphEdge[1]['photos']->getParentGraphEdge();
428-
$childGraphEdgeFour = $graphEdge[1]['photos'][0]['likes']->getParentGraphEdge();
425+
$childGraphEdgeOne = $graphEdge[0]->getField('likes')->getParentGraphEdge();
426+
$childGraphEdgeTwo = $graphEdge[1]->getField('likes')->getParentGraphEdge();
427+
$childGraphEdgeThree = $graphEdge[1]->getField('photos')->getParentGraphEdge();
428+
$childGraphEdgeFour = $graphEdge[1]->getField('photos')[0]->getField('likes')->getParentGraphEdge();
429429

430430
$this->assertNull($topGraphEdge);
431431
$this->assertEquals('/111/likes', $childGraphEdgeOne);

0 commit comments

Comments
 (0)