code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
public function __construct(Factory $factory, ContainerInterface $container) { $this->factory = $factory; $this->container = $container; }
AbstractValidators constructor. @param Factory $factory @param ContainerInterface $container
__construct
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function messages($record = null): array { return $this->messages; }
Get custom messages for a resource object validator. @param mixed|null $record @return array
messages
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function attributes($record = null): array { return $this->attributes; }
Get custom attributes for a resource object validator. @param mixed|null $record @return array
attributes
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function deleteRules($record): ?array { return []; }
Get rules for a delete resource validator. @param $record @return array|null the rules, or an empty value to indicate no validation. @todo 2.0.0 make this abstract.
deleteRules
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function deleteMessages($record): array { return $this->deleteMessages; }
Get custom messages for a delete resource validator. @param $record @return array
deleteMessages
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function deleteAttributes($record): array { return $this->deleteAttributes; }
Get custom attributes for a delete resource validator. @param $record @return array
deleteAttributes
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function dataForCreate(array $document): array { return $document['data'] ?? []; }
Get validation data for creating a domain record. @param array $document @return array
dataForCreate
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function dataForUpdate($record, array $document): array { $resource = $document['data'] ?? []; if ($this->mustValidateExisting($record, $document)) { $resource['attributes'] = $this->extractAttributes( $record, $resource['attributes'] ?? [] ); $resource['relationships'] = $this->extractRelationships( $record, $resource['relationships'] ?? [] ); /** @see https://github.com/cloudcreativity/laravel-json-api/issues/576 */ $resource = json_decode(json_encode($resource), true); } return $resource; }
Get validation data for updating a domain record. The JSON API spec says: > If a request does not include all of the attributes for a resource, > the server MUST interpret the missing attributes as if they were included > with their current values. The server MUST NOT interpret missing > attributes as null values. So that the validator has access to the current values of attributes, we merge attributes provided by the client over the top of the existing attribute values. @param mixed $record the record being updated. @param array $document the JSON API document to validate. @return array
dataForUpdate
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function dataForDelete($record): array { $schema = $this->container->getSchema($record); return ResourceObject::create([ 'type' => $schema->getResourceType(), 'id' => $schema->getId($record), 'attributes' => $schema->getAttributes($record), 'relationships' => collect($this->existingRelationships($record))->all(), ])->all(); }
Get validation data for deleting a domain record. @param $record @return array
dataForDelete
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function mustValidateExisting($record, array $document): bool { return false !== $this->validateExisting; }
Should existing resource values be provided to the validator for an update request? Child classes can overload this method if they need to programmatically work out if existing values must be provided to the validator instance for an update request. @param mixed $record the record being updated @param array $document the JSON API document provided by the client. @return bool
mustValidateExisting
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function extractAttributes($record, array $new): array { return collect($this->existingAttributes($record))->merge($new)->all(); }
Extract attributes for a resource update. @param $record @param array $new @return array
extractAttributes
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function existingAttributes($record): iterable { return $this->container->getSchema($record)->getAttributes($record); }
Get any existing attributes for the provided record. @param $record @return iterable
existingAttributes
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function extractRelationships($record, array $new): array { return collect($this->existingRelationships($record))->map(function ($value) { return $this->convertExistingRelationships($value); })->merge($new)->all(); }
Extract relationships for a resource update. @param $record @param array $new @return array
extractRelationships
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function existingRelationships($record): iterable { return []; }
Get any existing relationships for the provided record. As there is no reliable way for us to work this out (as we do not know the relationship keys), child classes should overload this method to add existing relationship data. @param $record @return iterable
existingRelationships
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function dataForRelationship($record, string $field, array $document): array { $schema = $this->container->getSchema($record); return [ 'type' => $schema->getResourceType(), 'id' => $schema->getId($record), 'relationships' => [ $field => [ 'data' => $document['data'], ], ], ]; }
Get validation data for modifying a relationship. @param mixed $record @param string $field @param array $document @return array
dataForRelationship
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function relationshipRules($record, string $field, array $data): array { return collect($this->rules($record, $data))->filter(function ($v, $key) use ($field) { return Str::startsWith($key, $field); })->all(); }
Get validation rules for a specified relationship field. @param mixed $record @param string $field @param array $data @return array
relationshipRules
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function validatorForResource( array $data, array $rules, array $messages = [], array $customAttributes = [] ): ValidatorInterface { return $this->factory->createResourceValidator( ResourceObject::create($data), $rules, $messages, $customAttributes ); }
Create a validator for a JSON API resource object. @param array $data @param array $rules @param array $messages @param array $customAttributes @return ValidatorInterface
validatorForResource
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function validatorForDelete( array $data, array $rules, array $messages = [], array $customAttributes = [] ): ValidatorInterface { return $this->factory->createDeleteValidator($data, $rules, $messages, $customAttributes); }
Create a validator for a delete request. @param array $data @param array $rules @param array $messages @param array $customAttributes @return ValidatorInterface
validatorForDelete
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function createValidator( array $data, array $rules, array $messages = [], array $customAttributes = [] ): ValidatorInterface { return $this->factory->createValidator($data, $rules, $messages, $customAttributes); }
Create a generic validator. @param array $data @param array $rules @param array $messages @param array $customAttributes @return ValidatorInterface
createValidator
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function excluded(string ...$keys): Collection { return collect($keys)->mapWithKeys(function ($key) { return [$key => new DisallowedParameter($key)]; }); }
Get rules to disallow the provided keys. @param string ...$keys @return Collection
excluded
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function validatorForQuery( array $data, array $rules, array $messages = [], array $customAttributes = [] ): ValidatorInterface { return $this->factory->createQueryValidator($data, $rules, $messages, $customAttributes); }
@param array $data @param array $rules @param array $messages @param array $customAttributes @return ValidatorInterface
validatorForQuery
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function queryValidator(array $params, $without = null): ValidatorInterface { $without = (array) $without; return $this->validatorForQuery( $params, $this->queryRulesWithout(...$without), $this->queryMessages(), $this->queryAttributes() ); }
Get a validator for all query parameters. @param array $params @param array|string|null $without @return ValidatorInterface
queryValidator
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function allowedIncludePaths(): AllowedIncludePaths { return new AllowedIncludePaths($this->allowedIncludePaths); }
Get a rule for the allowed include paths. @return AllowedIncludePaths
allowedIncludePaths
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function allowedFieldSets(): AllowedFieldSets { return new AllowedFieldSets($this->allowedFieldSets); }
Get a rule for the allowed field sets. @return AllowedFieldSets
allowedFieldSets
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function allowedSortParameters(): AllowedSortParameters { return new AllowedSortParameters($this->allowedSortParameters); }
Get a rule for the allowed sort parameters. @return AllowedSortParameters
allowedSortParameters
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function allowedPagingParameters(): AllowedPageParameters { return new AllowedPageParameters($this->allowedPagingParameters); }
Get a rule for the allowed page parameters. @return AllowedPageParameters
allowedPagingParameters
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
protected function allowedFilteringParameters(): AllowedFilterParameters { return new AllowedFilterParameters($this->allowedFilteringParameters); }
Get a rule for the allowed filtering parameters. @return AllowedFilterParameters
allowedFilteringParameters
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
private function convertExistingRelationships($value) { if (is_array($value) && array_key_exists('data', $value)) { return $value; } if (is_null($value)) { return ['data' => null]; } if (is_object($value) && !is_iterable($value)) { $schema = $this->container->getSchema($value); return [ 'data' => [ 'type' => $schema->getResourceType(), 'id' => $schema->getId($value), ], ]; } $data = collect($value)->map(function ($v) { $schema = $this->container->getSchema($v); return ['type' => $schema->getResourceType(), 'id' => $schema->getId($v)]; })->all(); return compact('data'); }
Convert relationships returned by the `existingRelationships()` method. We support the method returning JSON API formatted relationships, e.g.: ``` return [ 'author' => [ 'data' => [ 'type' => 'users', 'id' => (string) $record->author->getRouteKey(), ] ], ]; ``` Or this shorthand: ```php return [ 'author' => $record->author, ]; ``` This method converts the shorthand into the JSON API formatted relationships. @param $value @return array
convertExistingRelationships
php
cloudcreativity/laravel-json-api
src/Validation/AbstractValidators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/AbstractValidators.php
Apache-2.0
public function __construct( ValidatorContract $validator, ErrorTranslator $translator, ?Closure $callback = null ) { $this->validator = $validator; $this->translator = $translator; $this->callback = $callback; }
AbstractValidator constructor. @param ValidatorContract $validator @param ErrorTranslator $translator @param Closure|null $callback
__construct
php
cloudcreativity/laravel-json-api
src/Validation/Validator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Validator.php
Apache-2.0
public function __construct(StoreInterface $store, ErrorTranslator $translator, $document) { if (!is_object($document)) { throw new InvalidArgumentException('Expecting JSON API document to be an object.'); } $this->store = $store; $this->document = $document; $this->translator = $translator; $this->errors = new ErrorCollection(); }
AbstractValidator constructor. @param StoreInterface $store @param ErrorTranslator $translator @param object $document
__construct
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateTypeMember($value, string $path): bool { if (!is_string($value)) { $this->memberNotString($path, 'type'); return false; } if (empty($value)) { $this->memberEmpty($path, 'type'); return false; } if (!$this->store->isType($value)) { $this->resourceTypeNotRecognised($value, $path); return false; } return true; }
Validate the value of a type member. @param mixed $value @param string $path @return bool
validateTypeMember
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateIdMember($value, string $path): bool { if (!is_string($value)) { $this->memberNotString($path, 'id'); return false; } if (empty($value)) { $this->memberEmpty($path, 'id'); return false; } return true; }
Validate the value of an id member. @param mixed $value @param string $path @return bool
validateIdMember
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateIdentifier($value, string $path, ?int $index = null): bool { $member = is_int($index) ? (string) $index : 'data'; if (!is_object($value)) { $this->memberNotObject($path, $member); return false; } $dataPath = sprintf('%s/%s', rtrim($path, '/'), $member); $valid = true; if (!property_exists($value, 'type')) { $this->memberRequired($dataPath, 'type'); $valid = false; } else if (!$this->validateTypeMember($value->type, $dataPath)) { $valid = false; } if (!property_exists($value, 'id')) { $this->memberRequired($dataPath, 'id'); $valid = false; } else if (!$this->validateIdMember($value->id, $dataPath)) { $valid = false; } /** If it has attributes or relationships, it is a resource object not a resource identifier */ if (property_exists($value, 'attributes') || property_exists($value, 'relationships')) { $this->memberNotIdentifier($path, $member); $valid = false; } return $valid; }
Validate an identifier object. @param mixed $value @param string $path the path to the data member in which the identifier is contained. @param int|null $index the index for the identifier, if in a collection. @return bool
validateIdentifier
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateRelationship($relation, ?string $field = null): bool { $path = $field ? '/data/relationships' : '/'; $member = $field ?: 'data'; if (!is_object($relation)) { $this->memberNotObject($path, $member); return false; } $path = $field ? "{$path}/{$field}" : $path; if (!property_exists($relation, 'data')) { $this->memberRequired($path, 'data'); return false; } $data = $relation->data; if (is_array($data)) { return $this->validateToMany($data, $field); } return $this->validateToOne($data, $field); }
Validate a resource relationship. @param mixed $relation @param string|null $field @return bool
validateRelationship
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateToOne($value, ?string $field = null): bool { if (is_null($value)) { return true; } $dataPath = $field ? "/data/relationships/{$field}" : "/"; $identifierPath = $field ? "/data/relationships/{$field}" : "/data"; if (!$this->validateIdentifier($value, $dataPath)) { return false; } if (!$this->store->exists($value->type, $value->id)) { $this->resourceDoesNotExist($identifierPath); return false; } return true; }
Validate a to-one relation. @param mixed $value @param string|null $field the relationship field name. @return bool
validateToOne
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function validateToMany(array $value, ?string $field = null): bool { $path = $field ? "/data/relationships/{$field}/data" : "/data"; $valid = true; foreach ($value as $index => $item) { if (!$this->validateIdentifier($item, $path, $index)) { $valid = false; continue; } if ($this->isNotFound($item->type, $item->id)) { $this->resourceDoesNotExist("{$path}/{$index}"); $valid = false; } } return $valid; }
Validate a to-many relation. @param array $value @param string|null $field the relationship field name. @return bool
validateToMany
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function dataHas($key): bool { if (!isset($this->document->data)) { return false; } return property_exists($this->document->data, $key); }
Does the key exist in document data object? @param $key @return bool
dataHas
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function dataGet($key, $default = null) { if (!isset($this->document->data)) { return $default; } return data_get($this->document->data, $key, $default); }
Get a value from the document data object use dot notation. @param string|array $key @param mixed $default @return mixed|null
dataGet
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function isNotFound(string $type, string $id): bool { return !$this->store->exists($type, $id); }
Check if the resource is not found. @param string $type @param string $id @return bool
isNotFound
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberRequired(string $path, string $member): void { $this->errors->add($this->translator->memberRequired($path, $member)); }
Add an error for a member that is required. @param string $path @param string $member @return void
memberRequired
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberNotObject(string $path, string $member): void { $this->errors->add($this->translator->memberNotObject($path, $member)); }
Add an error for a member that must be an object. @param string $path @param string $member @return void
memberNotObject
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberNotIdentifier(string $path, string $member): void { $this->errors->add($this->translator->memberNotIdentifier($path, $member)); }
Add an error for a member that must be an object. @param string $path @param string|null $member @return void
memberNotIdentifier
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberFieldsNotAllowed(string $path, string $member, iterable $fields): void { foreach ($fields as $field) { $this->errors->add($this->translator->memberFieldNotAllowed($path, $member, $field)); } }
Add errors for when a member has a field that is not allowed. @param string $path @param string $member @param iterable $fields
memberFieldsNotAllowed
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberNotString(string $path, string $member): void { $this->errors->add($this->translator->memberNotString($path, $member)); }
Add an error for a member that must be a string. @param string $path @param string $member @return void
memberNotString
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function memberEmpty(string $path, string $member): void { $this->errors->add($this->translator->memberEmpty($path, $member)); }
Add an error for a member that cannot be an empty value. @param string $path @param string $member @return void
memberEmpty
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceTypeNotSupported(string $actual, string $path = '/data'): void { $this->errors->add($this->translator->resourceTypeNotSupported($actual, $path)); }
Add an error for when the resource type is not supported by the endpoint. @param string $actual @param string $path @return void
resourceTypeNotSupported
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceTypeNotRecognised(string $actual, string $path = '/data'): void { $this->errors->add($this->translator->resourceTypeNotRecognised($actual, $path)); }
Add an error for when a resource type is not recognised. @param string $actual @param string $path @return void
resourceTypeNotRecognised
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceIdNotSupported(string $actual, string $path = '/data'): void { $this->errors->add($this->translator->resourceIdNotSupported($actual, $path)); }
Add an error for when the resource id is not supported by the endpoint. @param string $actual @param string $path @return void
resourceIdNotSupported
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceDoesNotSupportClientIds(string $type, string $path = '/data'): void { $this->errors->add($this->translator->resourceDoesNotSupportClientIds($type, $path)); }
Add an error for when the resource does not support client-generated ids. @param string $type @param string $path @return void
resourceDoesNotSupportClientIds
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceExists(string $type, string $id, string $path = '/data'): void { $this->errors->add($this->translator->resourceExists($type, $id, $path)); }
Add an error for when a resource already exists. @param string $type @param string $id @param string $path @return void
resourceExists
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceDoesNotExist(string $path): void { $this->errors->add($this->translator->resourceDoesNotExist($path)); }
Add an error for when a resource identifier does not exist. @param string $path @return void
resourceDoesNotExist
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
protected function resourceFieldsExistInAttributesAndRelationships(iterable $fields): void { foreach ($fields as $field) { $this->errors->add( $this->translator->resourceFieldExistsInAttributesAndRelationships($field) ); } }
Add errors for when a resource field exists in both the attributes and relationships members. @param iterable $fields @return void
resourceFieldsExistInAttributesAndRelationships
php
cloudcreativity/laravel-json-api
src/Validation/Spec/AbstractValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/AbstractValidator.php
Apache-2.0
public function __construct( StoreInterface $store, ErrorTranslator $translator, $document, string $expectedType, bool $clientIds = false ) { if (empty($expectedType)) { throw new InvalidArgumentException('Expecting type to be a non-empty string.'); } parent::__construct($store, $translator, $document); $this->expectedType = $expectedType; $this->clientIds = $clientIds; }
CreateResourceValidator constructor. @param StoreInterface $store @param ErrorTranslator $translator @param object $document @param string $expectedType @param bool $clientIds whether client ids are supported.
__construct
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateData(): bool { if (!property_exists($this->document, 'data')) { $this->memberRequired('/', 'data'); return false; } $data = $this->document->data; if (!is_object($data)) { $this->memberNotObject('/', 'data'); return false; } return true; }
Validate that the top-level `data` member is acceptable. @return bool
validateData
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateResource(): bool { $identifier = $this->validateTypeAndId(); $attributes = $this->validateAttributes(); $relationships = $this->validateRelationships(); if ($attributes && $relationships) { return $this->validateAllFields() && $identifier; } return $identifier && $attributes && $relationships; }
Validate the resource object. @return bool
validateResource
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateTypeAndId(): bool { if (!($this->validateType() && $this->validateId())) { return false; } $type = $this->dataGet('type'); $id = $this->dataGet('id'); if ($id && !$this->isNotFound($type, $id)) { $this->resourceExists($type, $id); return false; } return true; }
Validate the resource type and id. @return bool
validateTypeAndId
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateType(): bool { if (!$this->dataHas('type')) { $this->memberRequired('/data', 'type'); return false; } $value = $this->dataGet('type'); if (!$this->validateTypeMember($value, '/data')) { return false; } if ($this->expectedType !== $value) { $this->resourceTypeNotSupported($value); return false; } return true; }
Validate the resource type. @return bool
validateType
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateId(): bool { if (!$this->dataHas('id')) { return true; } $valid = $this->validateIdMember($this->dataGet('id'), '/data'); if (!$this->supportsClientIds()) { $valid = false; $this->resourceDoesNotSupportClientIds($this->expectedType); } return $valid; }
Validate the resource id. @return bool
validateId
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateAttributes(): bool { if (!$this->dataHas('attributes')) { return true; } $attrs = $this->dataGet('attributes'); if (!is_object($attrs)) { $this->memberNotObject('/data', 'attributes'); return false; } $disallowed = collect(['type', 'id'])->filter(function ($field) use ($attrs) { return property_exists($attrs, $field); }); $this->memberFieldsNotAllowed('/data', 'attributes', $disallowed); return $disallowed->isEmpty(); }
Validate the resource attributes. @return bool
validateAttributes
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateRelationships(): bool { if (!$this->dataHas('relationships')) { return true; } $relationships = $this->dataGet('relationships'); if (!is_object($relationships)) { $this->memberNotObject('/data', 'relationships'); return false; } $disallowed = collect(['type', 'id'])->filter(function ($field) use ($relationships) { return property_exists($relationships, $field); }); $valid = $disallowed->isEmpty(); $this->memberFieldsNotAllowed('/data', 'relationships', $disallowed); foreach ($relationships as $field => $relation) { if (!$this->validateRelationship($relation, $field)) { $valid = false; } } return $valid; }
Validate the resource relationships. @return bool
validateRelationships
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function validateAllFields(): bool { $duplicates = collect( (array) $this->dataGet('attributes', []) )->intersectByKeys( (array) $this->dataGet('relationships', []) )->keys(); $this->resourceFieldsExistInAttributesAndRelationships($duplicates); return $duplicates->isEmpty(); }
Validate the resource's attributes and relationships collectively. @return bool
validateAllFields
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
protected function supportsClientIds(): bool { return $this->clientIds; }
Are client ids supported? @return bool
supportsClientIds
php
cloudcreativity/laravel-json-api
src/Validation/Spec/CreateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/CreateResourceValidator.php
Apache-2.0
public function __construct( StoreInterface $store, ErrorTranslator $translator, $document, string $expectedType, string $expectedId ) { if (empty($expectedId)) { throw new InvalidArgumentException('Expecting id to be a non-empty string.'); } parent::__construct($store, $translator, $document, $expectedType); $this->expectedId = $expectedId; }
UpdateResourceValidator constructor. @param StoreInterface $store @param ErrorTranslator $translator @param object $document @param string $expectedType @param string $expectedId
__construct
php
cloudcreativity/laravel-json-api
src/Validation/Spec/UpdateResourceValidator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Validation/Spec/UpdateResourceValidator.php
Apache-2.0
public function with($apiName = null, $options = 0, $depth = 512) { $this->encoder = $this->service->api($apiName)->encoder($options, $depth); }
@param $apiName @param int $options @param int $depth
with
php
cloudcreativity/laravel-json-api
src/View/Renderer.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/View/Renderer.php
Apache-2.0
public function encode($data, $includePaths = null, $fieldSets = null) { if (!$this->encoder) { $this->with(); } $params = null; if ($includePaths || $fieldSets) { $params = new QueryParameters( $includePaths ? (array) $includePaths : $includePaths, $fieldSets ); } return $this->encoder ->withEncodingParameters($params) ->encodeData($data); }
@param $data @param string|array|null $includePaths @param array|null $fieldSets @return string
encode
php
cloudcreativity/laravel-json-api
src/View/Renderer.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/View/Renderer.php
Apache-2.0
public function scopeRelated(Builder $query, Post $post) { return $query->where(function (Builder $q) use ($post) { $q->whereHas('tags', function (Builder $t) use ($post) { $t->whereIn('tags.id', $post->tags()->pluck('tags.id')); })->orWhere('posts.author_id', $post->getKey()); })->where('posts.id', '<>', $post->getKey()); }
Scope a query for posts that are related to the supplied post. Related posts are those that: - have a tag in common with the provided post; or - are by the same author. @param Builder $query @param Post $post @return Builder
scopeRelated
php
cloudcreativity/laravel-json-api
tests/dummy/app/Post.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Post.php
Apache-2.0
public function scopePublished(Builder $query, bool $published = true): Builder { if ($published) { $query->whereNotNull('published_at'); } else { $query->whereNull('published_at'); } return $query; }
@param Builder $query @param bool $published @return Builder
scopePublished
php
cloudcreativity/laravel-json-api
tests/dummy/app/Post.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Post.php
Apache-2.0
public function scopeLikeTitle(Builder $query, string $title): Builder { return $query->where('title', 'like', $title . '%'); }
@param Builder $query @param string $title @return Builder
scopeLikeTitle
php
cloudcreativity/laravel-json-api
tests/dummy/app/Post.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Post.php
Apache-2.0
public function scopeRelated(Builder $query, Post $post) { $tags = $post->tags()->pluck('tags.id'); return $query->whereHas('tags', function (Builder $q) use ($tags) { $q->whereIn('tags.id', $tags); }); }
Scope a query for videos that are related to the supplied post. Finds videos that have at least one tag that is in common with the tags on the supplied post. @param Builder $query @param Post $post @return Builder
scopeRelated
php
cloudcreativity/laravel-json-api
tests/dummy/app/Video.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Video.php
Apache-2.0
public static function create($slug, array $values) { $site = new self($slug); $site->exchangeArray($values); return $site; }
@param string $slug @param array $values @return Site
create
php
cloudcreativity/laravel-json-api
tests/dummy/app/Entities/Site.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Entities/Site.php
Apache-2.0
public function remove($site) { $slug = ($site instanceof Site) ? $site->getSlug() : $site; unset($this->sites[$slug]); }
@param Site|string $site @return void
remove
php
cloudcreativity/laravel-json-api
tests/dummy/app/Entities/SiteRepository.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Entities/SiteRepository.php
Apache-2.0
protected function reading(Avatar $avatar): ?StreamedResponse { if ($this->willNotEncode($avatar->media_type)) { return null; } abort_unless( Storage::disk('local')->exists($avatar->path), 404, 'The image file does not exist.' ); return Storage::disk('local')->download($avatar->path); }
@param Avatar $avatar @return StreamedResponse|null
reading
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/AvatarsController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/AvatarsController.php
Apache-2.0
public function sharePost(FetchRelated $request, Comment $comment): Response { $post = $comment->commentable; abort_unless($post instanceof Post, '404'); SharePost::dispatch($post); return $this->reply()->content($post); }
@param FetchRelated $request @param Comment $comment @return Response
sharePost
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/CommentsController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/CommentsController.php
Apache-2.0
protected function creating(ValidatedRequest $request) { $data = $request->get("data.relationships.commentable.data"); if ($data && 'posts' === $data['type']) { $this->can('comment', Post::find($data['id'])); } }
@param ValidatedRequest $request @throws AuthenticationException @throws AuthorizationException
creating
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/CommentsController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/CommentsController.php
Apache-2.0
public function share(FetchResource $request, Post $post): Response { SharePost::dispatch($post); return $this->reply()->content($post); }
@param FetchResource $request @param Post $post @return Response
share
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/PostsController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/PostsController.php
Apache-2.0
public function __construct() { $this->middleware('guest')->except('logout'); }
Create a new controller instance. @return void
__construct
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/Auth/LoginController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/Auth/LoginController.php
Apache-2.0
protected function authenticated(Request $request, $user) { if (Helpers::wantsJsonApi($request)) { return $this->reply()->content($user); } return null; }
@param Request $request @param $user @return Response|null
authenticated
php
cloudcreativity/laravel-json-api
tests/dummy/app/Http/Controllers/Auth/LoginController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/Http/Controllers/Auth/LoginController.php
Apache-2.0
public function update($record, array $document, QueryParametersInterface $parameters) { if ($this->didDecode('application/vnd.api+json')) { return parent::update($record, $document, $parameters); } $path = request()->file('avatar')->store('avatars'); $data = [ 'type' => 'avatars', 'id' => $record->getRouteKey(), 'attributes' => [ 'path' => $path, 'media-type' => Storage::disk('local')->mimeType($path), ], ]; return parent::update($record, compact('data'), $parameters); }
@param Avatar $record @param array $document @param QueryParametersInterface $parameters @return mixed
update
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/Adapter.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/Adapter.php
Apache-2.0
protected function encodingsForOne(?Avatar $avatar): EncodingList { $mediaType = optional($avatar)->media_type; return $this ->encodingMediaTypes() ->when($this->request->isMethod('GET'), $mediaType); }
@param Avatar|null $avatar @return EncodingList
encodingsForOne
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/ContentNegotiator.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/ContentNegotiator.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'mediaType' => $resource->media_type, 'updatedAt' => $resource->updated_at, ]; }
@param Avatar|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/Schema.php
Apache-2.0
public function getRelationships(object $resource, bool $isPrimary, array $includeRelationships): array { return [ 'user' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includeRelationships['user']), self::DATA => function () use ($resource) { return $resource->user; }, ], ]; }
@param Avatar|object $resource @param bool $isPrimary @param array $includeRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/Schema.php
Apache-2.0
protected function rules($record, array $data): array { return [ // ]; }
Get resource validation rules. @param mixed|null $record the record being updated, or null if creating a resource. @param array $data the data being validated. @return mixed
rules
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/Validators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/Validators.php
Apache-2.0
protected function queryRules(): array { return [ // ]; }
Get query parameter validation rules. @return array
queryRules
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Avatars/Validators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Avatars/Validators.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'content' => $resource->content, 'updatedAt' => $resource->updated_at, ]; }
@param Comment|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Comments/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Comments/Schema.php
Apache-2.0
public function getRelationships(object $resource, bool $isPrimary, array $includeRelationships): array { return [ 'commentable' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includeRelationships['commentable']), self::DATA => function () use ($resource) { return $resource->commentable; }, ], 'createdBy' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includeRelationships['createdBy']), self::DATA => function () use ($resource) { return $resource->user; }, ], ]; }
@param Comment|object $resource @param bool $isPrimary @param array $includeRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Comments/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Comments/Schema.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'code' => $resource->code, 'name' => $resource->name, 'updatedAt' => $resource->updated_at, ]; }
@param Country|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Countries/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Countries/Schema.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'detail' => $resource->detail, 'updatedAt' => $resource->updated_at, ]; }
@param History|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Histories/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Histories/Schema.php
Apache-2.0
public function getRelationships(object $resource, bool $isPrimary, array $includeRelationships): array { return [ 'user' => [ self::SHOW_SELF => false, self::SHOW_RELATED => false, self::SHOW_DATA => isset($includeRelationships['user']), self::DATA => static function () use ($resource) { return $resource->user; }, ], ]; }
@param History|object $resource @param bool $isPrimary @param array $includeRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Histories/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Histories/Schema.php
Apache-2.0
protected function filter($query, Collection $filters) { $this->filterWithScopes($query, $filters); }
@param Builder $query @param Collection $filters @return void
filter
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Images/Adapter.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Images/Adapter.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'updatedAt' => $resource->updated_at, 'url' => $resource->url, ]; }
@param Image|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Images/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Images/Schema.php
Apache-2.0
public function getRelationships(object $resource, bool $isPrimary, array $includeRelationships): array { return [ 'user' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includeRelationships['user']), self::DATA => function () use ($resource) { return $resource->user; }, ], ]; }
@param Phone|object $resource @param bool $isPrimary @param array $includeRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Phones/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Phones/Schema.php
Apache-2.0
protected function rules($record, array $data): array { return [ // ]; }
Get the validation rules for the resource attributes. @param object|null $record the record being updated, or null if it is a create request. @param array $data the data being validated. @return array
rules
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Phones/Validators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Phones/Validators.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'content' => $resource->content, 'deletedAt' => $resource->deleted_at, 'published' => $resource->published_at, 'slug' => $resource->slug, 'title' => $resource->title, 'updatedAt' => $resource->updated_at, ]; }
@param Post|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Posts/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Posts/Schema.php
Apache-2.0
public function getRelationships(object $record, bool $isPrimary, array $includedRelationships): array { return [ 'author' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includedRelationships['author']), self::DATA => function () use ($record) { return $record->author; }, ], 'comments' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includedRelationships['comments']), self::DATA => function () use ($record) { return $record->comments; }, self::META => function () use ($record, $isPrimary) { return $isPrimary ? ['count' => $record->comments()->count()] : null; }, ], 'image' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includedRelationships['image']), self::DATA => function () use ($record) { return $record->image; }, ], 'tags' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => isset($includedRelationships['tags']), self::DATA => function () use ($record) { return $record->tags; }, ], ]; }
@param Post|object $record @param bool $isPrimary @param array $includedRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Posts/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Posts/Schema.php
Apache-2.0
protected function rules($record, array $data): array { $slugUnique = 'unique:posts,slug'; if ($record) { $slugUnique .= ',' . $record->getKey(); } return [ 'title' => "required|string|between:5,255", 'content' => "required|string|min:1", 'slug' => "required|alpha_dash|$slugUnique", 'published' => [ 'nullable', new DateTimeIso8601() ], 'author' => new HasOne('users'), 'tags' => new HasMany('tags'), ]; }
@param Post|null $record @param array $data @return array|mixed
rules
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Posts/Validators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Posts/Validators.php
Apache-2.0
protected function deleteRules($record): ?array { return [ 'no_comments' => 'accepted', ]; }
@param Post $record @return array|null
deleteRules
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Posts/Validators.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Posts/Validators.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'attempts' => $resource->attempts, 'completedAt' => $resource->completed_at, 'createdAt' => $resource->created_at, 'failed' => $resource->failed, 'resourceType' => $resource->resource_type, 'timeout' => $resource->timeout, 'timeoutAt' => $resource->timeout_at, 'tries' => $resource->tries, 'updatedAt' => $resource->updated_at, ]; }
@param ClientJob|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/QueueJobs/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/QueueJobs/Schema.php
Apache-2.0
public function getAttributes(object $resource): array { return [ 'createdAt' => $resource->created_at, 'name' => $resource->name, 'updatedAt' => $resource->updated_at, ]; }
@param Role|object $resource @return array
getAttributes
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Roles/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Roles/Schema.php
Apache-2.0
public function getRelationships(object $resource, bool $isPrimary, array $includeRelationships): array { return [ 'users' => [ self::SHOW_SELF => true, self::SHOW_RELATED => true, self::SHOW_DATA => false, ], ]; }
@param Role|object $resource @param bool $isPrimary @param array $includeRelationships @return array
getRelationships
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Roles/Schema.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Roles/Schema.php
Apache-2.0
protected function fillAttribute($record, $field, $value) { $method = 'set' . Str::classify($field); call_user_func([$record, $method], $value); }
@param object $record @param string $field @param mixed $value @return void
fillAttribute
php
cloudcreativity/laravel-json-api
tests/dummy/app/JsonApi/Sites/Adapter.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/dummy/app/JsonApi/Sites/Adapter.php
Apache-2.0