View::getDataAsArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Zenstruck\ControllerUtil;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
class View
9
{
10
    const DEFAULT_STATUS_CODE = 200;
11
12
    private $data;
13
    private $statusCode;
14
    private $template;
15
    private $cache;
16
    private $headers;
17
18
    /**
19
     * @param mixed $data
20
     * @param int   $sharedMaxAge
21
     * @param int   $statusCode
22
     *
23
     * @return static
24
     */
25 1
    public static function createCached($data, $sharedMaxAge, $statusCode = self::DEFAULT_STATUS_CODE)
26
    {
27 1
        return new static($data, $statusCode, null, array('s_maxage' => $sharedMaxAge));
28
    }
29
30
    /**
31
     * @param mixed|null   $data
32
     * @param int          $statusCode
33
     * @param string|array $template
34
     * @param array        $cache
35
     * @param array        $headers
36
     */
37 18
    public function __construct(
38
        $data = null,
39
        $statusCode = self::DEFAULT_STATUS_CODE,
40
        $template = null,
41
        array $cache = array(),
42
        array $headers = array()
43
    ) {
44 18
        $this->data = $data;
45 18
        $this->statusCode = $statusCode;
46 18
        $this->template = $template;
47 18
        $this->cache = $cache;
48 18
        $this->headers = $headers;
49 18
    }
50
51
    /**
52
     * @return mixed|null
53
     */
54 15
    public function getData()
55
    {
56 15
        return $this->data;
57
    }
58
59
    /**
60
     * @param string $key The key to use when creating array if data isn't already an array
61
     *
62
     * @return array
63
     */
64 7
    public function getDataAsArray($key = 'data')
65
    {
66 7
        $data = $this->getData();
67
68 7
        if (null === $data) {
69 1
            return array();
70
        }
71
72 6
        if (!is_array($data)) {
73 2
            $data = array($key => $data);
74 2
        }
75
76 6
        return $data;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 6
    public function getStatusCode()
83
    {
84 6
        return $this->statusCode;
85
    }
86
87
    /**
88
     * Either the template name or an array of templates.
89
     *
90
     * @return string|array|null
91
     */
92 10
    public function getTemplate()
93
    {
94 10
        return $this->template;
95
    }
96
97
    /**
98
     * Returns an array of cache options.
99
     *
100
     * @see Symfony\Component\HttpFoundation\Response::setCache
101
     *
102
     * @return array
103
     */
104 8
    public function getCache()
105
    {
106 8
        return $this->cache;
107
    }
108
109
    /**
110
     * @return array
111
     */
112 7
    public function getHeaders()
113
    {
114 7
        return $this->headers;
115
    }
116
}
117