Skip to content

Commit e7c1f8a

Browse files
authored
Feature/Add map values functionality (#14)
1 parent e60a69f commit e7c1f8a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Traits/HasAttributes.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace ComplexHeart\Domain\Model\Traits;
66

7+
use Closure;
8+
79
use function Lambdish\Phunctional\map;
810

911
/**
@@ -34,14 +36,20 @@ final public static function attributes(): array
3436
*
3537
* @return array<string, mixed>
3638
*/
37-
final public function values(): array
39+
final public function values(Closure $fn = null): array
3840
{
3941
$allowed = static::attributes();
4042

41-
return array_intersect_key(
43+
$attributes = array_intersect_key(
4244
get_object_vars($this),
4345
array_combine($allowed, $allowed)
4446
);
47+
48+
if (is_callable($fn)) {
49+
return map($fn, $attributes);
50+
}
51+
52+
return $attributes;
4553
}
4654

4755
/**

tests/ModelTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use ComplexHeart\Domain\Model\Test\OrderManagement\Domain\Price;
4+
5+
test('Model values should be mapped by custom function sucessfully', function() {
6+
$price = new Price(10, 'AUD');
7+
8+
$values = $price->values(fn($attribute) => "-->$attribute");
9+
10+
expect($values['amount'])->toStartWith('-->');
11+
expect($values['currency'])->toStartWith('-->');
12+
})
13+
->group('Unit');

0 commit comments

Comments
 (0)