Completed
Push — master ( c5de01...c9c19d )
by
unknown
07:34 queued 05:10
created

Response::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Coduo\TuTu\Config\Element;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class Response
8
{
9
    /**
10
     * @var string
11
     */
12
    private $content;
13
14
    /**
15
     * @var int
16
     */
17
    private $status;
18
19
    /**
20
     * @var array
21
     */
22
    private $headers;
23
24
    public function __construct($content = '', $status = 200, $headers = [])
25
    {
26
        $this->content = $content;
27
        $this->status = $status;
28
        $this->headers = $headers;
29
    }
30
31
    public static function fromArray(array $arrayConfig)
32
    {
33
        $configResolver = self::createArrayResponseResolver();
34
        $config = $configResolver->resolve($arrayConfig);
35
        $responseConfig = new Response(
36
            $config['content'],
37
            $config['status'],
38
            $config['headers']
39
        );
40
41
        return $responseConfig;
42
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function getStatus()
48
    {
49
        return $this->status;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getHeaders()
56
    {
57
        return $this->headers;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getContent()
64
    {
65
        return $this->content;
66
    }
67
68
    /**
69
     * @return OptionsResolver
70
     */
71
    private static function createArrayResponseResolver()
72
    {
73
        $configResolver = new OptionsResolver();
74
        $configResolver->setDefaults(['content' => '', 'status' => 200, 'headers' => []]);
75
        $configResolver->setAllowedTypes('content', 'string');
76
        $configResolver->setAllowedTypes('status', 'integer');
77
        $configResolver->setAllowedTypes('headers', 'array');
78
79
        return $configResolver;
80
    }
81
}
82