@@ -8,7 +8,7 @@ public function __construct(
88 public string $ type ,
99 public string $ uuid ,
1010 public Manifest $ manifest ,
11- public $ meta ,
11+ public mixed $ meta ,
1212 public string $ exporterClass ,
1313 public string $ modelClass ,
1414 public array $ fallbackMatches ,
@@ -45,45 +45,67 @@ public static function fromArray(array $array, Manifest $manifest)
4545 }, $ array );
4646 }
4747
48- public function __get ($ property )
48+ public function __get (string $ property )
4949 {
5050 $ asset = $ this ->manifest ->get ($ this ->uuid );
51+ $ value = null ;
5152
5253 if ($ property === 'model ' && !$ asset ) {
53- // Attempt to reconstruct discarded model if it exists on the target instance
54- $ assetInfo = [
55- 'model ' => $ this ->modelClass ,
56- 'attributes ' => $ this ->fallbackMatches ,
57- ];
58-
59- list ($ _ , $ model ) = Manifest::getModel ($ this ->uuid , $ assetInfo , 'discard ' , $ this ->exporterClass , false );
60-
61- // Only return the model if it is persisted in the database
62- if ($ model && $ model ->exists ) {
63- return $ model ;
64- }
54+ $ value = $ this ->getDiscardedModel ();
55+ } elseif ($ property === 'mode ' ) {
56+ $ value = $ this ->getMode ($ asset );
57+ } elseif ($ property === 'name ' ) {
58+ $ value = $ this ->getName ($ asset );
59+ } elseif ($ asset ) {
60+ $ value = $ asset ->$ property ;
6561 }
6662
67- if ($ property === 'mode ' ) {
68- if ($ asset ) {
69- return $ asset ->mode ;
70- } else {
71- return 'discard ' ;
72- }
63+ return $ value ;
64+ }
65+
66+ private function getDiscardedModel ()
67+ {
68+ if ($ this ->canUseDiscardedDependentFinder ()) {
69+ return $ this ->exporterClass ::findDiscardedDependentModel ($ this );
7370 }
7471
75- if ($ property === 'name ' ) {
76- if ($ asset ) {
77- return $ asset ->getName ($ this ->model );
78- } else {
79- return '' ;
80- }
72+ return $ this ->findPersistedDiscardedModel ();
73+ }
74+
75+ private function canUseDiscardedDependentFinder (): bool
76+ {
77+ if (!method_exists ($ this ->exporterClass , 'findDiscardedDependentModel ' )) {
78+ return false ;
8179 }
8280
83- if (!$ asset ) {
84- return null ;
81+ if (!method_exists ( $ this -> exporterClass , ' shouldFindDiscardedDependentModel ' ) ) {
82+ return true ;
8583 }
8684
87- return $ asset ->$ property ;
85+ return $ this ->exporterClass ::shouldFindDiscardedDependentModel ($ this );
86+ }
87+
88+ private function findPersistedDiscardedModel ()
89+ {
90+ // Attempt to reconstruct discarded model if it exists on the target instance
91+ $ assetInfo = [
92+ 'model ' => $ this ->modelClass ,
93+ 'attributes ' => $ this ->fallbackMatches ,
94+ ];
95+
96+ [, $ model ] = Manifest::getModel ($ this ->uuid , $ assetInfo , 'discard ' , $ this ->exporterClass , false );
97+
98+ // Only return the model if it is persisted in the database
99+ return $ model && $ model ->exists ? $ model : null ;
100+ }
101+
102+ private function getMode (mixed $ asset ): string
103+ {
104+ return $ asset ? $ asset ->mode : 'discard ' ;
105+ }
106+
107+ private function getName (mixed $ asset ): string
108+ {
109+ return $ asset ? $ asset ->getName ($ this ->model ) : '' ;
88110 }
89111}
0 commit comments