@@ -30,11 +30,49 @@ class TypeBuilder
30
30
31
31
public $ file = '' ;
32
32
33
+ public $ confluence = false ;
34
+
33
35
public function __construct ()
34
36
{
35
37
$ this ->processed = new \SplObjectStorage ();
36
38
}
37
39
40
+ public function anchorLink ($ destinationHeader , $ anchor = null )
41
+ {
42
+ if ($ this ->confluence ) {
43
+ $ l = str_replace ('` ' , '' , $ destinationHeader );
44
+ $ l = str_replace (' ' , '- ' , $ l );
45
+ if (!is_string ($ l )) {
46
+ return '# ' ;
47
+ }
48
+
49
+ $ l = urlencode ($ l );
50
+
51
+ return '# ' . $ l ;
52
+ }
53
+
54
+ if (!empty ($ anchor )) {
55
+ $ l = strtolower ($ anchor );
56
+ } else {
57
+ $ l = strtolower ($ destinationHeader );
58
+ }
59
+
60
+ return '# ' . $ l ;
61
+ }
62
+
63
+ public function header ($ text , $ anchor = null )
64
+ {
65
+ if ($ this ->confluence ) {
66
+ return $ text ;
67
+ }
68
+
69
+ if (!empty ($ anchor )) {
70
+ $ l = strtolower ($ anchor );
71
+ } else {
72
+ $ l = strtolower ($ text );
73
+ }
74
+ return '<a id=" ' . $ l . '"> ' . '</a> ' . $ text ;
75
+ }
38
76
39
77
/**
40
78
* @param Schema|boolean|null $schema
@@ -62,10 +100,17 @@ public function getTypeString($schema, $path = '')
62
100
63
101
if (!empty ($ schema ->enum )) {
64
102
$ res = '' ;
65
- foreach ($ schema ->enum as $ value ) {
66
- $ res .= '<br>` ' . var_export ($ value , true ) . '`, ' ;
103
+ if ($ this ->confluence ) {
104
+ foreach ($ schema ->enum as $ value ) {
105
+ $ res .= '` ' . var_export ($ value , true ) . '`, ' ;
106
+ }
107
+ return substr ($ res , 0 , -2 );
108
+ } else {
109
+ foreach ($ schema ->enum as $ value ) {
110
+ $ res .= '<br>` ' . var_export ($ value , true ) . '`, ' ;
111
+ }
112
+ return substr ($ res , 4 , -2 );
67
113
}
68
- return substr ($ res , 4 , -2 );
69
114
}
70
115
71
116
if (!empty ($ schema ->getFromRefs ())) {
@@ -246,6 +291,10 @@ public function getTypeString($schema, $path = '')
246
291
$ res = '`*` ' ;
247
292
}
248
293
294
+ if (empty ($ res )) {
295
+ $ res = '' ;
296
+ }
297
+
249
298
$ res = str_replace ('`` ' , '' , $ res );
250
299
251
300
return $ res ;
@@ -273,7 +322,11 @@ private function typeName(Schema $schema, $path, $raw = false)
273
322
return $ name ;
274
323
}
275
324
276
- return '[` ' . $ name . '`](# ' . strtolower ($ name ) . ') ' ;
325
+ if ($ this ->confluence ) {
326
+ return '[ ' . $ name . ']( ' . $ this ->anchorLink ($ name ) . ') ' ;
327
+ }
328
+
329
+ return '[` ' . $ name . '`]( ' . $ this ->anchorLink ($ name ) . ') ' ;
277
330
}
278
331
279
332
private static function constraints ()
@@ -350,12 +403,10 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
350
403
}
351
404
}
352
405
353
- $ tnl = strtolower ($ typeName );
354
-
355
406
$ res = <<<MD
356
407
357
408
358
- ### <a id=" $ tnl "></a> $ typeName
409
+ ### { $ this -> header ( $ typeName)}
359
410
$ head
360
411
361
412
MD ;
@@ -376,14 +427,19 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
376
427
];
377
428
}
378
429
}
379
- $ res . = TableRenderer::create (new \ArrayIterator ($ rows ))
430
+ $ tr = TableRenderer::create (new \ArrayIterator ($ rows ))
380
431
->stripEmptyColumns ()
381
432
->setColDelimiter ('| ' )
382
433
->setHeadRowDelimiter ('- ' )
383
434
->setOutlineVertical (true )
384
- ->multilineCellDelimiter ('<br> ' )
385
435
->setShowHeader ();
386
436
437
+ if (!$ this ->confluence ) {
438
+ $ tr ->multilineCellDelimiter ('<br> ' );
439
+ }
440
+
441
+ $ res .= $ tr ;
442
+
387
443
$ res .= "\n\n" ;
388
444
389
445
$ rows = [];
@@ -412,14 +468,18 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
412
468
}
413
469
}
414
470
415
- $ res . = TableRenderer::create (new \ArrayIterator ($ rows ))
471
+ $ tr = TableRenderer::create (new \ArrayIterator ($ rows ))
416
472
->stripEmptyColumns ()
417
473
->setColDelimiter ('| ' )
418
474
->setHeadRowDelimiter ('- ' )
419
475
->setOutlineVertical (true )
420
- ->multilineCellDelimiter ('<br> ' )
421
476
->setShowHeader ();
422
477
478
+ if (!$ this ->confluence ) {
479
+ $ tr ->multilineCellDelimiter ('<br> ' );
480
+ }
481
+
482
+ $ res .= $ tr ;
423
483
}
424
484
425
485
$ res .= <<<MD
@@ -470,15 +530,28 @@ public function tableOfContents()
470
530
return $ res ;
471
531
}
472
532
533
+ private function trim ($ s )
534
+ {
535
+ if (empty ($ s )) {
536
+ return '' ;
537
+ }
538
+
539
+ return trim ($ s );
540
+ }
541
+
473
542
private function description (Schema $ schema )
474
543
{
475
- $ res = str_replace ("\n" , " " , trim ($ schema ->title ));
476
- if (trim ($ schema ->description )) {
544
+ $ res = str_replace ("\n" , " " , $ this ->trim ($ schema ->title ));
545
+ if (!is_string ($ res )) {
546
+ return '' ;
547
+ }
548
+
549
+ if ($ this ->trim ($ schema ->description )) {
477
550
if ($ res ) {
478
551
$ res .= ". " ;
479
552
}
480
553
481
- $ res .= str_replace ("\n" , " " , trim ($ schema ->description ));
554
+ $ res .= str_replace ("\n" , " " , $ this -> trim ($ schema ->description ));
482
555
}
483
556
if ($ res ) {
484
557
return rtrim ($ res , '. ' ) . '. ' ;
0 commit comments