Skip to content

Commit bfd25f8

Browse files
committed
Adds docblocks to ImagePicker component
1 parent a3fde80 commit bfd25f8

1 file changed

Lines changed: 88 additions & 3 deletions

File tree

app/Http/Livewire/ImagePicker.php

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,108 @@ class ImagePicker extends Component
1212
{
1313
use WithFileUploads, HasImageUploads, AuthorizesRequests;
1414

15+
/**
16+
* Uploaded image file
17+
* @var $image
18+
*/
1519
public $image;
20+
21+
/**
22+
* Existing image URL (if the model already has an image)
23+
* @var string|null
24+
*/
1625
public $existing_image_url;
26+
27+
/**
28+
* A kebab-case unique identifier for each component
29+
* @var string
30+
* for the Livewire wire:key directive to identify the DOM elements when multiple instances of the component exist
31+
* Example: "banner-img"
32+
*/
1733
public $custom_key;
34+
35+
/**
36+
* Additional instructions for the image file selection
37+
* @var string|null
38+
* Example: "This photo will appear on your profile page and as your application profile image - please use a high-quality image (300x300 pixels or larger)."
39+
*
40+
*/
1841
public $custom_msg;
42+
43+
/**
44+
* Model instance that has an image is associated
45+
* @var \Illuminate\Database\Eloquent\Model
46+
*/
1947
public $model;
48+
49+
/**
50+
* Method on the model used to save the image
51+
* @var string
52+
*/
2053
public $save_function;
54+
55+
/**
56+
* Parameters passed to the save function
57+
* @var array
58+
*/
2159
public $save_params;
60+
61+
/**
62+
* The parameter name expected by the save function for the uploaded image
63+
* @var string
64+
*/
2265
public $image_param_name;
66+
67+
/**
68+
* Callback function to execute after saving the image
69+
* @var string|null
70+
*/
2371
public $callback_function;
72+
73+
/**
74+
* Parameters for the callback function
75+
* @var array
76+
*/
2477
public $callback_params;
78+
79+
/**
80+
* Additional blade partial view to display below the image preview
81+
* @var string|null
82+
*/
2583
public $partial_view;
84+
85+
/**
86+
* Route to redirect to after saving the image
87+
* @var string
88+
*/
2689
public $redirect_route;
90+
91+
/**
92+
* Flash message displayed after saving the image
93+
* @var string|null
94+
*/
2795
public $message;
96+
97+
/**
98+
* Authorization parameters for checking user permissions
99+
* @var array $auth_params
100+
*/
28101
public $auth_params;
29102

30103
public function mount()
31104
{
32-
if (isset($this->save_function)) {
105+
if (isset($this->save_function)) { // Validates save function only if set
33106
$this->validateCallUserFunc('save_function');
34107
}
35108

36-
if (isset($this->callback_function)) {
109+
if (isset($this->callback_function)) { // Validates callback function only if set
37110
$this->validateCallUserFunc('callback_function');
38111
}
39112
}
40113

114+
/**
115+
* Validates the image after a file has been selected and stores the updated image in the save function parameters for uploading
116+
*/
41117
public function updatedImage()
42118
{
43119
if ($this->image) {
@@ -53,17 +129,26 @@ public function save()
53129
{
54130
$this->authorize(...$this->auth_params);
55131

132+
// Calls the model's save function if an image is uploaded
56133
if ($this->image) {
57134
$this->message = call_user_func([$this->model, $this->save_function], ...$this->save_params ?? []);
58135
}
59136

137+
// If a callback function is set, execute it (e.g., updating related records)
60138
if (isset($this->callback_function)) {
61139
call_user_func([$this->model, $this->callback_function], ...$this->callback_params ?? []);
62140
}
63141

64142
return redirect($this->redirect_route)->with('flash_message', $this->message);
65143
}
66144

145+
/**
146+
* Validates that the provided method name exists on the model
147+
* Prevents runtime errors if an invalid method is supplied
148+
*
149+
* @param string $function_name
150+
* Name of the function to validate
151+
*/
67152
private function validateCallUserFunc($function_name)
68153
{
69154
$class_name = get_class($this->model);
@@ -81,8 +166,8 @@ function ($attribute, $value, $fail) use ($class_name) {
81166

82167
$function_name => [
83168
function ($attribute, $value, $fail) use ($class_name) {
84-
/** @var string|null $value */
85169
if (!is_callable($value, true) || !method_exists($class_name, $value)) {
170+
/** @var string|null $value */
86171
$fail('Please contact the app admin.');
87172
logger()->error("The method {$value} does not exist in {$class_name}.");
88173
}

0 commit comments

Comments
 (0)