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

namespace IlluminateHttpResourcesJson;

use 
ArrayAccess;
use 
IlluminateContainerContainer;
use 
IlluminateContractsRoutingUrlRoutable;
use 
IlluminateContractsSupportArrayable;
use 
IlluminateContractsSupportResponsable;
use 
IlluminateDatabaseEloquentJsonEncodingException;
use 
IlluminateHttpJsonResponse;
use 
IlluminateHttpRequest;
use 
IlluminateHttpResourcesConditionallyLoadsAttributes;
use 
IlluminateHttpResourcesDelegatesToResource;
use 
JsonException;
use 
JsonSerializable;

class 
JsonResource implements ArrayAccessJsonSerializableResponsableUrlRoutable
{
    use 
ConditionallyLoadsAttributesDelegatesToResource;

    
/**
     * The resource instance.
     *
     * @var mixed
     */
    
public $resource;

    
/**
     * The additional data that should be added to the top-level resource array.
     *
     * @var array
     */
    
public $with = [];

    
/**
     * The additional meta data that should be added to the resource response.
     *
     * Added during response construction by the developer.
     *
     * @var array
     */
    
public $additional = [];

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

    
/**
     * Create a new resource instance.
     *
     * @param  mixed  $resource
     * @return void
     */
    
public function __construct($resource)
    {
        
$this->resource $resource;
    }

    
/**
     * Create a new resource instance.
     *
     * @param  mixed  ...$parameters
     * @return static
     */
    
public static function make(...$parameters)
    {
        return new static(...
$parameters);
    }

    
/**
     * Create a new anonymous resource collection.
     *
     * @param  mixed  $resource
     * @return IlluminateHttpResourcesJsonAnonymousResourceCollection
     */
    
public static function collection($resource)
    {
        return 
tap(static::newCollection($resource), function ($collection) {
            if (
property_exists(static::class, 'preserveKeys')) {
                
$collection->preserveKeys = (new static([]))->preserveKeys === true;
            }
        });
    }

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

    
/**
     * Resolve the resource to an array.
     *
     * @param  IlluminateHttpRequest|null  $request
     * @return array
     */
    
public function resolve($request null)
    {
        
$data $this->toArray(
            
$request $request ?: Container::getInstance()->make('request')
        );

        if (
$data instanceof Arrayable) {
            
$data $data->toArray();
        } elseif (
$data instanceof JsonSerializable) {
            
$data $data->jsonSerialize();
        }

        return 
$this->filter((array) $data);
    }

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

        return 
is_array($this->resource)
            ? 
$this->resource
            
$this->resource->toArray();
    }

    
/**
     * Convert the model instance to JSON.
     *
     * @param  int  $options
     * @return string
     *
     * @throws IlluminateDatabaseEloquentJsonEncodingException
     */
    
public function toJson($options 0)
    {
        try {
            
$json json_encode($this->jsonSerialize(), $options JSON_THROW_ON_ERROR);
        } catch (
JsonException $e) {
            throw 
JsonEncodingException::forResource($this$e->getMessage());
        }

        return 
$json;
    }

    
/**
     * Get any additional data that should be returned with the resource array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return array
     */
    
public function with(Request $request)
    {
        return 
$this->with;
    }

    
/**
     * Add additional meta data to the resource response.
     *
     * @param  array  $data
     * @return $this
     */
    
public function additional(array $data)
    {
        
$this->additional $data;

        return 
$this;
    }

    
/**
     * Get the JSON serialization options that should be applied to the resource response.
     *
     * @return int
     */
    
public function jsonOptions()
    {
        return 
0;
    }

    
/**
     * Customize the response for a request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  IlluminateHttpJsonResponse  $response
     * @return void
     */
    
public function withResponse(Request $requestJsonResponse $response)
    {
        
//
    
}

    
/**
     * Set the string that should wrap the outer-most resource array.
     *
     * @param  string  $value
     * @return void
     */
    
public static function wrap($value)
    {
        static::
$wrap $value;
    }

    
/**
     * Disable wrapping of the outer-most resource array.
     *
     * @return void
     */
    
public static function withoutWrapping()
    {
        static::
$wrap null;
    }

    
/**
     * Transform the resource into an HTTP response.
     *
     * @param  IlluminateHttpRequest|null  $request
     * @return IlluminateHttpJsonResponse
     */
    
public function response($request null)
    {
        return 
$this->toResponse(
            
$request ?: Container::getInstance()->make('request')
        );
    }

    
/**
     * Create an HTTP response that represents the object.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpJsonResponse
     */
    
public function toResponse($request)
    {
        return (new 
ResourceResponse($this))->toResponse($request);
    }

    
/**
     * Prepare the resource for JSON serialization.
     *
     * @return array
     */
    
public function jsonSerialize(): array
    {
        return 
$this->resolve(Container::getInstance()->make('request'));
    }
}
Онлайн: 0
Реклама