1 | <?php |
||
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) |
|
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 | 16 | public function __construct( |
|
38 | $data = null, |
||
39 | 8 | $statusCode = self::DEFAULT_STATUS_CODE, |
|
40 | 8 | $template = null, |
|
41 | 8 | array $cache = array(), |
|
42 | 9 | array $headers = array() |
|
43 | ) { |
||
44 | 16 | $this->data = $data; |
|
45 | 16 | $this->statusCode = $statusCode; |
|
46 | 16 | $this->template = $template; |
|
47 | 16 | $this->cache = $cache; |
|
48 | 16 | $this->headers = $headers; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return mixed|null |
||
53 | */ |
||
54 | 10 | public function getData() |
|
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 | 4 | public function getDataAsArray($key = 'data') |
|
65 | { |
||
66 | $data = $this->getData(); |
||
67 | |||
68 | 4 | if (null === $data) { |
|
69 | 1 | return array(); |
|
70 | } |
||
71 | |||
72 | if (!is_array($data)) { |
||
73 | 2 | $data = array($key => $data); |
|
74 | } |
||
75 | |||
76 | 3 | return $data; |
|
77 | 4 | } |
|
78 | |||
79 | /** |
||
80 | * @return int |
||
81 | */ |
||
82 | 5 | public function getStatusCode() |
|
86 | |||
87 | /** |
||
88 | * Either the template name or an array of templates. |
||
89 | * |
||
90 | * @return string|array|null |
||
91 | */ |
||
92 | 7 | public function getTemplate() |
|
96 | |||
97 | /** |
||
98 | * Returns an array of cache options. |
||
99 | * |
||
100 | * @see Symfony\Component\HttpFoundation\Response::setCache |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | 7 | public function getCache() |
|
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | 6 | public function getHeaders() |
|
116 | } |
||
117 |