@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | 'args' => [ |
101 | 101 | 'bookAttributes' => [ |
102 | 102 | 'type' => Type::listOf($this->bookAttributesInputType), |
103 | - 'validate' => static function($var) { |
|
104 | - $i = 5; |
|
105 | - }, |
|
106 | - 'validateItem' => static function($book) { |
|
107 | - $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
108 | - return $res; |
|
109 | - } |
|
103 | + 'validate' => static function($var) { |
|
104 | + $i = 5; |
|
105 | + }, |
|
106 | + 'validateItem' => static function($book) { |
|
107 | + $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
108 | + return $res; |
|
109 | + } |
|
110 | 110 | ], |
111 | 111 | ], |
112 | 112 | 'resolve' => static function ($value) : bool { |
@@ -166,30 +166,30 @@ discard block |
||
166 | 166 | ] |
167 | 167 | ); |
168 | 168 | |
169 | - static::assertEmpty($res->errors); |
|
169 | + static::assertEmpty($res->errors); |
|
170 | 170 | |
171 | 171 | static::assertEquals( |
172 | - array ( |
|
173 | - 'valid' => false, |
|
174 | - 'result' => NULL, |
|
175 | - 'suberrors' => |
|
176 | - array ( |
|
177 | - 'bookAttributes' => |
|
178 | - array ( |
|
179 | - 'code' => NULL, |
|
180 | - 'msg' => NULL, |
|
181 | - 'suberrors' => |
|
182 | - array ( |
|
183 | - 'code' => 1, |
|
184 | - 'msg' => '', |
|
185 | - 'path' => |
|
186 | - array ( |
|
187 | - 0 => 1, |
|
188 | - ), |
|
189 | - ), |
|
190 | - ), |
|
191 | - ), |
|
192 | - ), |
|
172 | + array ( |
|
173 | + 'valid' => false, |
|
174 | + 'result' => NULL, |
|
175 | + 'suberrors' => |
|
176 | + array ( |
|
177 | + 'bookAttributes' => |
|
178 | + array ( |
|
179 | + 'code' => NULL, |
|
180 | + 'msg' => NULL, |
|
181 | + 'suberrors' => |
|
182 | + array ( |
|
183 | + 'code' => 1, |
|
184 | + 'msg' => '', |
|
185 | + 'path' => |
|
186 | + array ( |
|
187 | + 0 => 1, |
|
188 | + ), |
|
189 | + ), |
|
190 | + ), |
|
191 | + ), |
|
192 | + ), |
|
193 | 193 | $res->data['updateBooks'] |
194 | 194 | ); |
195 | 195 |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | 'title' => [ |
63 | 63 | 'type' => Type::string(), |
64 | 64 | 'description' => 'Enter a book title, no more than 10 characters in length', |
65 | - 'validate' => static function (string $title) { |
|
65 | + 'validate' => static function(string $title) { |
|
66 | 66 | if (strlen($title) > 10) { |
67 | 67 | return [1, 'book title must be less than 10 chaacters']; |
68 | 68 | } |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | 'unknownAuthor', |
77 | 77 | 'authorDeceased', |
78 | 78 | ], |
79 | - 'validate' => function (string $authorId) { |
|
80 | - if (! isset($this->data['people'][$authorId])) { |
|
79 | + 'validate' => function(string $authorId) { |
|
80 | + if (!isset($this->data['people'][$authorId])) { |
|
81 | 81 | return ['unknownAuthor', 'We have no record of that author']; |
82 | 82 | } |
83 | 83 | return 0; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | 'query' => $this->query, |
93 | 93 | 'mutation' => new ObjectType([ |
94 | 94 | 'name' => 'Mutation', |
95 | - 'fields' => function () { |
|
95 | + 'fields' => function() { |
|
96 | 96 | return [ |
97 | 97 | 'updateBooks' => new ValidatedFieldDefinition([ |
98 | 98 | 'name' => 'updateBooks', |
@@ -104,12 +104,12 @@ discard block |
||
104 | 104 | $i = 5; |
105 | 105 | }, |
106 | 106 | 'validateItem' => static function($book) { |
107 | - $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
107 | + $res = isset($book['author']) || isset($book['title']) ? 0 : 1; |
|
108 | 108 | return $res; |
109 | 109 | } |
110 | 110 | ], |
111 | 111 | ], |
112 | - 'resolve' => static function ($value) : bool { |
|
112 | + 'resolve' => static function($value) : bool { |
|
113 | 113 | // ... |
114 | 114 | // do update |
115 | 115 | // ... |
@@ -169,21 +169,21 @@ discard block |
||
169 | 169 | static::assertEmpty($res->errors); |
170 | 170 | |
171 | 171 | static::assertEquals( |
172 | - array ( |
|
172 | + array( |
|
173 | 173 | 'valid' => false, |
174 | 174 | 'result' => NULL, |
175 | 175 | 'suberrors' => |
176 | - array ( |
|
176 | + array( |
|
177 | 177 | 'bookAttributes' => |
178 | - array ( |
|
178 | + array( |
|
179 | 179 | 'code' => NULL, |
180 | 180 | 'msg' => NULL, |
181 | 181 | 'suberrors' => |
182 | - array ( |
|
182 | + array( |
|
183 | 183 | 'code' => 1, |
184 | 184 | 'msg' => '', |
185 | 185 | 'path' => |
186 | - array ( |
|
186 | + array( |
|
187 | 187 | 0 => 1, |
188 | 188 | ), |
189 | 189 | ), |
@@ -2,11 +2,11 @@ |
||
2 | 2 | namespace GraphQL\Type\Definition; |
3 | 3 | |
4 | 4 | class ValidateItemsError extends \Exception { |
5 | - public $path; |
|
6 | - public $error; |
|
7 | - function __construct($path, $error) { |
|
8 | - parent::__construct(); |
|
9 | - $this->path = $path; |
|
10 | - $this->error = $error; |
|
11 | - } |
|
5 | + public $path; |
|
6 | + public $error; |
|
7 | + function __construct($path, $error) { |
|
8 | + parent::__construct(); |
|
9 | + $this->path = $path; |
|
10 | + $this->error = $error; |
|
11 | + } |
|
12 | 12 | } |
13 | 13 | \ No newline at end of file |
@@ -81,11 +81,11 @@ |
||
81 | 81 | ]); |
82 | 82 | } |
83 | 83 | |
84 | - protected function _isAssoc(array $arr) |
|
85 | - { |
|
86 | - if (array() === $arr) return false; |
|
87 | - return array_keys($arr) !== range(0, count($arr) - 1); |
|
88 | - } |
|
84 | + protected function _isAssoc(array $arr) |
|
85 | + { |
|
86 | + if (array() === $arr) return false; |
|
87 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | protected function _validateItems($value, array $path, callable $validate) { |
91 | 91 | foreach ($value as $idx => $subValue) { |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | $this->typeSetter = $config['typeSetter'] ?? null; |
30 | 30 | |
31 | - if (! isset($config['type'])) { |
|
31 | + if (!isset($config['type'])) { |
|
32 | 32 | throw new Exception('You must specify a type for your field'); |
33 | 33 | } |
34 | 34 | |
@@ -38,14 +38,14 @@ discard block |
||
38 | 38 | 'result' => [ |
39 | 39 | 'type' => $config['type'], |
40 | 40 | 'description' => 'The payload, if any', |
41 | - 'resolve' => static function ($value) { |
|
41 | + 'resolve' => static function($value) { |
|
42 | 42 | return $value['result'] ?? null; |
43 | 43 | }, |
44 | 44 | ], |
45 | 45 | 'valid' => [ |
46 | 46 | 'type' => Type::nonNull(Type::boolean()), |
47 | 47 | 'description' => 'Whether all validation passed. True for yes, false for no.', |
48 | - 'resolve' => static function ($value) { |
|
48 | + 'resolve' => static function($value) { |
|
49 | 49 | return $value['valid']; |
50 | 50 | }, |
51 | 51 | ], |
@@ -62,17 +62,17 @@ discard block |
||
62 | 62 | 'type' => $type, |
63 | 63 | 'args' => $args, |
64 | 64 | 'name' => $name, |
65 | - 'resolve' => function ($value, $args1, $context, $info) use ($config, $args) { |
|
65 | + 'resolve' => function($value, $args1, $context, $info) use ($config, $args) { |
|
66 | 66 | // validate inputs |
67 | - $config['type'] = new InputObjectType([ |
|
67 | + $config['type'] = new InputObjectType([ |
|
68 | 68 | 'name'=>'', |
69 | 69 | 'fields' => $args, |
70 | 70 | ]); |
71 | 71 | $errors = $this->_validate($config, $args1); |
72 | 72 | $result = $errors; |
73 | - $result['valid'] = ! $errors; |
|
73 | + $result['valid'] = !$errors; |
|
74 | 74 | |
75 | - if (! isset($result['error']) && ! isset($result['suberrors'])) { |
|
75 | + if (!isset($result['error']) && !isset($result['suberrors'])) { |
|
76 | 76 | $result['result'] = $config['resolve']($value, $args1, $context, $info); |
77 | 77 | } |
78 | 78 | |
@@ -84,19 +84,19 @@ discard block |
||
84 | 84 | protected function _isAssoc(array $arr) |
85 | 85 | { |
86 | 86 | if (array() === $arr) return false; |
87 | - return array_keys($arr) !== range(0, count($arr) - 1); |
|
87 | + return array_keys($arr) !== range(0, count($arr)-1); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | protected function _validateItems($value, array $path, callable $validate) { |
91 | 91 | foreach ($value as $idx => $subValue) { |
92 | - if(is_array($subValue) && !$this->_isAssoc($subValue)) { |
|
92 | + if (is_array($subValue) && !$this->_isAssoc($subValue)) { |
|
93 | 93 | $path[count($path)-1] = $idx; |
94 | 94 | $newPath = $path; |
95 | 95 | $newPath[] = 0; |
96 | 96 | $this->_validateItems($subValue, $newPath, $validate); |
97 | 97 | } |
98 | 98 | else { |
99 | - $path[count($path) - 1] = $idx; |
|
99 | + $path[count($path)-1] = $idx; |
|
100 | 100 | $err = $validate($subValue); |
101 | 101 | |
102 | 102 | if ($err) { |
@@ -124,11 +124,11 @@ discard block |
||
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
127 | - if(isset($arg['validateItem'])) { |
|
127 | + if (isset($arg['validateItem'])) { |
|
128 | 128 | try { |
129 | 129 | $this->_validateItems($value, [0], $arg['validateItem']); |
130 | 130 | } |
131 | - catch(ValidateItemsError $e) { |
|
131 | + catch (ValidateItemsError $e) { |
|
132 | 132 | $res['suberrors'] = [ |
133 | 133 | 'error' => $e->error, |
134 | 134 | 'path' => $e->path |
@@ -83,7 +83,9 @@ discard block |
||
83 | 83 | |
84 | 84 | protected function _isAssoc(array $arr) |
85 | 85 | { |
86 | - if (array() === $arr) return false; |
|
86 | + if (array() === $arr) { |
|
87 | + return false; |
|
88 | + } |
|
87 | 89 | return array_keys($arr) !== range(0, count($arr) - 1); |
88 | 90 | } |
89 | 91 | |
@@ -94,8 +96,7 @@ discard block |
||
94 | 96 | $newPath = $path; |
95 | 97 | $newPath[] = 0; |
96 | 98 | $this->_validateItems($subValue, $newPath, $validate); |
97 | - } |
|
98 | - else { |
|
99 | + } else { |
|
99 | 100 | $path[count($path) - 1] = $idx; |
100 | 101 | $err = $validate($subValue); |
101 | 102 | |
@@ -127,8 +128,7 @@ discard block |
||
127 | 128 | if(isset($arg['validateItem'])) { |
128 | 129 | try { |
129 | 130 | $this->_validateItems($value, [0], $arg['validateItem']); |
130 | - } |
|
131 | - catch(ValidateItemsError $e) { |
|
131 | + } catch(ValidateItemsError $e) { |
|
132 | 132 | $res['suberrors'] = [ |
133 | 133 | 'error' => $e->error, |
134 | 134 | 'path' => $e->path |
@@ -118,21 +118,21 @@ discard block |
||
118 | 118 | public function testListOfInputObjectWithValidationOnSelf() |
119 | 119 | { |
120 | 120 | $type = new UserErrorsType([ |
121 | - 'validate' => static function(array $authors) { |
|
122 | - if(count($authors) < 1) { |
|
123 | - return ['atLeastOneAuthorRequired', "You must submit at least one author"]; |
|
124 | - } |
|
125 | - }, |
|
126 | - 'errorCodes' => ['atLeastOneBookRequired'], |
|
127 | - 'type' => Type::listOf(new InputObjectType([ |
|
121 | + 'validate' => static function(array $authors) { |
|
122 | + if(count($authors) < 1) { |
|
123 | + return ['atLeastOneAuthorRequired', "You must submit at least one author"]; |
|
124 | + } |
|
125 | + }, |
|
126 | + 'errorCodes' => ['atLeastOneBookRequired'], |
|
127 | + 'type' => Type::listOf(new InputObjectType([ |
|
128 | 128 | 'name'=> 'Author', |
129 | 129 | 'fields' => [ |
130 | 130 | 'firstName' => [ |
131 | 131 | 'type' => Type::string(), |
132 | 132 | ], |
133 | - 'lastName' => [ |
|
134 | - 'type' => Type::string(), |
|
135 | - ], |
|
133 | + 'lastName' => [ |
|
134 | + 'type' => Type::string(), |
|
135 | + ], |
|
136 | 136 | ], |
137 | 137 | ])) |
138 | 138 | ], ['authorList']); |
@@ -159,30 +159,30 @@ discard block |
||
159 | 159 | '), SchemaPrinter::doPrint(new Schema(['query' => $type]))); |
160 | 160 | } |
161 | 161 | |
162 | - public function testListOfInputObjectWithValidationOnWrappedSelf() |
|
163 | - { |
|
164 | - $type = new UserErrorsType([ |
|
165 | - 'suberrorCodes' => ['firstNameOrLastNameRequired'], |
|
166 | - 'validateItem' => static function(array $author) { |
|
167 | - if(!isset($author['firstName']) && !isset($author['lastName'])) { |
|
168 | - return ['atLeastOneAuthorRequired', "You must submit a first name or a last name"]; |
|
169 | - } |
|
170 | - }, |
|
171 | - 'type' => Type::listOf(new InputObjectType([ |
|
172 | - 'name'=> 'Author', |
|
173 | - 'fields' => [ |
|
174 | - 'firstName' => [ |
|
175 | - 'type' => Type::string(), |
|
176 | - 'validate' => function(string $name) {} |
|
177 | - ], |
|
178 | - 'lastName' => [ |
|
179 | - 'type' => Type::string(), |
|
180 | - ], |
|
181 | - ], |
|
182 | - ])) |
|
183 | - ], ['authorList']); |
|
184 | - |
|
185 | - self::assertEquals(Utils::nowdoc(' |
|
162 | + public function testListOfInputObjectWithValidationOnWrappedSelf() |
|
163 | + { |
|
164 | + $type = new UserErrorsType([ |
|
165 | + 'suberrorCodes' => ['firstNameOrLastNameRequired'], |
|
166 | + 'validateItem' => static function(array $author) { |
|
167 | + if(!isset($author['firstName']) && !isset($author['lastName'])) { |
|
168 | + return ['atLeastOneAuthorRequired', "You must submit a first name or a last name"]; |
|
169 | + } |
|
170 | + }, |
|
171 | + 'type' => Type::listOf(new InputObjectType([ |
|
172 | + 'name'=> 'Author', |
|
173 | + 'fields' => [ |
|
174 | + 'firstName' => [ |
|
175 | + 'type' => Type::string(), |
|
176 | + 'validate' => function(string $name) {} |
|
177 | + ], |
|
178 | + 'lastName' => [ |
|
179 | + 'type' => Type::string(), |
|
180 | + ], |
|
181 | + ], |
|
182 | + ])) |
|
183 | + ], ['authorList']); |
|
184 | + |
|
185 | + self::assertEquals(Utils::nowdoc(' |
|
186 | 186 | schema { |
187 | 187 | query: AuthorListError |
188 | 188 | } |
@@ -229,5 +229,5 @@ discard block |
||
229 | 229 | } |
230 | 230 | |
231 | 231 | '), SchemaPrinter::doPrint(new Schema(['query' => $type]))); |
232 | - } |
|
232 | + } |
|
233 | 233 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | $type = new UserErrorsType([ |
39 | 39 | 'type' => Type::listOf(Type::id()), |
40 | 40 | 'errorCodes'=> ['atLeastOneRequired'], |
41 | - 'validate' => static function ($value) { |
|
41 | + 'validate' => static function($value) { |
|
42 | 42 | return 0; |
43 | 43 | }, |
44 | 44 | ], ['users']); |
@@ -70,10 +70,10 @@ discard block |
||
70 | 70 | $type = new UserErrorsType([ |
71 | 71 | 'type' => Type::listOf(Type::listOf(Type::listOf(Type::id()))), |
72 | 72 | 'errorCodes'=> ['atLeastOneRequired'], |
73 | - 'validate' => static function ($value) { |
|
73 | + 'validate' => static function($value) { |
|
74 | 74 | return 0; |
75 | 75 | }, |
76 | - 'validateItem' => static function ($value) { |
|
76 | + 'validateItem' => static function($value) { |
|
77 | 77 | return 0; |
78 | 78 | } |
79 | 79 | ], ['users']); |
@@ -117,9 +117,9 @@ discard block |
||
117 | 117 | |
118 | 118 | public function testListOfInputObjectWithValidationOnSelf() |
119 | 119 | { |
120 | - $type = new UserErrorsType([ |
|
120 | + $type = new UserErrorsType([ |
|
121 | 121 | 'validate' => static function(array $authors) { |
122 | - if(count($authors) < 1) { |
|
122 | + if (count($authors) < 1) { |
|
123 | 123 | return ['atLeastOneAuthorRequired', "You must submit at least one author"]; |
124 | 124 | } |
125 | 125 | }, |
@@ -161,10 +161,10 @@ discard block |
||
161 | 161 | |
162 | 162 | public function testListOfInputObjectWithValidationOnWrappedSelf() |
163 | 163 | { |
164 | - $type = new UserErrorsType([ |
|
164 | + $type = new UserErrorsType([ |
|
165 | 165 | 'suberrorCodes' => ['firstNameOrLastNameRequired'], |
166 | 166 | 'validateItem' => static function(array $author) { |
167 | - if(!isset($author['firstName']) && !isset($author['lastName'])) { |
|
167 | + if (!isset($author['firstName']) && !isset($author['lastName'])) { |
|
168 | 168 | return ['atLeastOneAuthorRequired', "You must submit a first name or a last name"]; |
169 | 169 | } |
170 | 170 | }, |