Skip to content

Commit 3beeb06

Browse files
committed
Merge branches 'master' and 'v0.10' of https://github.com/webonyx/graphql-php into v0.10
# Conflicts: # src/Utils/MixedStore.php
2 parents 24bcc65 + bc6a7a3 commit 3beeb06

File tree

5 files changed

+220
-28
lines changed

5 files changed

+220
-28
lines changed

examples/01-blog/Blog/Type/CommentType.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public function __construct()
3636
];
3737
},
3838
'resolveField' => function($value, $args, $context, ResolveInfo $info) {
39-
if (method_exists($this, $info->fieldName)) {
40-
return $this->{$info->fieldName}($value, $args, $context, $info);
39+
$method = 'resolve' . ucfirst($info->fieldName);
40+
if (method_exists($this, $method)) {
41+
return $this->{$method}($value, $args, $context, $info);
4142
} else {
4243
return $value->{$info->fieldName};
4344
}
@@ -46,29 +47,29 @@ public function __construct()
4647
parent::__construct($config);
4748
}
4849

49-
public function author(Comment $comment)
50+
public function resolveAuthor(Comment $comment)
5051
{
5152
if ($comment->isAnonymous) {
5253
return null;
5354
}
5455
return DataSource::findUser($comment->authorId);
5556
}
5657

57-
public function parent(Comment $comment)
58+
public function resolveParent(Comment $comment)
5859
{
5960
if ($comment->parentId) {
6061
return DataSource::findComment($comment->parentId);
6162
}
6263
return null;
6364
}
6465

65-
public function replies(Comment $comment, $args)
66+
public function resolveReplies(Comment $comment, $args)
6667
{
6768
$args += ['after' => null];
6869
return DataSource::findReplies($comment->id, $args['limit'], $args['after']);
6970
}
7071

71-
public function totalReplyCount(Comment $comment)
72+
public function resolveTotalReplyCount(Comment $comment)
7273
{
7374
return DataSource::countReplies($comment->id);
7475
}

examples/01-blog/Blog/Type/StoryType.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public function __construct()
7676
Types::node()
7777
],
7878
'resolveField' => function($value, $args, $context, ResolveInfo $info) {
79-
if (method_exists($this, $info->fieldName)) {
80-
return $this->{$info->fieldName}($value, $args, $context, $info);
79+
$method = 'resolve' . ucfirst($info->fieldName);
80+
if (method_exists($this, $method)) {
81+
return $this->{$method}($value, $args, $context, $info);
8182
} else {
8283
return $value->{$info->fieldName};
8384
}
@@ -86,12 +87,12 @@ public function __construct()
8687
parent::__construct($config);
8788
}
8889

89-
public function author(Story $story)
90+
public function resolveAuthor(Story $story)
9091
{
9192
return DataSource::findUser($story->authorId);
9293
}
9394

94-
public function affordances(Story $story, $args, AppContext $context)
95+
public function resolveAffordances(Story $story, $args, AppContext $context)
9596
{
9697
$isViewer = $context->viewer === DataSource::findUser($story->authorId);
9798
$isLiked = DataSource::isLikedBy($story->id, $context->viewer->id);
@@ -108,17 +109,17 @@ public function affordances(Story $story, $args, AppContext $context)
108109
return $affordances;
109110
}
110111

111-
public function hasViewerLiked(Story $story, $args, AppContext $context)
112+
public function resolveHasViewerLiked(Story $story, $args, AppContext $context)
112113
{
113114
return DataSource::isLikedBy($story->id, $context->viewer->id);
114115
}
115116

116-
public function totalCommentCount(Story $story)
117+
public function resolveTotalCommentCount(Story $story)
117118
{
118119
return DataSource::countComments($story->id);
119120
}
120121

121-
public function comments(Story $story, $args)
122+
public function resolveComments(Story $story, $args)
122123
{
123124
$args += ['after' => null];
124125
return DataSource::findComments($story->id, $args['limit'], $args['after']);

examples/01-blog/Blog/Type/UserType.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function __construct()
4545
Types::node()
4646
],
4747
'resolveField' => function($value, $args, $context, ResolveInfo $info) {
48-
if (method_exists($this, $info->fieldName)) {
49-
return $this->{$info->fieldName}($value, $args, $context, $info);
48+
$method = 'resolve' . ucfirst($info->fieldName);
49+
if (method_exists($this, $method)) {
50+
return $this->{$method}($value, $args, $context, $info);
5051
} else {
5152
return $value->{$info->fieldName};
5253
}
@@ -55,12 +56,12 @@ public function __construct()
5556
parent::__construct($config);
5657
}
5758

58-
public function photo(User $user, $args)
59+
public function resolvePhoto(User $user, $args)
5960
{
6061
return DataSource::getUserPhoto($user->id, $args['size']);
6162
}
6263

63-
public function lastStoryPosted(User $user)
64+
public function resolveLastStoryPosted(User $user)
6465
{
6566
return DataSource::findLastStoryFor($user->id);
6667
}

src/Utils/MixedStore.php

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class MixedStore implements \ArrayAccess
1616
/**
1717
* @var array
1818
*/
19-
private $scalarStore;
19+
private $standardStore;
20+
21+
/**
22+
* @var array
23+
*/
24+
private $floatStore;
2025

2126
/**
2227
* @var \SplObjectStorage
@@ -51,17 +56,41 @@ class MixedStore implements \ArrayAccess
5156
/**
5257
* @var bool
5358
*/
54-
private $nullValueIsSet = false;
59+
private $nullValueIsSet;
60+
61+
/**
62+
* @var mixed
63+
*/
64+
private $trueValue;
65+
66+
/**
67+
* @var bool
68+
*/
69+
private $trueValueIsSet;
70+
71+
/**
72+
* @var mixed
73+
*/
74+
private $falseValue;
75+
76+
/**
77+
* @var bool
78+
*/
79+
private $falseValueIsSet;
5580

5681
/**
5782
* MixedStore constructor.
5883
*/
5984
public function __construct()
6085
{
61-
$this->scalarStore = [];
86+
$this->standardStore = [];
87+
$this->floatStore = [];
6288
$this->objectStore = new \SplObjectStorage();
6389
$this->arrayKeys = [];
6490
$this->arrayValues = [];
91+
$this->nullValueIsSet = false;
92+
$this->trueValueIsSet = false;
93+
$this->falseValueIsSet = false;
6594
}
6695

6796
/**
@@ -78,8 +107,17 @@ public function __construct()
78107
*/
79108
public function offsetExists($offset)
80109
{
81-
if (is_scalar($offset)) {
82-
return array_key_exists($offset, $this->scalarStore);
110+
if (false === $offset) {
111+
return $this->falseValueIsSet;
112+
}
113+
if (true === $offset) {
114+
return $this->trueValueIsSet;
115+
}
116+
if (is_int($offset) || is_string($offset)) {
117+
return array_key_exists($offset, $this->standardStore);
118+
}
119+
if (is_float($offset)) {
120+
return array_key_exists((string) $offset, $this->floatStore);
83121
}
84122
if (is_object($offset)) {
85123
return $this->objectStore->offsetExists($offset);
@@ -110,8 +148,17 @@ public function offsetExists($offset)
110148
*/
111149
public function offsetGet($offset)
112150
{
113-
if (is_scalar($offset)) {
114-
return $this->scalarStore[$offset];
151+
if (true === $offset) {
152+
return $this->trueValue;
153+
}
154+
if (false === $offset) {
155+
return $this->falseValue;
156+
}
157+
if (is_int($offset) || is_string($offset)) {
158+
return $this->standardStore[$offset];
159+
}
160+
if (is_float($offset)) {
161+
return $this->floatStore[(string)$offset];
115162
}
116163
if (is_object($offset)) {
117164
return $this->objectStore->offsetGet($offset);
@@ -147,8 +194,16 @@ public function offsetGet($offset)
147194
*/
148195
public function offsetSet($offset, $value)
149196
{
150-
if (is_scalar($offset)) {
151-
$this->scalarStore[$offset] = $value;
197+
if (false === $offset) {
198+
$this->falseValue = $value;
199+
$this->falseValueIsSet = true;
200+
} else if (true === $offset) {
201+
$this->trueValue = $value;
202+
$this->trueValueIsSet = true;
203+
} else if (is_int($offset) || is_string($offset)) {
204+
$this->standardStore[$offset] = $value;
205+
} else if (is_float($offset)) {
206+
$this->floatStore[(string)$offset] = $value;
152207
} else if (is_object($offset)) {
153208
$this->objectStore[$offset] = $value;
154209
} else if (is_array($offset)) {
@@ -173,8 +228,16 @@ public function offsetSet($offset, $value)
173228
*/
174229
public function offsetUnset($offset)
175230
{
176-
if (is_scalar($offset)) {
177-
unset($this->scalarStore[$offset]);
231+
if (true === $offset) {
232+
$this->trueValue = null;
233+
$this->trueValueIsSet = false;
234+
} else if (false === $offset) {
235+
$this->falseValue = null;
236+
$this->falseValueIsSet = false;
237+
} else if (is_int($offset) || is_string($offset)) {
238+
unset($this->standardStore[$offset]);
239+
} else if (is_float($offset)) {
240+
unset($this->floatStore[(string)$offset]);
178241
} else if (is_object($offset)) {
179242
$this->objectStore->offsetUnset($offset);
180243
} else if (is_array($offset)) {

tests/Utils/MixedStoreTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
namespace GraphQL\Tests\Utils;
3+
4+
5+
use GraphQL\Utils;
6+
use GraphQL\Utils\MixedStore;
7+
8+
class MixedStoreTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var MixedStore
12+
*/
13+
private $mixedStore;
14+
15+
public function setUp()
16+
{
17+
$this->mixedStore = new MixedStore();
18+
}
19+
20+
public function getPossibleValues()
21+
{
22+
return [
23+
null,
24+
false,
25+
true,
26+
'',
27+
'0',
28+
'1',
29+
'a',
30+
[],
31+
new \stdClass(),
32+
function() {},
33+
new MixedStore()
34+
];
35+
}
36+
37+
public function testAcceptsNullKeys()
38+
{
39+
foreach ($this->getPossibleValues() as $value) {
40+
$this->assertAcceptsKeyValue(null, $value);
41+
}
42+
}
43+
44+
public function testAcceptsBoolKeys()
45+
{
46+
foreach ($this->getPossibleValues() as $value) {
47+
$this->assertAcceptsKeyValue(false, $value);
48+
}
49+
foreach ($this->getPossibleValues() as $value) {
50+
$this->assertAcceptsKeyValue(true, $value);
51+
}
52+
}
53+
54+
public function testAcceptsIntKeys()
55+
{
56+
foreach ($this->getPossibleValues() as $value) {
57+
$this->assertAcceptsKeyValue(-100000, $value);
58+
$this->assertAcceptsKeyValue(-1, $value);
59+
$this->assertAcceptsKeyValue(0, $value);
60+
$this->assertAcceptsKeyValue(1, $value);
61+
$this->assertAcceptsKeyValue(1000000, $value);
62+
}
63+
}
64+
65+
public function testAcceptsFloatKeys()
66+
{
67+
foreach ($this->getPossibleValues() as $value) {
68+
$this->assertAcceptsKeyValue(-100000.5, $value);
69+
$this->assertAcceptsKeyValue(-1.6, $value);
70+
$this->assertAcceptsKeyValue(-0.0001, $value);
71+
$this->assertAcceptsKeyValue(0.0000, $value);
72+
$this->assertAcceptsKeyValue(0.0001, $value);
73+
$this->assertAcceptsKeyValue(1.6, $value);
74+
$this->assertAcceptsKeyValue(1000000.5, $value);
75+
}
76+
}
77+
78+
public function testAcceptsArrayKeys()
79+
{
80+
foreach ($this->getPossibleValues() as $value) {
81+
$this->assertAcceptsKeyValue([], $value);
82+
$this->assertAcceptsKeyValue([null], $value);
83+
$this->assertAcceptsKeyValue([[]], $value);
84+
$this->assertAcceptsKeyValue([new \stdClass()], $value);
85+
$this->assertAcceptsKeyValue(['a', 'b'], $value);
86+
$this->assertAcceptsKeyValue(['a' => 'b'], $value);
87+
}
88+
}
89+
90+
public function testAcceptsObjectKeys()
91+
{
92+
foreach ($this->getPossibleValues() as $value) {
93+
$this->assertAcceptsKeyValue(new \stdClass(), $value);
94+
$this->assertAcceptsKeyValue(new MixedStore(), $value);
95+
$this->assertAcceptsKeyValue(function() {}, $value);
96+
}
97+
}
98+
99+
private function assertAcceptsKeyValue($key, $value)
100+
{
101+
$err = 'Failed assertion that MixedStore accepts key ' .
102+
Utils::printSafe($key) . ' with value ' . Utils::printSafe($value);
103+
104+
$this->assertFalse($this->mixedStore->offsetExists($key), $err);
105+
$this->mixedStore->offsetSet($key, $value);
106+
$this->assertTrue($this->mixedStore->offsetExists($key), $err);
107+
$this->assertSame($value, $this->mixedStore->offsetGet($key), $err);
108+
$this->mixedStore->offsetUnset($key);
109+
$this->assertFalse($this->mixedStore->offsetExists($key), $err);
110+
$this->assertProvidesArrayAccess($key, $value);
111+
}
112+
113+
private function assertProvidesArrayAccess($key, $value)
114+
{
115+
$err = 'Failed assertion that MixedStore provides array access for key ' .
116+
Utils::printSafe($key) . ' with value ' . Utils::printSafe($value);
117+
118+
$this->assertFalse(isset($this->mixedStore[$key]), $err);
119+
$this->mixedStore[$key] = $value;
120+
$this->assertTrue(isset($this->mixedStore[$key]), $err);
121+
$this->assertEquals(!empty($value), !empty($this->mixedStore[$key]), $err);
122+
$this->assertSame($value, $this->mixedStore[$key], $err);
123+
unset($this->mixedStore[$key]);
124+
$this->assertFalse(isset($this->mixedStore[$key]), $err);
125+
}
126+
}

0 commit comments

Comments
 (0)