Skip to content

Commit e9c1b22

Browse files
committed
feat(view): add ViewData class
1 parent 85a59b0 commit e9c1b22

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

src/classes/Common/View/View.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
class View
1111
{
12+
protected ViewData $data;
13+
1214
/**
1315
* Constructor.
1416
*
@@ -17,8 +19,9 @@ class View
1719
*/
1820
public function __construct(
1921
protected string $file,
20-
protected array $data = []
22+
array $data = []
2123
) {
24+
$this->data = new ViewData($data);
2225
}
2326

2427
/**
@@ -30,17 +33,21 @@ public function __construct(
3033
*/
3134
public function with(array $data, bool $override = true): View
3235
{
33-
$this->data = $override ? $data : array_merge($this->data, $data);
36+
if ($override) {
37+
$this->data->replace($data);
38+
} else {
39+
$this->data->merge($data);
40+
}
3441

3542
return $this;
3643
}
3744

3845
/**
3946
* Returns data array.
4047
*
41-
* @return array<string, mixed> View data.
48+
* @return ViewData View data.
4249
*/
43-
public function getData(): array
50+
public function getData(): ViewData
4451
{
4552
return $this->data;
4653
}
@@ -53,7 +60,7 @@ public function getData(): array
5360
*/
5461
public function render(bool $echo = false): string
5562
{
56-
ViewHelper::pushVariables($this->data);
63+
ViewHelper::pushVariables($this->data->getAll());
5764

5865
ob_start();
5966
include $this->file;

src/classes/Common/View/ViewComposer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ abstract class ViewComposer
1818
*/
1919
protected static ?array $views = null;
2020

21+
/**
22+
* View data instance
23+
*
24+
* @var ViewData
25+
*/
26+
protected ViewData $viewData;
27+
2128
/**
2229
* List of views served by this composer
2330
*
@@ -54,12 +61,12 @@ public static function getViews()
5461
*/
5562
public function compose(View $view): void
5663
{
57-
$viewData = $view->getData();
64+
$this->viewData = $view->getData();
5865

5966
$view->with(
6067
array_merge(
6168
$this->with(),
62-
$viewData,
69+
$this->viewData->getAll(),
6370
$this->override()
6471
)
6572
);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoWStarterTheme\Common\View;
6+
7+
/**
8+
* View Data Container.
9+
*/
10+
class ViewData
11+
{
12+
/** @var array<string, mixed> */
13+
private array $data;
14+
15+
/**
16+
* ViewData constructor.
17+
*
18+
* @param array<string, mixed> $data Initial data for the view.
19+
*/
20+
public function __construct(array $data)
21+
{
22+
$this->data = $data;
23+
}
24+
25+
/**
26+
* Get the view data.
27+
*
28+
* @return array<string, mixed>
29+
*/
30+
public function getAll(): array
31+
{
32+
return $this->data;
33+
}
34+
35+
/**
36+
* Get a specific data item.
37+
*
38+
* @param string $key Key of the data item to retrieve.
39+
* @param mixed $default Default value if the key does not exist.
40+
* @return mixed|null
41+
*/
42+
public function get(string $key, mixed $default = null): mixed
43+
{
44+
return $this->data[$key] ?? $default;
45+
}
46+
47+
/**
48+
* Set a specific data item.
49+
*
50+
* @param string $key Key of the data item to set.
51+
* @param mixed $value Value to set for the key.
52+
* @return void
53+
*/
54+
public function set(string $key, mixed $value): void
55+
{
56+
$this->data[$key] = $value;
57+
}
58+
59+
/**
60+
* Merge additional data into the existing data array.
61+
*
62+
* @param array<string, mixed> $data Data to merge with the existing data.
63+
* @return void
64+
*/
65+
public function merge(array $data): void
66+
{
67+
$this->data = array_merge($this->data, $data);
68+
}
69+
70+
/**
71+
* Replace the entire data array with new data.
72+
*
73+
* @param array<string, mixed> $data New data.
74+
* @return void
75+
*/
76+
public function replace(array $data): void
77+
{
78+
$this->data = $data;
79+
}
80+
}

0 commit comments

Comments
 (0)