Setup model and json-api for morphology legends

This commit is contained in:
Leonard Smith 2021-04-13 21:35:21 -05:00
parent 3ac05e3d4d
commit 35f18dc595
8 changed files with 169 additions and 1 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace App\JsonApi\MorphLegends;
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class Adapter extends AbstractAdapter
{
/**
* Mapping of JSON API attribute field names to model keys.
*
* @var array
*/
protected $attributes = [];
/**
* Mapping of JSON API filter names to model scopes.
*
* @var array
*/
protected $filterScopes = [];
/**
* Adapter constructor.
*
* @param StandardStrategy $paging
*/
public function __construct(StandardStrategy $paging)
{
parent::__construct(new \App\MorphLegend(), $paging);
}
/**
* @param Builder $query
* @param Collection $filters
* @return void
*/
protected function filter($query, Collection $filters)
{
$this->filterWithScopes($query, $filters);
}
/**
* @return \CloudCreativity\LaravelJsonApi\Eloquent\HasMany
*/
protected function words()
{
return $this->hasMany();
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\JsonApi\MorphLegends;
use Neomerx\JsonApi\Schema\SchemaProvider;
/**
* Schema.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 4/11/21
* Time: 12:28 PM
*/
class Schema extends SchemaProvider
{
/**
* @var string
*/
protected $resourceType = 'morph-legends';
/**
* @param \App\MorphLegend $resource
* the domain record being serialized.
* @return string
*/
public function getId($resource)
{
return (string)$resource->getRouteKey();
}
/**
* @param \App\MorphLegend $resource
* the domain record being serialized.
* @return array
*/
public function getAttributes($resource)
{
return [
'morph' => $resource->morph,
'description' => $resource->description,
];
}
/**
* @param object $resource
* @param bool $isPrimary
* @param array $includeRelationships
* @return array|\bool[][]
*/
public function getRelationships($resource, $isPrimary, array $includeRelationships)
{
return [
'words' => [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
]
];
}
}

View File

@ -59,4 +59,12 @@ class Adapter extends AbstractAdapter
{
return $this->belongsTo();
}
/**
* @return \CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo
*/
protected function morphLegend()
{
return $this->belongsTo();
}
}

View File

@ -62,7 +62,15 @@ class Schema extends SchemaProvider
self::DATA => function () use ($resource) {
return $resource->lexical_entry;
}
]
],
'morph-legends' => [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
self::SHOW_DATA => isset($includeRelationships['morph-legends']),
self::DATA => function () use ($resource) {
return $resource->morph_legend;
}
],
];
}
}

27
app/MorphLegend.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class MorphLegend extends Model
{
public $incrementing = false;
public $table = 'morph_legend';
public $primaryKey = 'morph';
public $visible = [
'morph',
'description',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function words()
{
return $this->hasMany(Word::class, 'morph', 'morph');
}
}

View File

@ -42,4 +42,12 @@ class Word extends Model
{
return $this->belongsTo(LexicalEntry::class, 'strongs_number', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function morphLegend()
{
return $this->belongsTo(MorphLegend::class, 'morph', 'morph)');
}
}

View File

@ -71,6 +71,7 @@ return [
'verses' => \App\Verse::class,
'words' => \App\Word::class,
'lexical-entries' => \App\LexicalEntry::class,
'morph-legends' => \App\MorphLegend::class,
],
/*

View File

@ -37,9 +37,14 @@ JsonApi::register('default')->routes(function ($api) {
$api->resource('words')->readOnly()->relationships(function ($relations) {
$relations->hasOne('lexical-entry')->readOnly();
$relations->hasOne('morph-legend')->readOnly();
});
$api->resource('lexical-entries')->readOnly()->relationships(function ($relations) {
$relations->hasMany('words')->readOnly();
});
$api->resource('morph-legends')->readOnly()->relationships(function ($relations) {
$relations->hasMany('words')->readOnly();
});
});