1 | <?php |
||
9 | abstract class ResourceCommand extends Command |
||
10 | { |
||
11 | /** |
||
12 | * Resource ID. |
||
13 | */ |
||
14 | protected $resourceId; |
||
15 | |||
16 | /** |
||
17 | * Resource path. |
||
18 | * |
||
19 | * @return string |
||
20 | */ |
||
21 | abstract public function resourcePath(); |
||
22 | |||
23 | /** |
||
24 | * Resource class name. |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | abstract public function resourceClass(); |
||
29 | |||
30 | /** |
||
31 | * Determines if current command is list command. |
||
32 | * |
||
33 | * @return bool |
||
34 | */ |
||
35 | protected function isListCommand(): bool |
||
39 | |||
40 | /** |
||
41 | * Command name. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function command() |
||
49 | |||
50 | /** |
||
51 | * HTTP request method. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function requestMethod() |
||
63 | |||
64 | /** |
||
65 | * HTTP request URL. |
||
66 | * |
||
67 | * @param \Laravel\Forge\Contracts\ResourceContract $resource |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function requestUrl(ResourceContract $resource) |
||
81 | |||
82 | /** |
||
83 | * Set resource ID. |
||
84 | * |
||
85 | * @param int|string $resourceId |
||
86 | * |
||
87 | * @return static |
||
88 | */ |
||
89 | public function setResourceId($resourceId) |
||
95 | |||
96 | /** |
||
97 | * Get resource ID. |
||
98 | * |
||
99 | * @return int|string|null |
||
100 | */ |
||
101 | public function getResourceId() |
||
105 | |||
106 | /** |
||
107 | * Handle command response. |
||
108 | * |
||
109 | * @param \Psr\Http\Message\ResponseInterface $response |
||
110 | * @param \Laravel\Forge\Contracts\ResourceContract $owner |
||
111 | * |
||
112 | * @return \Laravel\Forge\Contracts\ResourceContract|array|bool|string |
||
113 | */ |
||
114 | public function handleResponse(ResponseInterface $response, ResourceContract $owner) |
||
124 | |||
125 | /** |
||
126 | * List response handler. |
||
127 | * |
||
128 | * @param \Psr\Http\Message\ResponseInterface $response |
||
129 | * @param \Laravel\Forge\Contracts\ResourceContract $owner |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | public function handleListCommandResponse(ResponseInterface $response, ResourceContract $owner) |
||
154 | } |
||
155 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: