Completed
Push — develop ( 2fa385...aae295 )
by Mike
06:11
created

Collection::offsetSet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 9.7
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;
17
18
use phpDocumentor\Transformer\Router\Queue;
19
20
/**
21
 * A collection of Writer objects.
22
 *
23
 * In this collection we can receive writers.
24
 *
25
 * In addition this class can also verify if all requirements for the various writers in it are met.
26
 */
27
class Collection extends \ArrayObject
28
{
29
    /** @var Queue A series of routers, in order of importance, that are used to generate urls with */
30
    protected $routers;
31
32
    /**
33
     * Initializes this writer collection with the necessary requirements.
34
     *
35
     * @param Queue $routers A series of routers, in order of importance, that are used to generate urls with.
36
     */
37 1
    public function __construct(Queue $routers)
38
    {
39 1
        $this->routers = $routers;
40
41 1
        parent::__construct();
42 1
    }
43
44
    /**
45
     * Registers a writer with a given name.
46
     *
47
     * @param string         $index a Writer's name, must be at least 3
48
     *     characters, alphanumeric and/or contain one or more hyphens,
49
     *     underscores and forward slashes.
50
     * @param WriterAbstract $newval The Writer object to register to this name.
51
     *
52
     * @throws \InvalidArgumentException if either of the above restrictions is
53
     *     not met.
54
     */
55 2
    public function offsetSet($index, $newval)
56
    {
57 2
        if (!$newval instanceof WriterAbstract) {
58 1
            throw new \InvalidArgumentException(
59 1
                'The Writer Collection may only contain objects descending from WriterAbstract'
60
            );
61
        }
62
63 1
        if (!preg_match('/^[a-zA-Z0-9\-\_\/]{3,}$/', $index)) {
64 1
            throw new \InvalidArgumentException(
65
                'The name of a Writer may only contain alphanumeric characters, one or more hyphens, underscores and '
66 1
                . 'forward slashes and must be at least three characters wide'
67
            );
68
        }
69
70
        parent::offsetSet($index, $newval);
71
    }
72
73
    /**
74
     * Retrieves a writer from the collection.
75
     *
76
     * @param string $index the name of the writer to retrieve.
77
     *
78
     * @throws \InvalidArgumentException if the writer is not in the collection.
79
     *
80
     * @return WriterAbstract
81
     */
82 2
    public function offsetGet($index)
83
    {
84 2
        if (!$this->offsetExists($index)) {
85 1
            throw new \InvalidArgumentException('Writer "' . $index . '" does not exist');
86
        }
87
88 1
        return parent::offsetGet($index);
89
    }
90
91
    /**
92
     * Iterates over each writer in this collection and checks its requirements.
93
     *
94
     * @throws Exception\RequirementMissing if a requirement of a writer is missing.
95
     */
96 1
    public function checkRequirements()
97
    {
98
        /** @var WriterAbstract $writer */
99 1
        foreach ($this as $writer) {
100 1
            $writer->checkRequirements();
101
        }
102 1
    }
103
}
104