2424use  Illuminate \Database \Eloquent \Relations \MorphOne  as  EloquentMorphOne ;
2525use  LaravelJsonApi \Eloquent \Contracts \FillableToOne ;
2626use  LaravelJsonApi \Eloquent \Fields \Concerns \IsReadOnly ;
27- use  LogicException ;
2827
2928class  HasOne extends  ToOne implements  FillableToOne
3029{
31- 
30+      /** @var int */ 
3231    private  const  KEEP_DETACHED_MODEL  = 0 ;
32+ 
33+     /** @var int */ 
3334    private  const  DELETE_DETACHED_MODEL  = 1 ;
35+ 
36+     /** @var int */ 
3437    private  const  FORCE_DELETE_DETACHED_MODEL  = 2 ;
3538
3639    use  IsReadOnly;
@@ -103,14 +106,24 @@ public function mustExist(): bool
103106     */ 
104107    public  function  fill (Model   $ model , ?array  $ identifier ): void 
105108    {
106-         $ relation   = $ model ->{ $ this  ->relationName ()} ();
109+         $ name   = $ this  ->relationName ();
107110
108-         if  (!$ relation  instanceof  EloquentHasOne && !$ relation  instanceof  EloquentMorphOne) {
109-             throw  new  LogicException ('Expecting an Eloquent has-one or morph-one relation. ' );
110-         }
111+         assert (method_exists ($ model , $ name ), sprintf (
112+             'Expecting method %s to exist on model %s. ' ,
113+             $ name ,
114+             $ model ::class,
115+         ));
116+ 
117+         $ relation  = $ model ->{$ name }();
118+ 
119+         assert ($ relation  instanceof  EloquentHasOne || $ relation  instanceof  EloquentMorphOne, sprintf (
120+             'Expecting method %s on model %s to return a belongs-to-many relation. ' ,
121+             $ name ,
122+             $ model ::class,
123+         ));
111124
112125        /** @var Model|null $current */ 
113-         $ current  = $ model ->{$ this -> relationName () };
126+         $ current  = $ model ->{$ name  };
114127        $ related  = $ this  ->find ($ identifier );
115128
116129        if  ($ this  ->willChange ($ current , $ related )) {
@@ -150,7 +163,7 @@ private function willChange(?Model $current, ?Model $new): bool
150163     * @param EloquentMorphOne|EloquentHasOne $relation 
151164     * @param Model $current 
152165     */ 
153-     private  function  disassociate ($ relation , Model   $ current ): void 
166+     private  function  disassociate (EloquentMorphOne | EloquentHasOne   $ relation , Model   $ current ): void 
154167    {
155168        if  (self ::KEEP_DETACHED_MODEL  === $ this  ->detachMode ) {
156169            $ this  ->setInverseToNull ($ relation , $ current );
@@ -163,11 +176,11 @@ private function disassociate($relation, Model $current): void
163176    /** 
164177     * Disassociate the related model by setting the relationship column(s) to `null`. 
165178     * 
166-      * @param $relation 
179+      * @param EloquentMorphOne|EloquentHasOne  $relation 
167180     * @param Model $current 
168181     * @return void 
169182     */ 
170-     private  function  setInverseToNull ($ relation , Model   $ current ): void 
183+     private  function  setInverseToNull (EloquentMorphOne | EloquentHasOne   $ relation , Model   $ current ): void 
171184    {
172185        if  ($ relation  instanceof  EloquentMorphOne) {
173186            $ current ->setAttribute ($ relation ->getMorphType (), null );
0 commit comments