Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

_No Planned Updates_

## [1.3.2] - 2020-01-11
### Fixed
- Issue-2: Can now paginate HasMany relationship.

## [1.3.1] - 2020-01-11
### Fixed
- Issue-4: Fixed pagination
Expand Down
16 changes: 13 additions & 3 deletions src/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
Expand All @@ -20,6 +26,7 @@
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\Response as SymResponse;

Expand Down Expand Up @@ -172,7 +179,7 @@ private function iterateThroughChildren(Model $model, Request $request)
// Loop through the parts.
foreach ($parts as $part) {
// Look for an array accessor, "children[5]" for example.
preg_match('/\[(\d+)\]$/', $part, $matches);
preg_match('/\[(\d+)]$/', $part, $matches);
$offset = false;

// If one was found, save the offset and remove it from $part.
Expand All @@ -184,7 +191,8 @@ private function iterateThroughChildren(Model $model, Request $request)
$model = $model->$part();

// If it's a relationship, see if it's paginate-able.
if (method_exists($model, 'paginate')) {
if (stripos(get_class($model), 'Many') !== false) {
/** @var BelongsToMany|HasMany|HasOneOrMany|MorphMany|MorphOneOrMany|MorphToMany $model */
$model = $model->paginate();
} elseif ($model instanceof Relation) {
$model = $model->getResults();
Expand Down Expand Up @@ -299,8 +307,10 @@ private function redirectToShowRoute(Request $request, $data)
return null;
}

$key = Str::singular(str_replace('-', '_', $topLevel));

return redirect(route("$topLevel.show", [
str_replace('-', '_', $topLevel) => $data->id
$key => $data->id
]));
}

Expand Down
2 changes: 0 additions & 2 deletions src/Concerns/HasLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace TomHart\Restful\Concerns;

use Illuminate\Support\Collection;

interface HasLinks
{

Expand Down
11 changes: 4 additions & 7 deletions src/Traits/HasLinksTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function buildLinks(): array
$router = app(Router::class);

foreach ($routes as $routePart) {
/** @var HasLinks $this */
$link = LinkBuilder::buildLink($this, $routePart, $router);

if ($link) {
Expand All @@ -81,7 +82,6 @@ public function buildLinks(): array
return $links;
}


/**
* Builds the links to create the relationship resources.
*
Expand Down Expand Up @@ -120,6 +120,7 @@ public function buildRelationshipLinks(): array

$createLink = LinkBuilder::buildLink($targetClass, 'create', $router);
$storeLink = LinkBuilder::buildLink($targetClass, 'store', $router);
/** @var HasLinks $this */
$viewLink = LinkBuilder::buildLink($this, 'show.extra', $router, $method);

$links[$method] = [
Expand All @@ -132,17 +133,13 @@ public function buildRelationshipLinks(): array
return $links;
}


/**
* Return the name for the resource route this model
* @return string|null
*/
public function getRouteName(): ?string
{
$name = $this->getRouteKey();
if (!$name) {
return null;
}
return Str::kebab(Str::studly($name));
$name = class_basename($this);
return Str::plural(Str::kebab(Str::studly($name)));
}
}
16 changes: 8 additions & 8 deletions tests/AccessingChildrenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function testIteratingThroughChildren(): void
$parent->children()->save($child2);

/** @var TestResponse $response1 */
$response1 = $this->get(route('model-parent.show.extra', [
'model_parent' => $parent->id,
$response1 = $this->get(route('model-parent-tests.show.extra', [
'model_parent_test' => $parent->id,
'extra' => 'children'
]), [
'Accept' => 'application/json'
Expand All @@ -47,8 +47,8 @@ public function testIteratingThroughChildren(): void
$this->assertCount(2, $data['data']);

/** @var TestResponse $response1 */
$response1 = $this->get(route('model-parent.show.extra', [
'model_parent' => $parent->id,
$response1 = $this->get(route('model-parent-tests.show.extra', [
'model_parent_test' => $parent->id,
'extra' => 'children[1]'
]), [
'Accept' => 'application/json'
Expand Down Expand Up @@ -81,8 +81,8 @@ public function testSingleRelationship(): void
$parent->save();

/** @var TestResponse $response1 */
$response1 = $this->get(route('model-parent.show.extra', [
'model_parent' => $parent->id,
$response1 = $this->get(route('model-parent-tests.show.extra', [
'model_parent_test' => $parent->id,
'extra' => 'child'
]), [
'Accept' => 'application/json'
Expand All @@ -107,8 +107,8 @@ public function testAccessingPropertyThrowsError(): void
$parent->save();

/** @var TestResponse $response1 */
$response1 = $this->get(route('model-parent.show.extra', [
'model_parent' => $parent->id,
$response1 = $this->get(route('model-parent-tests.show.extra', [
'model_parent_test' => $parent->id,
'extra' => 'name'
]), [
'Accept' => 'application/json'
Expand Down
29 changes: 29 additions & 0 deletions tests/Classes/Controllers/PostsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TomHart\Restful\Tests\Classes\Controllers;

use TomHart\Restful\AbstractRestfulController;
use TomHart\Restful\Tests\Classes\Models\Post;

class PostsController extends AbstractRestfulController
{

/**
* The views to render.
* @var string[]
*/
protected $views = [
'index' => 'index',
'show' => 'show',
'store' => 'store'
];

/**
* What Model class to search for entities.
* @return string
*/
protected function getModelClass(): string
{
return Post::class;
}
}
25 changes: 25 additions & 0 deletions tests/Classes/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TomHart\Restful\Tests\Classes\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use TomHart\Restful\Concerns\HasLinks;
use TomHart\Restful\Traits\HasLinksTrait;

/**
* @property int $id
* @property string $name
* @method find(int $id)
*/
class Comment extends Model implements HasLinks
{
use HasLinksTrait;



public function post(): HasOne
{
return $this->hasOne(Post::class);
}
}
12 changes: 0 additions & 12 deletions tests/Classes/Models/ModelHasLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use TomHart\Restful\Concerns\HasLinks;
use TomHart\Restful\Tests\Classes\ModelTest;
use TomHart\Restful\Traits\HasLinksTrait;

/**
Expand All @@ -18,16 +16,6 @@ class ModelHasLinksTest extends Model implements HasLinks
{
use HasLinksTrait;

public function getRouteKey()
{
return 'has_links_test';
}

public function getRouteKeyName()
{
return 'has_links_test';
}

public function without(): HasOne
{
return $this->hasOne(ModelWithoutLinksTest::class);
Expand Down
11 changes: 0 additions & 11 deletions tests/Classes/Models/ModelParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
use TomHart\Restful\Concerns\HasLinks;
use TomHart\Restful\Traits\HasLinksTrait;

Expand All @@ -18,16 +17,6 @@ class ModelParentTest extends Model implements HasLinks
{
use HasLinksTrait;

public function getRouteKey()
{
return 'model_parent';
}

public function getRouteKeyName()
{
return 'model_parent';
}

public function children(): BelongsToMany
{
return $this->belongsToMany(ModelTest::class);
Expand Down
12 changes: 0 additions & 12 deletions tests/Classes/Models/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace TomHart\Restful\Tests\Classes\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use TomHart\Restful\Concerns\HasLinks;
use TomHart\Restful\Traits\HasLinksTrait;

Expand All @@ -18,16 +16,6 @@ class ModelTest extends Model implements HasLinks
{
use HasLinksTrait;

public function getRouteKey()
{
return 'model_test';
}

public function getRouteKeyName()
{
return 'model_test';
}

public function parent(): HasOne
{
return $this->hasOne(ModelTest::class);
Expand Down
25 changes: 25 additions & 0 deletions tests/Classes/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TomHart\Restful\Tests\Classes\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use TomHart\Restful\Concerns\HasLinks;
use TomHart\Restful\Traits\HasLinksTrait;

/**
* @property int $id
* @property string $name
* @method find(int $id)
*/
class Post extends Model implements HasLinks
{
use HasLinksTrait;



public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}
Loading