Skip to content

Commit 32b61a5

Browse files
author
František Mazura
committed
readme update
add touched add ignored add interface tests update
1 parent 3586ba3 commit 32b61a5

File tree

7 files changed

+178
-33
lines changed

7 files changed

+178
-33
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
# php-value-objects
2+
23
PHP Value object for storing valid data.
4+
5+
# Examples
6+
7+
- check `tests` dir
8+
9+
# TODO
10+
11+
- [x] add tests
12+
- [x] ignored attributes
13+
- [x] touched attributes
14+
- [x] interface
15+
- [ ] Auto-generated OpenAPI
16+
- [ ] Auto-generated TypeScripts objects
17+
- [ ] yaml definition of ValueObjects
18+
- [ ] parent (possibility to get parent object)
19+
20+
# Changelog
21+
22+
- 2021-02-26
23+
- ignored attributes
24+
- touch (`setTouched`, `isTouched`, `getTouchedAll`)
25+
- add interface `ValueObjectInterface`

src/AbstractValueObject.php

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,63 @@
44

55
use OG\ValueObjects\Exceptions\ValidatorException;
66
use OG\ValueObjects\Interfaces\GlobalValidatorInterface;
7+
use OG\ValueObjects\Interfaces\ValueObjectInterface;
78
use Throwable;
89

9-
abstract class AbstractValueObject
10+
abstract class AbstractValueObject implements ValueObjectInterface
1011
{
12+
private $touched = [];
13+
private $ignoredAttrs = ["ignoredAttrs", "touched"];
14+
15+
public function isIgnoredAttr(string $attrName): bool
16+
{
17+
return in_array($attrName, $this->ignoredAttrs);
18+
}
1119

1220
/**
13-
* setter for attribute of ValueObject
14-
*
15-
* @param string $attrName
16-
* @param string $attrSetter
17-
* @param $attrValue
18-
* @param object $valueObject
19-
* @return mixed
20-
* @throws ValidatorException
21+
* @inheritDoc
2122
*/
22-
abstract public function setValue(string $attrName, string $attrSetter, $attrValue, object $valueObject);
23+
public function setTouched(string $attrName): void
24+
{
25+
$this->touched[$attrName] = true;
26+
}
2327

2428
/**
25-
* @param Throwable $throwable
26-
* @param string $attrName
27-
* @param string $attrSetter
28-
* @param $attrValue
29-
* @param object $valueObject
30-
* @return mixed
29+
* @inheritDoc
3130
*/
32-
abstract public function getAttributeErrorMessage(Throwable $throwable, string $attrName, string $attrSetter, $attrValue, object $valueObject): string;
31+
public function isTouched(string $attrName): bool
32+
{
33+
return $this->touched[$attrName];
34+
}
3335

3436
/**
35-
* @param object|array $data
36-
* @return void
37-
* @throws ValidatorException
37+
* @inheritDoc
38+
*/
39+
public function getTouchedAll(): array
40+
{
41+
return $this->touched;
42+
}
43+
44+
/**
45+
* @inheritDoc
3846
*/
3947
public function init($data): void
4048
{
4149
if (is_object($data)) {
4250
$data = get_object_vars($data);
4351
}
52+
// foreach get data
4453
foreach ($data as $name => $item) {
4554
$setter = "set" . ucfirst($name);
4655
$value = $data[$name] ?? null;
56+
$this->setTouched($name);
4757
try {
48-
$this->setValue($name, $setter, $value, $this);
58+
if (method_exists($this, $setter)) {
59+
$this->setValue($name, $setter, $value, $this);
60+
} else {
61+
// attribute setter is missing
62+
$this->handleUndefinedAttribute($name, $setter, $value, $this);
63+
}
4964
} catch (Throwable $e) {
5065
$msg = $this->getAttributeErrorMessage($e, $name, $setter, $value, $this);
5166
$code = $this->getAttributeErrorCode($e, $name, $setter, $value, $this);
@@ -59,28 +74,24 @@ public function init($data): void
5974
}
6075

6176
/**
62-
* @param Throwable $throwable
63-
* @param string $attrName
64-
* @param string $attrSetter
65-
* @param $attrValue
66-
* @param object $valueObject
67-
* @return int
77+
* @inheritDoc
6878
*/
6979
public function getAttributeErrorCode(Throwable $throwable, string $attrName, string $attrSetter, $attrValue, object $valueObject): int
7080
{
7181
return 0;
7282
}
7383

7484
/**
75-
* Transform value object to array
76-
*
77-
* @return array
85+
* @inheritDoc
7886
*/
7987
public function toArray(): array
8088
{
8189
$arr = get_object_vars($this);
8290
$out = [];
8391
foreach ($arr as $name => $item) {
92+
if ($this->isIgnoredAttr($name)) {
93+
continue;
94+
}
8495
// ValueObject element
8596
if ($item instanceof AbstractValueObject) {
8697
$out[$name] = $item->toArray();
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
4+
namespace OG\ValueObjects\Interfaces;
5+
6+
7+
use OG\ValueObjects\Exceptions\ValidatorException;
8+
use Throwable;
9+
10+
interface ValueObjectInterface
11+
{
12+
13+
/**
14+
* Init object from array or object
15+
*
16+
* @param object|array $data
17+
* @return void
18+
* @throws ValidatorException
19+
*/
20+
public function init($data): void;
21+
22+
/**
23+
* setter for attribute of ValueObject
24+
*
25+
* @param string $attrName
26+
* @param string $attrSetter
27+
* @param $attrValue
28+
* @param object $valueObject
29+
* @return mixed
30+
* @throws ValidatorException
31+
*/
32+
public function setValue(string $attrName, string $attrSetter, $attrValue, object $valueObject): void;
33+
34+
/**
35+
* @param Throwable $throwable
36+
* @param string $attrName
37+
* @param string $attrSetter
38+
* @param $attrValue
39+
* @param object $valueObject
40+
* @return mixed
41+
*/
42+
public function getAttributeErrorMessage(Throwable $throwable, string $attrName, string $attrSetter, $attrValue, object $valueObject): string;
43+
44+
/**
45+
* @param Throwable $throwable
46+
* @param string $attrName
47+
* @param string $attrSetter
48+
* @param $attrValue
49+
* @param object $valueObject
50+
* @return int
51+
*/
52+
public function getAttributeErrorCode(Throwable $throwable, string $attrName, string $attrSetter, $attrValue, object $valueObject): int;
53+
54+
/**
55+
* transform instance to array
56+
* @return array
57+
*/
58+
public function toArray(): array;
59+
60+
61+
/**
62+
* setter for undefined attributes
63+
*
64+
* @param string $attrName
65+
* @param string $attrSetter
66+
* @param $attrValue
67+
* @param object $valueObject
68+
* @return mixed
69+
* @throws ValidatorException
70+
*/
71+
public function handleUndefinedAttribute(string $attrName, string $attrSetter, $attrValue, object $valueObject): void;
72+
73+
/**
74+
* set attribute touch
75+
*
76+
* @param string $attrName
77+
* @return void
78+
*/
79+
public function setTouched(string $attrName): void;
80+
81+
/**
82+
* check if attribute touched
83+
*
84+
* @param string $attrName
85+
* @return bool
86+
*/
87+
public function isTouched(string $attrName): bool;
88+
89+
/**
90+
* get all touched attributes
91+
*
92+
* @return array
93+
*/
94+
public function getTouchedAll(): array;
95+
96+
/**
97+
* check if attribute is ignored
98+
*
99+
* @param string $attrName
100+
* @return bool
101+
*/
102+
public function isIgnoredAttr(string $attrName): bool;
103+
}

src/Traits/InitAndValidateTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
trait InitAndValidateTrait
1818
{
19-
public function setValue(string $attrName, string $attrSetter, $attrValue, object $obj)
19+
public function setValue(string $attrName, string $attrSetter, $attrValue, object $obj): void
2020
{
2121
$reflectionParameter = self::getReflectionParameter($obj, $attrSetter);
2222
// build-in types

tests/ValueObjectTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function testTest(): void
1414
$data = [
1515
"name" => "My car",
1616
"enginePower" => "1000",
17+
"enginePower1" => "1000",
1718
"wheels" => [
1819
["size" => 5],
1920
["size" => null],
@@ -39,6 +40,8 @@ public function testTest(): void
3940
];
4041

4142
$this->assertEquals($expactOutput, $car->toArray());
43+
44+
$this->assertTrue($car->isTouched("name"));
4245
}
4346
}
4447

tests/ValueObjects/AbstractMyValueObject.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
use OG\ValueObjects\Traits\InitAndValidateTrait;
77
use Throwable;
88

9-
class AbstractMyValueObject extends AbstractValueObject
9+
abstract class AbstractMyValueObject extends AbstractValueObject
1010
{
1111
use InitAndValidateTrait;
1212

1313
public function getAttributeErrorMessage(Throwable $throwable, string $attrName, string $attrSetter, $attrValue, object $valueObject): string
1414
{
1515
return "Error " . $attrName . " in " . get_class($valueObject) . " data <" . print_r($attrValue, true) . ">";
1616
}
17+
18+
public function handleUndefinedAttribute(string $attrName, string $attrSetter, $attrValue, object $valueObject): void
19+
{
20+
// ignore
21+
return;
22+
}
1723
}

tests/ValueObjects/CarValueObject.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,4 @@ public function setWheels(WheelValueObject ...$wheels): CarValueObject
7272
$this->wheels = $wheels;
7373
return $this;
7474
}
75-
7675
}

0 commit comments

Comments
 (0)