Completed
Push — develop ( 4557a4...04dbcf )
by Jaap
09:52
created

Extension::typeOfElement()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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