Skip to content

Commit 2dc69f2

Browse files
committed
Add immutability
1 parent c9d90e1 commit 2dc69f2

File tree

5 files changed

+218
-104
lines changed

5 files changed

+218
-104
lines changed

DynamicDataAbstract.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function __construct()
2020
$this->data = new \stdClass();
2121
}
2222

23+
/**
24+
* Will Always try to return a string from object
25+
*
26+
* @return string
27+
*/
2328
public function __toString(): string
2429
{
2530
$val = $this->get();
@@ -35,25 +40,55 @@ public function __toString(): string
3540
return (string)$val;
3641
}
3742

38-
public function __set($key, $value)
43+
/**
44+
* Will add item to object
45+
*
46+
* @param string $key The object key name
47+
* @param mixed $value The object item value
48+
* @return void
49+
*/
50+
public function __set(string $key, mixed $value): void
3951
{
40-
$this->data->{$key} = $value;
52+
$this->addToObject($key, $value);
4153
}
4254

55+
/**
56+
* Try to get data object item
57+
*
58+
* @param $key
59+
* @return null
60+
*/
4361
public function __get($key)
4462
{
4563
return ($this->data->{$key} ?? null);
4664
}
4765

66+
/**
67+
* Get the main data that will contain your encapsulated object
68+
*
69+
* @return mixed
70+
*/
4871
public function getData(): mixed
4972
{
5073
return $this->data;
5174
}
5275

76+
/**
77+
* Will add item to object but will not select it
78+
*
79+
* @param string $key The object key name
80+
* @param mixed $value The object item value
81+
* @return self
82+
*/
83+
public function addToObject(string $key, mixed $value): self
84+
{
85+
$this->data->{$key} = $value;
86+
return $this;
87+
}
88+
5389
/**
5490
* Used to get a readable value
5591
* @return string
56-
* @throws ErrorException
5792
*/
5893
public function toString(): string
5994
{

0 commit comments

Comments
 (0)