Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / annotated-command / src / Parser / Internal / DocblockTag.php
1 <?php
2 namespace Consolidation\AnnotatedCommand\Parser\Internal;
3
4 /**
5  * Hold the tag definition for one tag in a DocBlock.
6  *
7  * The tag can be sliced into the following forms:
8  * - "@tag content"
9  * - "@tag word description"
10  * - "@tag $variable description"
11  * - "@tag word $variable description"
12  */
13 class DocblockTag
14 {
15     /** @var string Name of the tag */
16     protected $tag;
17
18     /** @var string|null Contents of the tag. */
19     protected $content;
20
21     const TAG_REGEX = '@(?P<tag>[^\s$]+)[\s]*';
22     const VARIABLE_REGEX = '\\$(?P<variable>[^\s$]+)[\s]*';
23     const VARIABLE_OR_WORD_REGEX = '\\$?(?P<variable>[^\s$]+)[\s]*';
24     const TYPE_REGEX = '(?P<type>[^\s$]+)[\s]*';
25     const WORD_REGEX = '(?P<word>[^\s$]+)[\s]*';
26     const DESCRIPTION_REGEX = '(?P<description>.*)';
27     const IS_TAG_REGEX = '/^[*\s]*@/';
28
29     /**
30      * Check if the provided string begins with a tag
31      * @param string $subject
32      * @return bool
33      */
34     public static function isTag($subject)
35     {
36         return preg_match(self::IS_TAG_REGEX, $subject);
37     }
38
39     /**
40      * Use a regular expression to separate the tag from the content.
41      *
42      * @param string $subject
43      * @param string[] &$matches Sets $matches['tag'] and $matches['description']
44      * @return bool
45      */
46     public static function splitTagAndContent($subject, &$matches)
47     {
48         $regex = '/' . self::TAG_REGEX . self::DESCRIPTION_REGEX . '/s';
49         return preg_match($regex, $subject, $matches);
50     }
51
52     /**
53      * DockblockTag constructor
54      */
55     public function __construct($tag, $content = null)
56     {
57         $this->tag = $tag;
58         $this->content = $content;
59     }
60
61     /**
62      * Add more content onto a tag during parsing.
63      */
64     public function appendContent($line)
65     {
66         $this->content .= "\n$line";
67     }
68
69     /**
70      * Return the tag - e.g. "@foo description" returns 'foo'
71      *
72      * @return string
73      */
74     public function getTag()
75     {
76         return $this->tag;
77     }
78
79     /**
80      * Return the content portion of the tag - e.g. "@foo bar baz boz" returns
81      * "bar baz boz"
82      *
83      * @return string
84      */
85     public function getContent()
86     {
87         return $this->content;
88     }
89
90     /**
91      * Convert tag back into a string.
92      */
93     public function __toString()
94     {
95         return '@' . $this->getTag() . ' ' . $this->getContent();
96     }
97
98     /**
99      * Determine if tag is one of:
100      * - "@tag variable description"
101      * - "@tag $variable description"
102      * - "@tag type $variable description"
103      *
104      * @param string $subject
105      * @param string[] &$matches Sets $matches['variable'] and
106      *   $matches['description']; might set $matches['type'].
107      * @return bool
108      */
109     public function hasVariable(&$matches)
110     {
111         return
112             $this->hasTypeVariableAndDescription($matches) ||
113             $this->hasVariableAndDescription($matches);
114     }
115
116     /**
117      * Determine if tag is "@tag $variable description"
118      * @param string $subject
119      * @param string[] &$matches Sets $matches['variable'] and
120      *   $matches['description']
121      * @return bool
122      */
123     public function hasVariableAndDescription(&$matches)
124     {
125         $regex = '/^\s*' . self::VARIABLE_OR_WORD_REGEX . self::DESCRIPTION_REGEX . '/s';
126         return preg_match($regex, $this->getContent(), $matches);
127     }
128
129     /**
130      * Determine if tag is "@tag type $variable description"
131      *
132      * @param string $subject
133      * @param string[] &$matches Sets $matches['variable'],
134      *   $matches['description'] and $matches['type'].
135      * @return bool
136      */
137     public function hasTypeVariableAndDescription(&$matches)
138     {
139         $regex = '/^\s*' . self::TYPE_REGEX . self::VARIABLE_REGEX . self::DESCRIPTION_REGEX . '/s';
140         return preg_match($regex, $this->getContent(), $matches);
141     }
142
143     /**
144      * Determine if tag is "@tag word description"
145      * @param string $subject
146      * @param string[] &$matches Sets $matches['word'] and
147      *   $matches['description']
148      * @return bool
149      */
150     public function hasWordAndDescription(&$matches)
151     {
152         $regex = '/^\s*' . self::WORD_REGEX . self::DESCRIPTION_REGEX . '/s';
153         return preg_match($regex, $this->getContent(), $matches);
154     }
155 }