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: 1 addition & 1 deletion src/Controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function search($modelClass, $filters = [])
return [
'meta' => Arr::except($paginator->toArray(), ['data', 'next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']),
'links' => Arr::only($paginator->toArray(), ['next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']),
'data' => $items
'data' => $items,
];
}

Expand Down
19 changes: 10 additions & 9 deletions src/Controllers/RestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class RestResponse
protected $type;

/**
* Key of the newly created resource
* Key of the newly created resource.
* @var
*/
protected $id;
/**
* Model related entities
* Model related entities.
* @var
*/
protected $relationships;
Expand Down Expand Up @@ -184,7 +184,7 @@ public function errors(array $errors = null)
*/
public function addError($message)
{
if ( ! isset($this->errors)) {
if (! isset($this->errors)) {
$this->errors = [];
}

Expand Down Expand Up @@ -269,7 +269,7 @@ public function __get($key)
return $this->$key;
}

$code = 'static::REST_RESPONSE_' . strtoupper($key) . '_CODE';
$code = 'static::REST_RESPONSE_'.strtoupper($key).'_CODE';

return defined($code) ? constant($code) : null;
}
Expand All @@ -284,7 +284,7 @@ public function __get($key)
*/
public function __call($func, $args)
{
$code = 'static::REST_RESPONSE_' . strtoupper($func) . '_CODE';
$code = 'static::REST_RESPONSE_'.strtoupper($func).'_CODE';

if (defined($code)) {
return $this->code(constant($code));
Expand All @@ -303,7 +303,7 @@ public function __call($func, $args)
*/
public function respond($response = null)
{
if ( ! func_num_args()) {
if (! func_num_args()) {
$response = new \stdClass();
$response->data = new \stdClass();

Expand Down Expand Up @@ -408,7 +408,7 @@ public function getAttribute($name)
}

/**
* Set attributes at root level
* Set attributes at root level.
*
* @param array $attributes
* @return mixed
Expand Down Expand Up @@ -445,6 +445,7 @@ protected function response()
public function header($name, $value)
{
$this->headers[$name] = $value;

return $this;
}

Expand All @@ -455,12 +456,13 @@ public function header($name, $value)
public function type($type)
{
$this->type = $type;

return $this;
}

/**
* Useful when newly created repository, will prepare the response according
* with JSON:API https://jsonapi.org/format/#document-resource-object-fields
* with JSON:API https://jsonapi.org/format/#document-resource-object-fields.
*
* @param Repository $repository
* @param bool $withRelations
Expand Down Expand Up @@ -502,7 +504,6 @@ public static function beforeRespond($response)
return $response;
}


return $response;
}
}
2 changes: 0 additions & 2 deletions src/Fields/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace Binaryk\LaravelRestify\Fields;

/**
* @package Binaryk\LaravelRestify;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
abstract class BaseField
{

}
18 changes: 9 additions & 9 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
use JsonSerializable;

/**
* @package Binaryk\LaravelRestify;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
class Field extends OrganicField implements JsonSerializable
{
use RulesTrait;

/**
* Column name of the field
* Column name of the field.
* @var string
*/
public $attribute;

/**
* Callback called when the value is filled, this callback will do not override the fill action
* Callback called when the value is filled, this callback will do not override the fill action.
* @var Closure
*/
public $storeCallback;

/**
* Callback called when trying to fill this attribute, this callback will override the fill action, so make
* sure you assign the attribute to the model over this callback
* sure you assign the attribute to the model over this callback.
*
* @var Closure
*/
Expand Down Expand Up @@ -56,7 +55,7 @@ public static function fire(...$arguments)
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function jsonSerialize()
{
Expand All @@ -65,7 +64,7 @@ public function jsonSerialize()

/**
* Callback called when the value is filled, this callback will do not override the fill action. If fillCallback is defined
* this will do not be called
* this will do not be called.
*
* @param Closure $callback
* @return Field
Expand All @@ -79,19 +78,20 @@ public function storeCallback(Closure $callback)

/**
* Callback called when trying to fill this attribute, this callback will override the fill action, so make
* sure you assign the attribute to the model over this callback
* sure you assign the attribute to the model over this callback.
*
* @param Closure $callback
* @return $this
*/
public function fillCallback(Closure $callback)
{
$this->fillCallback = $callback;

return $this;
}

/**
* Fill attribute with value from the request or delegate this action to the user defined callback
* Fill attribute with value from the request or delegate this action to the user defined callback.
*
* @param RestifyRequest $request
* @param $model
Expand All @@ -111,7 +111,7 @@ public function fillAttribute(RestifyRequest $request, $model)
}

/**
* Fill the model with value from the request
* Fill the model with value from the request.
*
* @param RestifyRequest $request
* @param $model
Expand Down
1 change: 0 additions & 1 deletion src/Fields/OrganicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Binaryk\LaravelRestify\Fields;

/**
* @package Binaryk\LaravelRestify;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
abstract class OrganicField extends BaseField
Expand Down
15 changes: 7 additions & 8 deletions src/Fields/RulesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,31 @@
use Illuminate\Contracts\Validation\Rule;

/**
* @package Binaryk\LaravelRestify\Fields;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait RulesTrait
{
/**
* Rules for applied when store
* Rules for applied when store.
*
* @var array
*/
public $storingRules = [];

/**
* Rules for applied when store and update
* Rules for applied when store and update.
*
* @var array
*/
public $rules = [];


/**
* @var array
*/
public $messages = [];

/**
* Validation rules for store
* Validation rules for store.
* @param callable|array|string $rules
* @return RulesTrait
*/
Expand All @@ -43,7 +41,7 @@ public function storingRules($rules)
}

/**
* Validation rules for store
* Validation rules for store.
* @param callable|array|string $rules
* @return RulesTrait
*/
Expand All @@ -55,19 +53,20 @@ public function rules($rules)
}

/**
* Validation messages
* Validation messages.
*
* @param array $messages
* @return RulesTrait
*/
public function messages(array $messages)
{
$this->messages = $messages;

return $this;
}

/**
* Validation rules for storing
* Validation rules for storing.
*
* @return array
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/RepositoryStoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RepositoryStoreController extends RepositoryController
public function handle(RepositoryStoreRequest $request)
{
/**
* @var Repository $repository
* @var Repository
*/
$repository = $request->repository();

Expand All @@ -57,7 +57,7 @@ public function handle(RepositoryStoreRequest $request)
return $this->response()
->code(201)
->forRepository($request->newRepositoryWith($model), true)
->header('Location', Restify::path() . '/' . $repository::uriKey() . '/' . $model->id)
->header('Location', Restify::path().'/'.$repository::uriKey().'/'.$model->id)
->respond();
}
}
1 change: 0 additions & 1 deletion src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,4 @@ public function newRepositoryWith($model)

return new $resource($model);
}

}
8 changes: 3 additions & 5 deletions src/Repositories/RepositoryFillFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
use Illuminate\Support\Collection;

/**
* @package Binaryk\LaravelRestify\Repositories;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait RepositoryFillFields
{

/**
* Fill fields on store request
* Fill fields on store request.
*
* @param RestifyRequest $request
* @param $model
Expand All @@ -32,7 +30,7 @@ public static function fillWhenStore(RestifyRequest $request, $model)
}

/**
* Fill each field separately
* Fill each field separately.
*
* @param RestifyRequest $request
* @param Model $model
Expand All @@ -50,7 +48,7 @@ protected static function fillFields(RestifyRequest $request, Model $model, Coll

/**
* If some fields were not defined in the @fields method, but they are in fillable attributes and present in request,
* they should be also filled on request
* they should be also filled on request.
* @param RestifyRequest $request
* @param Model $model
* @param Collection $fields
Expand Down
8 changes: 3 additions & 5 deletions src/Repositories/ValidatingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Facades\Validator;

/**
* @package Binaryk\LaravelRestify\Repositories;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait ValidatingTrait
Expand All @@ -24,7 +23,6 @@ abstract public function collectFields(RestifyRequest $request);
*/
abstract public static function newModel();


/**
* @param RestifyRequest $request
* @return \Illuminate\Contracts\Validation\Validator
Expand All @@ -36,16 +34,16 @@ public static function validatorForStoring(RestifyRequest $request)
$messages = $on->collectFields($request)->flatMap(function ($k) {
$messages = [];
foreach ($k->messages as $ruleFor => $message) {
$messages[$k->attribute . '.' . $ruleFor] = $message;
$messages[$k->attribute.'.'.$ruleFor] = $message;
}

return $messages;
})->toArray();

return Validator::make($request->all(), $on->getStoringRules($request), $messages)->after(function ($validator) use ($request) {
static::afterValidation($request, $validator);
static::afterStoringValidation($request, $validator);
});

}

/**
Expand Down Expand Up @@ -79,7 +77,7 @@ public function getStoringRules(RestifyRequest $request)
{
return $this->collectFields($request)->mapWithKeys(function (Field $k) {
return [
$k->attribute => $k->getStoringRules()
$k->attribute => $k->getStoringRules(),
];
})->toArray();
}
Expand Down
2 changes: 0 additions & 2 deletions src/Traits/PerformsRequestValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace Binaryk\LaravelRestify\Traits;

/**
* @package Binaryk\LaravelRestify\Traits;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
trait PerformsRequestValidation
{

}
3 changes: 1 addition & 2 deletions tests/Controllers/RepositoryStoreControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Binaryk\LaravelRestify\Tests\IntegrationTest;

/**
* @package Binaryk\LaravelRestify\Tests\Controllers;
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
class RepositoryStoreControllerTest extends IntegrationTest
Expand All @@ -24,7 +23,7 @@ public function test_basic_validation_works()
->assertJson([
'errors' => [
'description' => [
'Description field is required bro.'
'Description field is required bro.',
],
],
]);
Expand Down
Loading