2626 */
2727trait StatefullyLoadsAttributes
2828{
29- use ConditionallyLoadsAttributes;
29+ use ConditionallyLoadsAttributes, ResolvesState ;
3030
3131 /**
3232 * Retrieve a value if the current state matches the given state.
3333 *
34- * @param ResourceState $state
34+ * @param string| ResourceState $state
3535 * @param mixed $value
3636 * @param mixed $default
3737 * @return \Illuminate\Http\Resources\MissingValue|mixed
3838 */
3939 protected function whenState ($ state , $ value , $ default = null )
4040 {
41+ $ state = $ this ->resolveState ($ state );
42+
4143 if (func_num_args () === 3 ) {
4244 return $ this ->when ($ this ->getState () === $ state , $ value , $ default );
4345 }
@@ -48,13 +50,15 @@ protected function whenState($state, $value, $default = null)
4850 /**
4951 * Retrieve a value unless the current state matches the given state.
5052 *
51- * @param ResourceState $state
53+ * @param string| ResourceState $state
5254 * @param mixed $value
5355 * @param mixed $default
5456 * @return \Illuminate\Http\Resources\MissingValue|mixed
5557 */
5658 protected function unlessState ($ state , $ value , $ default = null )
5759 {
60+ $ state = $ this ->resolveState ($ state );
61+
5862 if (func_num_args () === 3 ) {
5963 return $ this ->unless ($ this ->getState () === $ state , $ value , $ default );
6064 }
@@ -65,13 +69,15 @@ protected function unlessState($state, $value, $default = null)
6569 /**
6670 * Retrieve a value if the current state is one of the given states.
6771 *
68- * @param array<ResourceState> $states
72+ * @param array<string| ResourceState> $states
6973 * @param mixed $value
7074 * @param mixed $default
7175 * @return \Illuminate\Http\Resources\MissingValue|mixed
7276 */
7377 protected function whenStateIn (array $ states , $ value , $ default = null )
7478 {
79+ $ states = array_map (fn ($ state ) => $ this ->resolveState ($ state ), $ states );
80+
7581 $ condition = in_array ($ this ->getState (), $ states , true );
7682
7783 if (func_num_args () === 3 ) {
@@ -84,13 +90,15 @@ protected function whenStateIn(array $states, $value, $default = null)
8490 /**
8591 * Retrieve a value unless the current state is one of the given states.
8692 *
87- * @param array<ResourceState> $states
93+ * @param array<string| ResourceState> $states
8894 * @param mixed $value
8995 * @param mixed $default
9096 * @return \Illuminate\Http\Resources\MissingValue|mixed
9197 */
9298 protected function unlessStateIn (array $ states , $ value , $ default = null )
9399 {
100+ $ states = array_map (fn ($ state ) => $ this ->resolveState ($ state ), $ states );
101+
94102 $ condition = in_array ($ this ->getState (), $ states , true );
95103
96104 if (func_num_args () === 3 ) {
@@ -103,13 +111,15 @@ protected function unlessStateIn(array $states, $value, $default = null)
103111 /**
104112 * Merge a value if the current state matches the given state.
105113 *
106- * @param ResourceState $state
114+ * @param string| ResourceState $state
107115 * @param mixed $value
108116 * @param mixed $default
109117 * @return \Illuminate\Http\Resources\MergeValue|mixed
110118 */
111119 protected function mergeWhenState ($ state , $ value , $ default = null )
112120 {
121+ $ state = $ this ->resolveState ($ state );
122+
113123 if (func_num_args () === 3 ) {
114124 return $ this ->mergeWhen ($ this ->getState () === $ state , $ value , $ default );
115125 }
@@ -120,13 +130,15 @@ protected function mergeWhenState($state, $value, $default = null)
120130 /**
121131 * Merge a value unless the current state matches the given state.
122132 *
123- * @param ResourceState $state
133+ * @param string| ResourceState $state
124134 * @param mixed $value
125135 * @param mixed $default
126136 * @return \Illuminate\Http\Resources\MergeValue|mixed
127137 */
128138 protected function mergeUnlessState ($ state , $ value , $ default = null )
129139 {
140+ $ state = $ this ->resolveState ($ state );
141+
130142 if (func_num_args () === 3 ) {
131143 return $ this ->mergeUnless ($ this ->getState () === $ state , $ value , $ default );
132144 }
@@ -138,7 +150,7 @@ protected function mergeUnlessState($state, $value, $default = null)
138150 * Get the current state of the resource.
139151 * This method should be implemented by the class using this trait.
140152 */
141- abstract protected function getState (): ResourceState ;
153+ abstract protected function getState (): string ;
142154
143155 public function __call ($ method , $ parameters )
144156 {
0 commit comments