Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

Template::getAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author    Mike van Riel <[email protected]>
12
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      http://phpdoc.org
15
 */
16
17
namespace phpDocumentor\Transformer;
18
19
use phpDocumentor\Transformer\Template\Parameter;
20
21
/**
22
 * Model representing a template.
23
 */
24
final class Template implements \ArrayAccess, \Countable, \IteratorAggregate
25
{
26
    /**
27
     * @var string Name for this template
28
     */
29
    protected $name = null;
30
31
    /**
32
     * @var string The name and optionally mail address of the author, i.e. `Mike van Riel <[email protected]>`.
33
     */
34
    protected $author = '';
35
36
    /**
37
     * @var string The version of the template according to semantic versioning, i.e. 1.2.0
38
     */
39
    protected $version = '';
40
41
    /**
42
     * @var string A free-form copyright notice.
43
     */
44
    protected $copyright = '';
45
46
    /**
47
     * @var string a text providing more information on this template.
48
     */
49
    protected $description = '';
50
51
    /**
52
     * @var Transformation[] A series of transformations to execute in sequence during transformation.
53
     */
54
    protected $transformations = [];
55
56
    /**
57
     * @var Parameter[] Global parameters that are passed to each transformation.
58
     */
59
    protected $parameters = [];
60
61
    /**
62
     * Initializes this object with a name and optionally with contents.
63
     *
64
     * @param string $name Name for this template.
65
     */
66 8
    public function __construct(string $name)
67
    {
68 8
        $this->name = $name;
69 8
    }
70
71
    /**
72
     * Name for this template.
73
     */
74 1
    public function getName(): ?string
75
    {
76 1
        return $this->name;
77
    }
78
79
    /**
80
     * The name of the author of this template (optionally including mail
81
     * address).
82
     *
83
     * @param string $author Name of the author optionally including mail address
84
     *  between angle brackets.
85
     */
86 1
    public function setAuthor(string $author)
87
    {
88 1
        $this->author = $author;
89 1
    }
90
91
    /**
92
     * Returns the name and/or mail address of the author.
93
     */
94 1
    public function getAuthor()
95
    {
96 1
        return $this->author;
97
    }
98
99
    /**
100
     * Sets the copyright string for this template.
101
     *
102
     * @param string $copyright Free-form copyright notice.
103
     */
104 1
    public function setCopyright(string $copyright)
105
    {
106 1
        $this->copyright = $copyright;
107 1
    }
108
109
    /**
110
     * Returns the copyright string for this template.
111
     */
112 1
    public function getCopyright()
113
    {
114 1
        return $this->copyright;
115
    }
116
117
    /**
118
     * Sets the version number for this template.
119
     *
120
     * @param string $version Semantic version number in this format: 1.0.0
121
     *
122
     * @throws \InvalidArgumentException if the version number is invalid
123
     */
124 2
    public function setVersion(string $version)
125
    {
126 2
        if (!preg_match('/^\d+\.\d+\.\d+$/', $version)) {
127 1
            throw new \InvalidArgumentException(
128 1
                'Version number is invalid; ' . $version . ' does not match '
129 1
                . 'x.x.x (where x is a number)'
130
            );
131
        }
132
133 1
        $this->version = $version;
134 1
    }
135
136
    /**
137
     * Returns the version number for this template.
138
     */
139 1
    public function getVersion(): string
140
    {
141 1
        return $this->version;
142
    }
143
144
    /**
145
     * Sets the description for this template.
146
     *
147
     * @param string $description An unconstrained text field where the user can provide additional information
148
     *     regarding details of the template.
149
     */
150 1
    public function setDescription(string $description)
151
    {
152 1
        $this->description = $description;
153 1
    }
154
155
    /**
156
     * Returns the description for this template.
157
     */
158 1
    public function getDescription(): string
159
    {
160 1
        return $this->description;
161
    }
162
163
    /**
164
     * Sets a transformation at the given offset.
165
     *
166
     * @param integer|string $offset The offset to place the value at.
167
     * @param Transformation $value  The transformation to add to this template.
168
     *
169
     * @throws \InvalidArgumentException if an invalid item was received
170
     */
171 7
    public function offsetSet($offset, $value)
172
    {
173 7
        if (!$value instanceof Transformation) {
174 1
            throw new \InvalidArgumentException(
175
                '\phpDocumentor\Transformer\Template may only contain items of '
176 1
                . 'type \phpDocumentor\Transformer\Transformation'
177
            );
178
        }
179
180 6
        $this->transformations[$offset] = $value;
181 6
    }
182
183
    /**
184
     * Gets the transformation at the given offset.
185
     *
186
     * @param integer|string $offset The offset to retrieve from.
187
     *
188
     * @return Transformation
189
     */
190 1
    public function offsetGet($offset)
191
    {
192 1
        return $this->transformations[$offset];
193
    }
194
195
    /**
196
     * Offset to unset.
197
     *
198
     * @param integer|string $offset Index of item to unset.
199
     *
200
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
201
     */
202 1
    public function offsetUnset($offset)
203
    {
204 1
        unset($this->transformations[$offset]);
205 1
    }
206
207
    /**
208
     * Whether a offset exists.
209
     *
210
     * @param mixed $offset An offset to check for.
211
     *
212
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
213
     *
214
     * @return boolean Returns true on success or false on failure.
215
     */
216 2
    public function offsetExists($offset)
217
    {
218 2
        return isset($this->transformations[$offset]);
219
    }
220
221
    /**
222
     * Count the number of transformations.
223
     *
224
     * @link http://php.net/manual/en/countable.count.php
225
     *
226
     * @return int The count as an integer.
227
     */
228 1
    public function count()
229
    {
230 1
        return count($this->transformations);
231
    }
232
233
    /**
234
     * Returns the parameters associated with this template.
235
     *
236
     * @return Parameter[]
237
     */
238 2
    public function getParameters(): array
239
    {
240 2
        return $this->parameters;
241
    }
242
243
    /**
244
     * Sets a new parameter in the collection.
245
     *
246
     * @param string|integer $key
247
     * @param Parameter      $value
248
     */
249 2
    public function setParameter($key, Parameter $value)
250
    {
251 2
        $this->parameters[$key] = $value;
252 2
    }
253
254
    /**
255
     * Pushes the parameters of this template into the transformations.
256
     */
257 1
    public function propagateParameters()
258
    {
259 1
        foreach ($this->transformations as $transformation) {
260 1
            $transformation->setParameters(array_merge($transformation->getParameters(), $this->getParameters()));
261
        }
262 1
    }
263
264 1
    public function getIterator(): \ArrayIterator
265
    {
266 1
        return new \ArrayIterator($this->transformations);
267
    }
268
}
269