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
2 changes: 0 additions & 2 deletions src/Controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ protected function response($data = null, $status = 200, array $headers = [])
return $this->response;
}


/**
* @param $modelClass
* @param array $filters
Expand All @@ -144,7 +143,6 @@ public function search($modelClass, $filters = [])
$paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE));
$items = $paginator->getCollection()->map->serializeForIndex($this->request());


return array_merge($paginator->toArray(), [
'data' => $items,
]);
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Database\Eloquent\Model;

/**
* @package Binaryk\LaravelRestify\Http\Requests;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait InteractWithRepositories
Expand Down Expand Up @@ -45,7 +44,7 @@ public function repository()
]), 404);
}

if ( ! $repository::authorizedToViewAny($this)) {
if (! $repository::authorizedToViewAny($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name.', [
'name' => $repository,
]), 403);
Expand Down
1 change: 0 additions & 1 deletion src/Http/Requests/RestifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
class RestifyRequest extends FormRequest
{
use InteractWithRepositories;

}
1 change: 0 additions & 1 deletion src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Binaryk\LaravelRestify\Traits\AuthorizableModels;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

/**
Expand Down
32 changes: 16 additions & 16 deletions src/Services/Search/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function search(RestifyRequest $request, Model $model)
}

/**
* Will prepare the eloquent array to return
* Will prepare the eloquent array to return.
*
* @return array
*/
Expand All @@ -54,7 +54,7 @@ protected function prepare()
}

/**
* Prepare eloquent exact fields
* Prepare eloquent exact fields.
*
* @param $fields
*
Expand Down Expand Up @@ -100,7 +100,7 @@ protected function prepareIn($fields)
}

/**
* Prepare eloquent exact fields
* Prepare eloquent exact fields.
*
* @param $fields
*
Expand All @@ -117,16 +117,16 @@ protected function prepareOperator($fields)
foreach ($values as $field => $value) {
$qualifiedField = $this->model->qualifyColumn($field);
switch ($key) {
case "gte":
case 'gte':
$this->builder->where($qualifiedField, '>=', $value);
break;
case "gt":
case 'gt':
$this->builder->where($qualifiedField, '>', $value);
break;
case "lte":
case 'lte':
$this->builder->where($qualifiedField, '<=', $value);
break;
case "lt":
case 'lt':
$this->builder->where($qualifiedField, '<', $value);
break;
}
Expand All @@ -138,15 +138,15 @@ protected function prepareOperator($fields)
}

/**
* Prepare eloquent exact fields
* Prepare eloquent exact fields.
*
* @param $fields
*
* @return $this
*/
protected function prepareMatchFields()
{
foreach($this->model::getMatchByFields() as $key => $type) {
foreach ($this->model::getMatchByFields() as $key => $type) {
if (! $this->request->has($key) && ! data_get($this->fixedInput, "match.$key")) {
continue;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ protected function prepareMatchFields()
}

/**
* Prepare eloquent order by
* Prepare eloquent order by.
*
* @param $sort
*
Expand Down Expand Up @@ -214,7 +214,7 @@ protected function prepareOrders($sort)
}

/**
* Prepare relations
* Prepare relations.
*
* @return $this
*/
Expand Down Expand Up @@ -243,7 +243,7 @@ protected function prepareRelations()
}

/**
* Prepare search
* Prepare search.
*
* @param $search
* @return $this
Expand All @@ -252,7 +252,7 @@ protected function prepareSearchFields($search)
{
$this->builder->where(function (Builder $query) use ($search) {
/**
* @var RestifySearchable|Model $model
* @var RestifySearchable|Model
*/
$model = $query->getModel();

Expand All @@ -263,23 +263,22 @@ protected function prepareSearchFields($search)
($connectionType != 'pgsql' || $search <= PHP_INT_MAX) &&
in_array($query->getModel()->getKeyName(), $model::getSearchableFields());


if ($canSearchPrimaryKey) {
$query->orWhere($query->getModel()->getQualifiedKeyName(), $search);
}

$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

foreach ($this->model::getSearchableFields() as $column) {
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
}
});

return $this;
}

/**
* Set order
* Set order.
*
* @param $param
*
Expand All @@ -289,6 +288,7 @@ public function setOrder($param)
{
if ($param === 'random') {
$this->builder->inRandomOrder();

return $this;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Services/Search/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
* @package Binaryk\LaravelRestify\Services\Search;
*/
abstract class Searchable
{
/**
Expand All @@ -17,7 +14,7 @@ abstract class Searchable
protected $request;

/**
* @var RestifySearchable|Model $model
* @var RestifySearchable|Model
*/
protected $model;

Expand All @@ -26,7 +23,6 @@ abstract class Searchable
*/
protected $fixedInput;


/**
* @param $input
*
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/AuthorizableModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Illuminate\Support\Str;

/**
* Could be used as a trait in a model class and in a repository class
*
* Could be used as a trait in a model class and in a repository class.
*
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait AuthorizableModels
Expand Down
23 changes: 10 additions & 13 deletions tests/Controllers/RepositoryIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class RepositoryIndexControllerTest extends IntegrationTest
{
public function test_list_resource()
{
{
factory(User::class)->create();
factory(User::class)->create();
factory(User::class)->create();
Expand All @@ -24,7 +24,6 @@ public function test_list_resource()
$response->assertJsonCount(3, 'data.data');
}


public function test_the_rest_controller_can_paginate()
{
$this->mockUsers(50);
Expand Down Expand Up @@ -80,12 +79,12 @@ public function test_search_query_works()
'data' => [
'data' => [$expected],
'current_page' => 1,
'first_page_url' => "http://localhost/restify-api/users?page=1",
'first_page_url' => 'http://localhost/restify-api/users?page=1',
'from' => 1,
'last_page' => 1,
'last_page_url' => "http://localhost/restify-api/users?page=1",
'last_page_url' => 'http://localhost/restify-api/users?page=1',
'next_page_url' => null,
'path' => "http://localhost/restify-api/users",
'path' => 'http://localhost/restify-api/users',
'per_page' => 15,
'prev_page_url' => null,
'to' => 1,
Expand All @@ -101,12 +100,12 @@ public function test_search_query_works()
'data' => [
'data' => [],
'current_page' => 1,
'first_page_url' => "http://localhost/restify-api/users?page=1",
'first_page_url' => 'http://localhost/restify-api/users?page=1',
'from' => 1,
'last_page' => 1,
'last_page_url' => "http://localhost/restify-api/users?page=1",
'last_page_url' => 'http://localhost/restify-api/users?page=1',
'next_page_url' => null,
'path' => "http://localhost/restify-api/users",
'path' => 'http://localhost/restify-api/users',
'per_page' => 15,
'prev_page_url' => null,
'to' => 1,
Expand All @@ -125,8 +124,6 @@ public function test_that_desc_sort_query_param_works()

$this->assertSame($response->data['data'][0]['id'], 10);
$this->assertSame($response->data['data'][9]['id'], 1);


}

public function test_that_asc_sort_query_param_works()
Expand Down Expand Up @@ -173,12 +170,12 @@ public function test_that_match_param_works()
'data' => [
'data' => [$expected],
'current_page' => 1,
'first_page_url' => "http://localhost/restify-api/users?page=1",
'first_page_url' => 'http://localhost/restify-api/users?page=1',
'from' => 1,
'last_page' => 1,
'last_page_url' => "http://localhost/restify-api/users?page=1",
'last_page_url' => 'http://localhost/restify-api/users?page=1',
'next_page_url' => null,
'path' => "http://localhost/restify-api/users",
'path' => 'http://localhost/restify-api/users',
'per_page' => 15,
'prev_page_url' => null,
'to' => 1,
Expand Down
1 change: 0 additions & 1 deletion tests/Factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Faker\Generator as Faker;
use Illuminate\Support\Str;

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;

/**
* @package Binaryk\LaravelRestify\Tests\Fixtures;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
class Post extends Model
Expand All @@ -14,6 +13,6 @@ class Post extends Model
'id',
'user_id',
'title',
'description'
'description',
];
}
6 changes: 3 additions & 3 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class User extends Authenticatable implements Passportable, MustVerifyEmail, Res
InteractWithSearch;

public static $search = ['id', 'email'];
public static $sort = ['id',];
public static $match = ['id' => 'int', 'email' => 'string',];
public static $in = ['id' => 'int',];
public static $sort = ['id'];
public static $match = ['id' => 'int', 'email' => 'string'];
public static $in = ['id' => 'int'];
public static $withs = ['posts'];

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/InteractWithModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function mockUsers($count = 1, $predefinedEmails = [])
$i++;
}

foreach($predefinedEmails as $email) {
foreach ($predefinedEmails as $email) {
$users->push(factory(User::class)->create([
'email' => $email,
]));
Expand All @@ -44,7 +44,7 @@ public function mockPosts($userId, $count = 1)
$i = 0;
while ($i < $count) {
$users->push(factory(Post::class)->create(
['user_id' => $userId,]
['user_id' => $userId]
));
$i++;
}
Expand Down