Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Http/Resources/JsonApi/JsonApiResource.php
Строк: 239
<?php

namespace IlluminateHttpResourcesJsonApi;

use 
BadMethodCallException;
use 
IlluminateHttpJsonResponse;
use 
IlluminateHttpRequest;
use 
IlluminateHttpResourcesJsonJsonResource;
use 
IlluminateSupportArr;

class 
JsonApiResource extends JsonResource
{
    use 
ConcernsResolvesJsonApiElements,
        
ConcernsResolvesJsonApiRequest;

    
/**
     * The "data" wrapper that should be applied.
     *
     * @var string|null
     */
    
public static $wrap 'data';

    
/**
     * The resource's "version" for JSON:API.
     *
     * @var array{version?: string, ext?: array, profile?: array, meta?: array}
     */
    
public static $jsonApiInformation = [];

    
/**
     * The resource's "links" for JSON:API.
     */
    
protected array $jsonApiLinks = [];

    
/**
     * The resource's "meta" for JSON:API.
     */
    
protected array $jsonApiMeta = [];

    
/**
     * Set the JSON:API version for the request.
     *
     * @return void
     */
    
public static function configure(?string $version null, array $ext = [], array $profile = [], array $meta = [])
    {
        static::
$jsonApiInformation array_filter([
            
'version' => $version,
            
'ext' => $ext,
            
'profile' => $profile,
            
'meta' => $meta,
        ]);
    }

    
/**
     * Get the resource's ID.
     *
     * @return string|null
     */
    
public function toId(Request $request)
    {
        return 
null;
    }

    
/**
     * Get the resource's type.
     *
     * @return string|null
     */
    
public function toType(Request $request)
    {
        return 
null;
    }

    
/**
     * Transform the resource into an array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateContractsSupportArrayable|JsonSerializable|array
     */
    #[Override]
    
public function toAttributes(Request $request)
    {
        if (
property_exists($this'attributes')) {
            return 
$this->attributes;
        }

        return 
$this->toArray($request);
    }

    
/**
     * Get the resource's relationships.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateContractsSupportArrayable|array
     */
    
public function toRelationships(Request $request)
    {
        if (
property_exists($this'relationships')) {
            return 
$this->relationships;
        }

        return [];
    }

    
/**
     * Get the resource's links.
     *
     * @return array
     */
    
public function toLinks(Request $request)
    {
        return 
$this->jsonApiLinks;
    }

    
/**
     * Get the resource's meta information.
     *
     * @return array
     */
    
public function toMeta(Request $request)
    {
        return 
$this->jsonApiMeta;
    }

    
/**
     * Get any additional data that should be returned with the resource array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return array
     */
    #[Override]
    
public function with($request)
    {
        return 
array_filter([
            
'included' => $this->resolveIncludedResourceObjects($request)
                ->
uniqueStrict('_uniqueKey')
                ->
map(fn ($included) => Arr::except($included, ['_uniqueKey']))
                ->
values()
                ->
all(),
            ...(
$implementation = static::$jsonApiInformation)
                ? [
'jsonapi' => $implementation]
                : [],
        ]);
    }

    
/**
     * Resolve the resource to an array.
     *
     * @param  IlluminateHttpRequest|null  $request
     * @return array
     */
    #[Override]
    
public function resolve($request null)
    {
        return [
            
'data' => $this->resolveResourceData($this->resolveJsonApiRequestFrom($request ?? $this->resolveRequestFromContainer())),
        ];
    }

    
/**
     * Resolve the resource data to an array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return array
     */
    #[Override]
    
public function resolveResourceData(Request $request)
    {
        return 
$this->resolveResourceObject($request);
    }

    
/**
     * Customize the outgoing response for the resource.
     */
    #[Override]
    
public function withResponse(Request $requestJsonResponse $response): void
    
{
        
$response->header('Content-Type''application/vnd.api+json');
    }

    
/**
     * Create an HTTP response that represents the object.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpJsonResponse
     */
    #[Override]
    
public function toResponse($request)
    {
        return 
parent::toResponse($this->resolveJsonApiRequestFrom($request));
    }

    
/**
     * Resolve the HTTP request instance from container.
     *
     * @return IlluminateHttpResourcesJsonApiJsonApiRequest
     */
    #[Override]
    
protected function resolveRequestFromContainer()
    {
        return 
$this->resolveJsonApiRequestFrom(parent::resolveRequestFromContainer());
    }

    
/**
     * Create a new resource collection instance.
     *
     * @param  mixed  $resource
     * @return IlluminateHttpResourcesJsonApiAnonymousResourceCollection
     */
    #[Override]
    
protected static function newCollection($resource)
    {
        return new 
AnonymousResourceCollection($resource, static::class);
    }

    
/**
     * Set the string that should wrap the outer-most resource array.
     *
     * @param  string  $value
     * @return never
     *
     * @throws RuntimeException
     */
    #[Override]
    
public static function wrap($value)
    {
        throw new 
BadMethodCallException(sprintf('Using %s() method is not allowed.'__METHOD__));
    }

    
/**
     * Disable wrapping of the outer-most resource array.
     *
     * @return never
     */
    #[Override]
    
public static function withoutWrapping()
    {
        throw new 
BadMethodCallException(sprintf('Using %s() method is not allowed.'__METHOD__));
    }

    
/**
     * Flush the resource's global state.
     *
     * @return void
     */
    #[Override]
    
public static function flushState()
    {
        
parent::flushState();

        static::
$jsonApiInformation = [];
        static::
$maxRelationshipDepth 5;
    }
}
Онлайн: 1
Реклама