Skip to content

Commit

Permalink
Create general info serializer and transformer [MA-144]
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgarwood committed Nov 10, 2023
1 parent 7c87284 commit 6bc024b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Models/Transformers/GeneralInfoTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models\Transformers;

use A17\Twill\Models\Contracts\TwillModelContract;
use League\Fractal\TransformerAbstract;

class GeneralInfoTransformer extends TransformerAbstract
{
public function transform(TwillModelContract $label)
{
return [
$label->key => $label->text,
];
}
}
25 changes: 25 additions & 0 deletions app/Repositories/Serializers/GeneralInfoSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Repositories\Serializers;

use App\Models\Transformers\GeneralInfoTransformer;
use League\Fractal\Manager;
use League\Fractal\Resource;
use League\Fractal\Serializer\ArraySerializer;

class GeneralInfoSerializer
{
protected ?Manager $manager = null;

public function __construct()
{
$this->manager = new Manager();
$this->manager->setSerializer(new AssociativeArraySerializer());
}

public function serialize($labels)
{
$resource = new Resource\Collection($labels, new GeneralInfoTransformer(), 'general_info');
return $this->manager->createData($resource)->toArray();
}
}
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
return $serializer->serialize($galleries);
});

Route::get('/general_info', function () {
$repository = App::make(LabelRepository::class);
$labels = $repository->getBaseModel()->newQuery()->get();
$serializer = new GeneralInfoSerializer();
return $serializer->serialize($labels);
});

Route::get('/messages', function () {
return ['messages' => []]; // Legacy from Drupal
});
Expand Down

0 comments on commit 6bc024b

Please sign in to comment.