Skip to content

Commit 6b2f460

Browse files
committed
Add custom label to the field
1 parent 5bf9fea commit 6b2f460

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/Fields/Field.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class Field extends OrganicField implements JsonSerializable
107107
*/
108108
public $afterUpdateCallback;
109109

110+
public $label;
111+
110112
/**
111113
* Create a new field.
112114
*
@@ -117,6 +119,8 @@ public function __construct($attribute, callable $resolveCallback = null)
117119
{
118120
$this->attribute = $attribute;
119121

122+
$this->label = $attribute;
123+
120124
$this->resolveCallback = $resolveCallback;
121125

122126
$this->default(null);
@@ -313,7 +317,7 @@ public function resolveForShow($repository, $attribute = null)
313317
return;
314318
}
315319

316-
if (! $this->showCallback) {
320+
if (!$this->showCallback) {
317321
$this->resolve($repository, $attribute);
318322
} elseif (is_callable($this->showCallback)) {
319323
tap($this->value ?? $this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
@@ -336,7 +340,7 @@ public function resolveForIndex($repository, $attribute = null)
336340
return;
337341
}
338342

339-
if (! $this->indexCallback) {
343+
if (!$this->indexCallback) {
340344
$this->resolve($repository, $attribute);
341345
} elseif (is_callable($this->indexCallback)) {
342346
tap($this->value ?? $this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
@@ -356,7 +360,7 @@ public function resolve($repository, $attribute = null)
356360
return;
357361
}
358362

359-
if (! $this->resolveCallback) {
363+
if (!$this->resolveCallback) {
360364
$this->value = $this->resolveAttribute($repository, $attribute);
361365
} elseif (is_callable($this->resolveCallback)) {
362366
tap($this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
@@ -388,16 +392,23 @@ public function jsonSerialize()
388392
{
389393
return with(app(RestifyRequest::class), function ($request) {
390394
return [
391-
'attribute' => $this->attribute,
395+
'attribute' => $this->label ?? $this->attribute,
392396
'value' => $this->resolveDefaultValue($request) ?? $this->value,
393397
];
394398
});
395399
}
396400

401+
public function label($label)
402+
{
403+
$this->label = $label;
404+
405+
return $this;
406+
}
407+
397408
public function serializeToValue($request)
398409
{
399410
return [
400-
$this->attribute => $this->resolveDefaultValue($request) ?? $this->value,
411+
$this->label ?? $this->attribute => $this->resolveDefaultValue($request) ?? $this->value,
401412
];
402413
}
403414

tests/Controllers/RepositoryUpdateControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public function test_do_not_update_fields_without_permission()
7676
$response = $this->putJson('/restify-api/post-with-unathorized-fields/'.$post->id, [
7777
'title' => 'Updated title',
7878
'user_id' => 2,
79-
])->assertStatus(200);
79+
])
80+
->dump()
81+
->assertStatus(200);
8082

8183
$this->assertEquals('Title', $response->json('data.attributes.title'));
8284
$this->assertEquals(2, $response->json('data.attributes.user_id'));

tests/Unit/FieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,13 @@ public function test_field_after_update_called()
240240

241241
$field->invokeAfter($request, $model);
242242
}
243+
244+
public function test_field_can_have_custom_label()
245+
{
246+
$field = Field::make('name')->label('custom_label');
247+
248+
$field->resolveForIndex((object) ['name' => 'Binaryk'], 'name');
249+
250+
$this->assertEquals('custom_label', $field->label);
251+
}
243252
}

0 commit comments

Comments
 (0)