Completed
Push — refactor ( 0ce6e5 )
by Marco
01:39
created

Methods::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace MJanssen\Route;
4
5
use InvalidArgumentException;
6
7
class Methods
8
{
9
    const ALLOWED_METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'PURGE', 'OPTIONS', 'TRACE', 'CONNECT'];
10
11
    /**
12
     * @var array
13
     */
14
    private $methods = [];
15
16
    /**
17
     * @param array $methods
18
     */
19
    public function __construct(array $methods)
20
    {
21
        $methods = array_map('strtoupper', $methods);
22
23
        foreach ($methods as $method) {
24
            if (!in_array($method, self::ALLOWED_METHODS)) {
25
                throw new InvalidArgumentException('Method "' . $method . '" is not valid, only the following methods are allowed: ' . join(', ', self::ALLOWED_METHODS));
26
            }
27
        }
28
29
        $this->methods = $methods;
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function toArray()
36
    {
37
        return $this->methods;
38
    }
39
}