1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Language; |
6
|
|
|
|
7
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
8
|
|
|
use GraphQL\Language\AST\BooleanValueNode; |
9
|
|
|
use GraphQL\Language\AST\DirectiveDefinitionNode; |
10
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
11
|
|
|
use GraphQL\Language\AST\DocumentNode; |
12
|
|
|
use GraphQL\Language\AST\EnumTypeDefinitionNode; |
13
|
|
|
use GraphQL\Language\AST\EnumTypeExtensionNode; |
14
|
|
|
use GraphQL\Language\AST\EnumValueDefinitionNode; |
15
|
|
|
use GraphQL\Language\AST\EnumValueNode; |
16
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
17
|
|
|
use GraphQL\Language\AST\FieldNode; |
18
|
|
|
use GraphQL\Language\AST\FloatValueNode; |
19
|
|
|
use GraphQL\Language\AST\FragmentDefinitionNode; |
20
|
|
|
use GraphQL\Language\AST\FragmentSpreadNode; |
21
|
|
|
use GraphQL\Language\AST\InlineFragmentNode; |
22
|
|
|
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; |
23
|
|
|
use GraphQL\Language\AST\InputObjectTypeExtensionNode; |
24
|
|
|
use GraphQL\Language\AST\InputValueDefinitionNode; |
25
|
|
|
use GraphQL\Language\AST\InterfaceTypeDefinitionNode; |
26
|
|
|
use GraphQL\Language\AST\InterfaceTypeExtensionNode; |
27
|
|
|
use GraphQL\Language\AST\IntValueNode; |
28
|
|
|
use GraphQL\Language\AST\ListTypeNode; |
29
|
|
|
use GraphQL\Language\AST\ListValueNode; |
30
|
|
|
use GraphQL\Language\AST\NamedTypeNode; |
31
|
|
|
use GraphQL\Language\AST\Node; |
32
|
|
|
use GraphQL\Language\AST\NodeKind; |
33
|
|
|
use GraphQL\Language\AST\NonNullTypeNode; |
34
|
|
|
use GraphQL\Language\AST\NullValueNode; |
35
|
|
|
use GraphQL\Language\AST\ObjectFieldNode; |
36
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
37
|
|
|
use GraphQL\Language\AST\ObjectTypeExtensionNode; |
38
|
|
|
use GraphQL\Language\AST\ObjectValueNode; |
39
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode; |
40
|
|
|
use GraphQL\Language\AST\OperationTypeDefinitionNode; |
41
|
|
|
use GraphQL\Language\AST\ScalarTypeDefinitionNode; |
42
|
|
|
use GraphQL\Language\AST\ScalarTypeExtensionNode; |
43
|
|
|
use GraphQL\Language\AST\SchemaDefinitionNode; |
44
|
|
|
use GraphQL\Language\AST\SchemaTypeExtensionNode; |
45
|
|
|
use GraphQL\Language\AST\SelectionSetNode; |
46
|
|
|
use GraphQL\Language\AST\StringValueNode; |
47
|
|
|
use GraphQL\Language\AST\UnionTypeDefinitionNode; |
48
|
|
|
use GraphQL\Language\AST\UnionTypeExtensionNode; |
49
|
|
|
use GraphQL\Language\AST\VariableDefinitionNode; |
50
|
|
|
use GraphQL\Utils\Utils; |
51
|
|
|
use function count; |
52
|
|
|
use function implode; |
53
|
|
|
use function json_encode; |
54
|
|
|
use function preg_replace; |
55
|
|
|
use function sprintf; |
56
|
|
|
use function str_replace; |
57
|
|
|
use function strpos; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Prints AST to string. Capable of printing GraphQL queries and Type definition language. |
61
|
|
|
* Useful for pretty-printing queries or printing back AST for logging, documentation, etc. |
62
|
|
|
* |
63
|
|
|
* Usage example: |
64
|
|
|
* |
65
|
|
|
* ```php |
66
|
|
|
* $query = 'query myQuery {someField}'; |
67
|
|
|
* $ast = GraphQL\Language\Parser::parse($query); |
68
|
|
|
* $printed = GraphQL\Language\Printer::doPrint($ast); |
69
|
|
|
* ``` |
70
|
|
|
*/ |
71
|
|
|
class Printer |
72
|
|
|
{ |
73
|
|
|
/** |
74
|
|
|
* Prints AST to string. Capable of printing GraphQL queries and Type definition language. |
75
|
|
|
* |
76
|
|
|
* @param Node $ast |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
* |
80
|
|
|
* @api |
81
|
|
|
*/ |
82
|
139 |
|
public static function doPrint($ast) |
83
|
|
|
{ |
84
|
139 |
|
static $instance; |
85
|
139 |
|
$instance = $instance ?: new static(); |
86
|
|
|
|
87
|
139 |
|
return $instance->printAST($ast); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
protected function __construct() |
91
|
|
|
{ |
92
|
1 |
|
} |
93
|
|
|
|
94
|
139 |
|
public function printAST($ast) |
95
|
|
|
{ |
96
|
139 |
|
return Visitor::visit( |
97
|
139 |
|
$ast, |
98
|
|
|
[ |
99
|
|
|
'leave' => [ |
100
|
|
|
NodeKind::NAME => static function (Node $node) { |
101
|
74 |
|
return '' . $node->value; |
102
|
139 |
|
}, |
103
|
|
|
|
104
|
|
|
NodeKind::VARIABLE => static function ($node) { |
105
|
4 |
|
return '$' . $node->name; |
106
|
139 |
|
}, |
107
|
|
|
|
108
|
|
|
NodeKind::DOCUMENT => function (DocumentNode $node) { |
109
|
29 |
|
return $this->join($node->definitions, "\n\n") . "\n"; |
110
|
139 |
|
}, |
111
|
|
|
|
112
|
|
|
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) { |
113
|
8 |
|
$op = $node->operation; |
114
|
8 |
|
$name = $node->name; |
115
|
8 |
|
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')'); |
116
|
8 |
|
$directives = $this->join($node->directives, ' '); |
117
|
8 |
|
$selectionSet = $node->selectionSet; |
118
|
|
|
// Anonymous queries with no directives or variable definitions can use |
119
|
|
|
// the query short form. |
120
|
8 |
|
return ! $name && ! $directives && ! $varDefs && $op === 'query' |
|
|
|
|
121
|
8 |
|
? $selectionSet |
122
|
8 |
|
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' '); |
123
|
139 |
|
}, |
124
|
|
|
|
125
|
|
|
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) { |
126
|
4 |
|
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue); |
|
|
|
|
127
|
139 |
|
}, |
128
|
|
|
|
129
|
|
|
NodeKind::SELECTION_SET => function (SelectionSetNode $node) { |
130
|
9 |
|
return $this->block($node->selections); |
131
|
139 |
|
}, |
132
|
|
|
|
133
|
|
|
NodeKind::FIELD => function (FieldNode $node) { |
134
|
10 |
|
return $this->join( |
135
|
|
|
[ |
136
|
10 |
|
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap( |
137
|
10 |
|
'(', |
138
|
10 |
|
$this->join($node->arguments, ', '), |
139
|
10 |
|
')' |
140
|
|
|
), |
141
|
10 |
|
$this->join($node->directives, ' '), |
142
|
10 |
|
$node->selectionSet, |
143
|
|
|
], |
144
|
10 |
|
' ' |
145
|
|
|
); |
146
|
139 |
|
}, |
147
|
|
|
|
148
|
|
|
NodeKind::ARGUMENT => static function (ArgumentNode $node) { |
149
|
9 |
|
return $node->name . ': ' . $node->value; |
|
|
|
|
150
|
139 |
|
}, |
151
|
|
|
|
152
|
|
|
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) { |
153
|
2 |
|
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' ')); |
154
|
139 |
|
}, |
155
|
|
|
|
156
|
|
|
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) { |
157
|
2 |
|
return $this->join( |
158
|
|
|
[ |
159
|
2 |
|
'...', |
160
|
2 |
|
$this->wrap('on ', $node->typeCondition), |
161
|
2 |
|
$this->join($node->directives, ' '), |
162
|
2 |
|
$node->selectionSet, |
163
|
|
|
], |
164
|
2 |
|
' ' |
165
|
|
|
); |
166
|
139 |
|
}, |
167
|
|
|
|
168
|
|
|
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) { |
169
|
|
|
// Note: fragment variable definitions are experimental and may be changed or removed in the future. |
170
|
3 |
|
return sprintf('fragment %s', $node->name) |
171
|
3 |
|
. $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')') |
172
|
3 |
|
. sprintf(' on %s ', $node->typeCondition) |
173
|
3 |
|
. $this->wrap('', $this->join($node->directives, ' '), ' ') |
174
|
3 |
|
. $node->selectionSet; |
175
|
139 |
|
}, |
176
|
|
|
|
177
|
|
|
NodeKind::INT => static function (IntValueNode $node) { |
178
|
21 |
|
return $node->value; |
179
|
139 |
|
}, |
180
|
|
|
|
181
|
|
|
NodeKind::FLOAT => static function (FloatValueNode $node) { |
182
|
8 |
|
return $node->value; |
183
|
139 |
|
}, |
184
|
|
|
|
185
|
|
|
NodeKind::STRING => function (StringValueNode $node, $key) { |
186
|
29 |
|
if ($node->block) { |
187
|
8 |
|
return $this->printBlockString($node->value, $key === 'description'); |
188
|
|
|
} |
189
|
|
|
|
190
|
25 |
|
return json_encode($node->value); |
191
|
139 |
|
}, |
192
|
|
|
|
193
|
|
|
NodeKind::BOOLEAN => static function (BooleanValueNode $node) { |
194
|
12 |
|
return $node->value ? 'true' : 'false'; |
195
|
139 |
|
}, |
196
|
|
|
|
197
|
|
|
NodeKind::NULL => static function (NullValueNode $node) { |
|
|
|
|
198
|
8 |
|
return 'null'; |
199
|
139 |
|
}, |
200
|
|
|
|
201
|
|
|
NodeKind::ENUM => static function (EnumValueNode $node) { |
202
|
18 |
|
return $node->value; |
203
|
139 |
|
}, |
204
|
|
|
|
205
|
|
|
NodeKind::LST => function (ListValueNode $node) { |
206
|
5 |
|
return '[' . $this->join($node->values, ', ') . ']'; |
207
|
139 |
|
}, |
208
|
|
|
|
209
|
|
|
NodeKind::OBJECT => function (ObjectValueNode $node) { |
210
|
4 |
|
return '{' . $this->join($node->fields, ', ') . '}'; |
211
|
139 |
|
}, |
212
|
|
|
|
213
|
|
|
NodeKind::OBJECT_FIELD => static function (ObjectFieldNode $node) { |
214
|
4 |
|
return $node->name . ': ' . $node->value; |
|
|
|
|
215
|
139 |
|
}, |
216
|
|
|
|
217
|
|
|
NodeKind::DIRECTIVE => function (DirectiveNode $node) { |
218
|
7 |
|
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'); |
219
|
139 |
|
}, |
220
|
|
|
|
221
|
|
|
NodeKind::NAMED_TYPE => static function (NamedTypeNode $node) { |
222
|
67 |
|
return $node->name; |
223
|
139 |
|
}, |
224
|
|
|
|
225
|
|
|
NodeKind::LIST_TYPE => static function (ListTypeNode $node) { |
226
|
57 |
|
return '[' . $node->type . ']'; |
227
|
139 |
|
}, |
228
|
|
|
|
229
|
|
|
NodeKind::NON_NULL_TYPE => static function (NonNullTypeNode $node) { |
230
|
59 |
|
return $node->type . '!'; |
231
|
139 |
|
}, |
232
|
|
|
|
233
|
|
|
NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) { |
234
|
3 |
|
return $this->join( |
235
|
|
|
[ |
236
|
3 |
|
'schema', |
237
|
3 |
|
$this->join($def->directives, ' '), |
238
|
3 |
|
$this->block($def->operationTypes), |
239
|
|
|
], |
240
|
3 |
|
' ' |
241
|
|
|
); |
242
|
139 |
|
}, |
243
|
|
|
|
244
|
|
|
NodeKind::OPERATION_TYPE_DEFINITION => static function (OperationTypeDefinitionNode $def) { |
245
|
4 |
|
return $def->operation . ': ' . $def->type; |
246
|
139 |
|
}, |
247
|
|
|
|
248
|
|
|
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) { |
249
|
58 |
|
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' '); |
250
|
139 |
|
}), |
251
|
|
|
|
252
|
|
|
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) { |
253
|
57 |
|
return $this->join( |
254
|
|
|
[ |
255
|
57 |
|
'type', |
256
|
57 |
|
$def->name, |
257
|
57 |
|
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), |
258
|
57 |
|
$this->join($def->directives, ' '), |
259
|
57 |
|
$this->block($def->fields), |
260
|
|
|
], |
261
|
57 |
|
' ' |
262
|
|
|
); |
263
|
139 |
|
}), |
264
|
|
|
|
265
|
|
|
NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) { |
266
|
|
|
$noIndent = Utils::every($def->arguments, static function (string $arg) { |
267
|
57 |
|
return strpos($arg, "\n") === false; |
268
|
57 |
|
}); |
269
|
|
|
|
270
|
57 |
|
return $def->name |
271
|
57 |
|
. ($noIndent |
272
|
57 |
|
? $this->wrap('(', $this->join($def->arguments, ', '), ')') |
273
|
57 |
|
: $this->wrap("(\n", $this->indent($this->join($def->arguments, "\n")), "\n)")) |
274
|
57 |
|
. ': ' . $def->type |
|
|
|
|
275
|
57 |
|
. $this->wrap(' ', $this->join($def->directives, ' ')); |
276
|
139 |
|
}), |
277
|
|
|
|
278
|
|
|
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) { |
279
|
57 |
|
return $this->join( |
280
|
|
|
[ |
281
|
57 |
|
$def->name . ': ' . $def->type, |
|
|
|
|
282
|
57 |
|
$this->wrap('= ', $def->defaultValue), |
283
|
57 |
|
$this->join($def->directives, ' '), |
284
|
|
|
], |
285
|
57 |
|
' ' |
286
|
|
|
); |
287
|
139 |
|
}), |
288
|
|
|
|
289
|
139 |
|
NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription( |
290
|
|
|
function (InterfaceTypeDefinitionNode $def) { |
291
|
57 |
|
return $this->join( |
292
|
|
|
[ |
293
|
57 |
|
'interface', |
294
|
57 |
|
$def->name, |
295
|
57 |
|
$this->join($def->directives, ' '), |
296
|
57 |
|
$this->block($def->fields), |
297
|
|
|
], |
298
|
57 |
|
' ' |
299
|
|
|
); |
300
|
139 |
|
} |
301
|
|
|
), |
302
|
|
|
|
303
|
|
|
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) { |
304
|
57 |
|
return $this->join( |
305
|
|
|
[ |
306
|
57 |
|
'union', |
307
|
57 |
|
$def->name, |
308
|
57 |
|
$this->join($def->directives, ' '), |
309
|
57 |
|
$def->types |
310
|
57 |
|
? '= ' . $this->join($def->types, ' | ') |
311
|
57 |
|
: '', |
312
|
|
|
], |
313
|
57 |
|
' ' |
314
|
|
|
); |
315
|
139 |
|
}), |
316
|
|
|
|
317
|
|
|
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) { |
318
|
57 |
|
return $this->join( |
319
|
|
|
[ |
320
|
57 |
|
'enum', |
321
|
57 |
|
$def->name, |
322
|
57 |
|
$this->join($def->directives, ' '), |
323
|
57 |
|
$this->block($def->values), |
324
|
|
|
], |
325
|
57 |
|
' ' |
326
|
|
|
); |
327
|
139 |
|
}), |
328
|
|
|
|
329
|
|
|
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) { |
330
|
57 |
|
return $this->join([$def->name, $this->join($def->directives, ' ')], ' '); |
331
|
139 |
|
}), |
332
|
|
|
|
333
|
|
|
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function ( |
334
|
|
|
InputObjectTypeDefinitionNode $def |
335
|
|
|
) { |
336
|
57 |
|
return $this->join( |
337
|
|
|
[ |
338
|
57 |
|
'input', |
339
|
57 |
|
$def->name, |
340
|
57 |
|
$this->join($def->directives, ' '), |
341
|
57 |
|
$this->block($def->fields), |
342
|
|
|
], |
343
|
57 |
|
' ' |
344
|
|
|
); |
345
|
139 |
|
}), |
346
|
|
|
|
347
|
|
|
NodeKind::SCHEMA_EXTENSION => function (SchemaTypeExtensionNode $def) { |
348
|
1 |
|
return $this->join( |
349
|
|
|
[ |
350
|
1 |
|
'extend schema', |
351
|
1 |
|
$this->join($def->directives, ' '), |
352
|
1 |
|
$this->block($def->operationTypes), |
353
|
|
|
], |
354
|
1 |
|
' ' |
355
|
|
|
); |
356
|
139 |
|
}, |
357
|
|
|
|
358
|
|
|
NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) { |
359
|
3 |
|
return $this->join( |
360
|
|
|
[ |
361
|
3 |
|
'extend scalar', |
362
|
3 |
|
$def->name, |
363
|
3 |
|
$this->join($def->directives, ' '), |
364
|
|
|
], |
365
|
3 |
|
' ' |
366
|
|
|
); |
367
|
139 |
|
}, |
368
|
|
|
|
369
|
|
|
NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) { |
370
|
2 |
|
return $this->join( |
371
|
|
|
[ |
372
|
2 |
|
'extend type', |
373
|
2 |
|
$def->name, |
374
|
2 |
|
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), |
375
|
2 |
|
$this->join($def->directives, ' '), |
376
|
2 |
|
$this->block($def->fields), |
377
|
|
|
], |
378
|
2 |
|
' ' |
379
|
|
|
); |
380
|
139 |
|
}, |
381
|
|
|
|
382
|
|
|
NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) { |
383
|
2 |
|
return $this->join( |
384
|
|
|
[ |
385
|
2 |
|
'extend interface', |
386
|
2 |
|
$def->name, |
387
|
2 |
|
$this->join($def->directives, ' '), |
388
|
2 |
|
$this->block($def->fields), |
389
|
|
|
], |
390
|
2 |
|
' ' |
391
|
|
|
); |
392
|
139 |
|
}, |
393
|
|
|
|
394
|
|
|
NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) { |
395
|
2 |
|
return $this->join( |
396
|
|
|
[ |
397
|
2 |
|
'extend union', |
398
|
2 |
|
$def->name, |
399
|
2 |
|
$this->join($def->directives, ' '), |
400
|
2 |
|
$def->types |
401
|
2 |
|
? '= ' . $this->join($def->types, ' | ') |
402
|
2 |
|
: '', |
403
|
|
|
], |
404
|
2 |
|
' ' |
405
|
|
|
); |
406
|
139 |
|
}, |
407
|
|
|
|
408
|
|
|
NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) { |
409
|
2 |
|
return $this->join( |
410
|
|
|
[ |
411
|
2 |
|
'extend enum', |
412
|
2 |
|
$def->name, |
413
|
2 |
|
$this->join($def->directives, ' '), |
414
|
2 |
|
$this->block($def->values), |
415
|
|
|
], |
416
|
2 |
|
' ' |
417
|
|
|
); |
418
|
139 |
|
}, |
419
|
|
|
|
420
|
|
|
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) { |
421
|
2 |
|
return $this->join( |
422
|
|
|
[ |
423
|
2 |
|
'extend input', |
424
|
2 |
|
$def->name, |
425
|
2 |
|
$this->join($def->directives, ' '), |
426
|
2 |
|
$this->block($def->fields), |
427
|
|
|
], |
428
|
2 |
|
' ' |
429
|
|
|
); |
430
|
139 |
|
}, |
431
|
|
|
|
432
|
|
|
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) { |
433
|
|
|
$noIndent = Utils::every($def->arguments, static function (string $arg) { |
434
|
57 |
|
return strpos($arg, "\n") === false; |
435
|
57 |
|
}); |
436
|
|
|
|
437
|
|
|
return 'directive @' |
438
|
57 |
|
. $def->name |
439
|
57 |
|
. ($noIndent |
440
|
57 |
|
? $this->wrap('(', $this->join($def->arguments, ', '), ')') |
441
|
57 |
|
: $this->wrap("(\n", $this->indent($this->join($def->arguments, "\n")), "\n")) |
442
|
57 |
|
. ' on ' . $this->join($def->locations, ' | '); |
443
|
139 |
|
}), |
444
|
|
|
], |
445
|
|
|
] |
446
|
|
|
); |
447
|
|
|
} |
448
|
|
|
|
449
|
139 |
|
public function addDescription(callable $cb) |
450
|
|
|
{ |
451
|
|
|
return function ($node) use ($cb) { |
452
|
58 |
|
return $this->join([$node->description, $cb($node)], "\n"); |
453
|
139 |
|
}; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* If maybeString is not null or empty, then wrap with start and end, otherwise |
458
|
|
|
* print an empty string. |
459
|
|
|
*/ |
460
|
67 |
|
public function wrap($start, $maybeString, $end = '') |
461
|
|
|
{ |
462
|
67 |
|
return $maybeString ? ($start . $maybeString . $end) : ''; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Given array, print each item on its own line, wrapped in an |
467
|
|
|
* indented "{ }" block. |
468
|
|
|
*/ |
469
|
66 |
|
public function block($array) |
470
|
|
|
{ |
471
|
66 |
|
return $array && $this->length($array) |
472
|
66 |
|
? "{\n" . $this->indent($this->join($array, "\n")) . "\n}" |
473
|
66 |
|
: ''; |
474
|
|
|
} |
475
|
|
|
|
476
|
66 |
|
public function indent($maybeString) |
477
|
|
|
{ |
478
|
66 |
|
return $maybeString ? ' ' . str_replace("\n", "\n ", $maybeString) : ''; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
public function manyList($start, $list, $separator, $end) |
482
|
|
|
{ |
483
|
|
|
return $this->length($list) === 0 ? null : ($start . $this->join($list, $separator) . $end); |
484
|
|
|
} |
485
|
|
|
|
486
|
66 |
|
public function length($maybeArray) |
487
|
|
|
{ |
488
|
66 |
|
return $maybeArray ? count($maybeArray) : 0; |
489
|
|
|
} |
490
|
|
|
|
491
|
69 |
|
public function join($maybeArray, $separator = '') |
492
|
|
|
{ |
493
|
69 |
|
return $maybeArray |
494
|
69 |
|
? implode( |
495
|
69 |
|
$separator, |
496
|
69 |
|
Utils::filter( |
497
|
69 |
|
$maybeArray, |
498
|
|
|
static function ($x) { |
499
|
69 |
|
return (bool) $x; |
500
|
69 |
|
} |
501
|
|
|
) |
502
|
|
|
) |
503
|
69 |
|
: ''; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Print a block string in the indented block form by adding a leading and |
508
|
|
|
* trailing blank line. However, if a block string starts with whitespace and is |
509
|
|
|
* a single-line, adding a leading blank line would strip that whitespace. |
510
|
|
|
*/ |
511
|
8 |
|
private function printBlockString($value, $isDescription) |
512
|
|
|
{ |
513
|
8 |
|
$escaped = str_replace('"""', '\\"""', $value); |
514
|
|
|
|
515
|
8 |
|
return ($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false |
516
|
3 |
|
? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""') |
517
|
8 |
|
: ('"""' . "\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n" . '"""'); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|