Completed
Push — develop ( 61b5c3...8eb671 )
by Mike
14:27 queued 04:31
created

Extension::path()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Writer\Xslt;
17
18
use phpDocumentor\Descriptor\DescriptorAbstract;
19
use phpDocumentor\Descriptor\ProjectDescriptor;
20
use phpDocumentor\Transformer\Router\Queue;
21
22
/**
23
 * XSLT filters that can be used inside a template.
24
 */
25
class Extension
26
{
27
    /**
28
     * @var Queue
29
     */
30
    public static $routers;
31
32
    /**
33
     * @var ProjectDescriptor
34
     */
35
    public static $projectDescriptor;
36
37
    /**
38
     * Markdown filter.
39
     *
40
     * Example usage inside template would be:
41
     *
42
     * ```
43
     * <div class="long_description">
44
     *     <xsl:value-of
45
     *         select="php:function('phpDocumentor\Transformer\Writer\Xslt\Extension::markdown',
46
     *             string(docblock/long-description))"
47
     *         disable-output-escaping="yes" />
48
     * </div>
49
     * ```
50
     *
51
     * @param string $text
52
     *
53
     * @return string
54
     */
55
    public static function markdown($text)
56
    {
57
        if (!is_string($text)) {
58
            return $text;
59
        }
60
61
        $markdown = \Parsedown::instance();
62
63
        return $markdown->parse($text);
64
    }
65
66
    /**
67
     * Returns a relative URL from the webroot if the given FQSEN exists in the project.
68
     *
69
     * Example usage inside template would be (where @link is an attribute called link):
70
     *
71
     * ```
72
     * <xsl:value-of select="php:function('phpDocumentor\Transformer\Writer\Xslt\Extension::path', string(@link))" />
73
     * ```
74
     *
75
     * @param string $fqsen
76
     *
77
     * @return bool|string
78
     */
79
    public static function path($fqsen)
80
    {
81
        $node = self::getDocumentedElement($fqsen) ?: $fqsen;
82
83
        $rule = self::$routers->match($node);
84
        if (! $rule) {
85
            return '';
86
        }
87
88
        $generatedUrl = $rule->generate($node);
89
90
        return $generatedUrl ? ltrim($generatedUrl, '/') : false;
91
    }
92
93
    /**
94
     * Example usage inside template would be (where @link is an attribute called link):
95
     *
96
     * ```
97
     * <xsl:value-of select="php:function('phpDocumentor\Transformer\Writer\Xslt\Extension::typeOfElement', string(@link))" />
98
     * ```
99
     * @param string $link
100
     *
101
     * @return string
102
     */
103
    public static function typeOfElement($link)
104
    {
105
        if (self::getDocumentedElement($link)) {
106
            return 'documented';
107
        }
108
109
        if (!self::getDocumentedElement($link) && filter_var($link, FILTER_VALIDATE_URL)) {
110
            return 'url';
111
        }
112
113
        return 'undocumented';
114
    }
115
116
    /**
117
     * Returns a Descriptor Object if the given FQSEN exists in the project.
118
     *
119
     * @param string $fqsen
120
     *
121
     * @return bool|DescriptorAbstract
122
     */
123
    private static function getDocumentedElement($fqsen)
124
    {
125
        $projectDescriptor = self::$projectDescriptor;
126
        $elementList = $projectDescriptor->getIndexes()->get('elements');
127
        $prefixedLink = '~\\' . $fqsen;
128
129
        if (isset($elementList[$fqsen])) {
130
            return $elementList[$fqsen];
131
        } elseif (isset($elementList[$prefixedLink])) {
132
            return $elementList[$prefixedLink];
133
        }
134
135
        return false;
136
    }
137
}
138