File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 44
55namespace ComplexHeart \Domain \Model \Traits ;
66
7+ use Closure ;
8+
79use 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 /**
Original file line number Diff line number Diff line change 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 ' );
You can’t perform that action at this time.
0 commit comments