Skip to content

Commit a8fd53c

Browse files
author
František Mazura
committed
added setOriginalValue and getOriginalValue
1 parent 316afc0 commit a8fd53c

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ PHP Value object for storing valid data.
2020
# Changelog
2121

2222
- 2021-02-26
23-
- ignored attributes
24-
- touch (`setTouched`, `isTouched`, `getTouchedAll`)
25-
- add interface `ValueObjectInterface`
26-
- remove trait
27-
- min request PHP 7.4
28-
- check null
29-
- root element
23+
- ignored attributes
24+
- touch (`setTouched`, `isTouched`, `getTouchedAll`)
25+
- add interface `ValueObjectInterface`
26+
- remove trait
27+
- min request PHP 7.4
28+
- check null
29+
- root element
30+
31+
- 2021-03-17
32+
- added `setOriginalValue` and `getOriginalValue` methods for get original value from init

src/AbstractValueObject.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class AbstractValueObject implements ValueObjectInterface
2323

2424
private array $touched = [];
2525
private array $ignoredAttrs = ["ignoredAttrs", "touched"];
26+
private array $originalValues = [];
2627

2728
public function isIgnoredAttr(string $attrName): bool
2829
{
@@ -73,6 +74,7 @@ public function init($data): self
7374
$setter = "set" . ucfirst($name);
7475
$value = $data[$name] ?? null;
7576
$this->setTouched($name);
77+
$this->setOriginalValue($name, $value);
7678
try {
7779
if (method_exists($this, $setter)) {
7880
$this->setValue($name, $setter, $value, $this);
@@ -300,4 +302,14 @@ private static function createValueObject($className, $data)
300302
}
301303
return false;
302304
}
305+
306+
public function setOriginalValue(string $attrName, $value)
307+
{
308+
$this->originalValues[$attrName] = $value;
309+
}
310+
311+
public function getOriginalValue(string $attrName)
312+
{
313+
$this->originalValues[$attrName];
314+
}
303315
}

src/Interfaces/ValueObjectInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,21 @@ public function getTouchedAll(): array;
100100
* @return bool
101101
*/
102102
public function isIgnoredAttr(string $attrName): bool;
103+
104+
/**
105+
* set original init value passed into setter before transformation
106+
*
107+
* @param string $attrName
108+
* @param $value
109+
* @return mixed
110+
*/
111+
public function setOriginalValue(string $attrName, $value);
112+
113+
/**
114+
* get original init value passed into setter before transformation
115+
*
116+
* @param string $attrName
117+
* @return mixed
118+
*/
119+
public function getOriginalValue(string $attrName);
103120
}

0 commit comments

Comments
 (0)