Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -17,16 +17,20 @@
"role": "Developer"
}
],
"config": {
"sort-packages": true
},
"require": {
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"doctrine/orm": "^2.5",
"illuminate/contracts": "~5.0",
"squizlabs/php_codesniffer": "~1.5",
"mockery/mockery": "~0.9",
"pagerfanta/pagerfanta": "~1.0.0",
"zendframework/zend-paginator":"~2.3"
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~1.5",
"zendframework/zend-paginator": "~2.3"
},
"suggest": {
"illuminate/pagination": "The Illuminate Pagination component.",

View File

@@ -38,6 +38,13 @@ class Manager
*/
protected $requestedExcludes = [];
/**
* Array of requested fieldsets.
*
* @var array
*/
protected $requestedFieldsets = [];
/**
* Array containing modifiers as keys and an array value of params.
*
@@ -66,6 +73,18 @@ class Manager
*/
protected $serializer;
/**
* Factory used to create new configured scopes.
*
* @var ScopeFactoryInterface
*/
private $scopeFactory;
public function __construct(ScopeFactoryInterface $scopeFactory = null)
{
$this->scopeFactory = $scopeFactory ?: new ScopeFactory();
}
/**
* Create Data.
*
@@ -79,18 +98,11 @@ class Manager
*/
public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
{
$scopeInstance = new Scope($this, $resource, $scopeIdentifier);
// Update scope history
if ($parentScopeInstance !== null) {
// This will be the new children list of parents (parents parents, plus the parent)
$scopeArray = $parentScopeInstance->getParentScopes();
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();
$scopeInstance->setParentScopes($scopeArray);
return $this->scopeFactory->createChildScopeFor($this, $parentScopeInstance, $resource, $scopeIdentifier);
}
return $scopeInstance;
return $this->scopeFactory->createScopeFor($this, $resource, $scopeIdentifier);
}
/**
@@ -208,6 +220,49 @@ class Manager
return $this;
}
/**
* Parse field parameter.
*
* @param array $fieldsets Array of fields to include. It must be an array
* whose keys are resource types and values a string
* of the fields to return, separated by a comma
*
* @return $this
*/
public function parseFieldsets(array $fieldsets)
{
$this->requestedFieldsets = [];
foreach ($fieldsets as $type => $fields) {
//Remove empty and repeated fields
$this->requestedFieldsets[$type] = array_unique(array_filter(explode(',', $fields)));
}
return $this;
}
/**
* Get requested fieldsets.
*
* @return array
*/
public function getRequestedFieldsets()
{
return $this->requestedFieldsets;
}
/**
* Get fieldset params for the specified type.
*
* @param string $type
*
* @return \League\Fractal\ParamBag|null
*/
public function getFieldset($type)
{
return !isset($this->requestedFieldsets[$type]) ?
null :
new ParamBag($this->requestedFieldsets[$type]);
}
/**
* Parse Exclude String.
*

View File

@@ -0,0 +1,118 @@
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <me@philsturgeon.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal\Pagination;
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* A paginator adapter for doctrine pagination.
*
* @author Fraser Stockley <fraser.stockley@gmail.com>
*/
class DoctrinePaginatorAdapter implements PaginatorInterface
{
/**
* The paginator instance.
* @var Paginator
*/
private $paginator;
/**
* The route generator.
*
* @var callable
*/
private $routeGenerator;
/**
* Create a new DoctrinePaginatorAdapter.
* @param Paginator $paginator
* @param callable $routeGenerator
*
*/
public function __construct(Paginator $paginator, callable $routeGenerator)
{
$this->paginator = $paginator;
$this->routeGenerator = $routeGenerator;
}
/**
* Get the current page.
*
* @return int
*/
public function getCurrentPage()
{
return ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1;
}
/**
* Get the last page.
*
* @return int
*/
public function getLastPage()
{
return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults());
}
/**
* Get the total.
*
* @return int
*/
public function getTotal()
{
return count($this->paginator);
}
/**
* Get the count.
*
* @return int
*/
public function getCount()
{
return $this->paginator->getIterator()->count();
}
/**
* Get the number per page.
*
* @return int
*/
public function getPerPage()
{
return $this->paginator->getQuery()->getMaxResults();
}
/**
* Get the url for the given page.
*
* @param int $page
*
* @return string
*/
public function getUrl($page)
{
return call_user_func($this->getRouteGenerator(), $page);
}
/**
* Get the the route generator.
*
* @return callable
*/
private function getRouteGenerator()
{
return $this->routeGenerator;
}
}

View File

@@ -0,0 +1,108 @@
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <me@philsturgeon.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal\Pagination;
/**
* A paginator adapter for PhalconPHP/pagination.
*
* @author Thien Tran <fcduythien@gmail.com>
*
*/
class PhalconFrameworkPaginatorAdapter implements PaginatorInterface
{
/**
* A slice of the result set to show in the pagination
*
* @var \Phalcon\Paginator\AdapterInterface
*/
private $paginator;
public function __construct($paginator)
{
$this->paginator = $paginator->getPaginate();
}
/**
* Get the current page.
*
* @return int
*/
public function getCurrentPage()
{
return $this->paginator->current;
}
/**
* Get the last page.
*
* @return int
*/
public function getLastPage()
{
return $this->paginator->last;
}
/**
* Get the total.
*
* @return int
*/
public function getTotal()
{
return $this->paginator->total_items;
}
/**
* Get the count.
*
* @return int
*/
public function getCount()
{
return $this->paginator->total_pages;
}
/**
* Get the number per page.
*
* @return int
*/
public function getPerPage()
{
// $this->paginator->items->count()
// Because when we use raw sql have not this method
return count($this->paginator->items);
}
/**
* Get the next.
*
* @return int
*/
public function getNext()
{
return $this->paginator->next;
}
/**
* Get the url for the given page.
*
* @param int $page
*
* @return string
*/
public function getUrl($page)
{
return $page;
}
}

View File

@@ -31,7 +31,7 @@ class Collection extends ResourceAbstract
protected $data;
/**
* A the paginator instance.
* The paginator instance.
*
* @var PaginatorInterface
*/

View File

@@ -14,7 +14,7 @@ namespace League\Fractal\Resource;
/**
* Item Resource
*
* The Item Resource can stored any mixed data, usually an ORM, ODM or
* The Item Resource can store any mixed data, usually an ORM, ODM or
* other sort of intelligent result, DataMapper model, etc but could
* be a basic array, object, or whatever you like.
*/

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <me@philsturgeon.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal\Resource;
/**
* Primitive Resource
*
* The Primitive Resource can store any primitive data, like a string, integer,
* float, double etc.
*/
class Primitive extends ResourceAbstract
{
//
}

View File

@@ -127,7 +127,7 @@ abstract class ResourceAbstract implements ResourceInterface
* Set the transformer.
*
* @param callable|TransformerAbstract $transformer
*
*
* @return $this
*/
public function setTransformer($transformer)

View File

@@ -14,6 +14,7 @@ namespace League\Fractal;
use InvalidArgumentException;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\Resource\NullResource;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Serializer\SerializerAbstract;
@@ -240,6 +241,9 @@ class Scope
// If the serializer wants the includes to be side-loaded then we'll
// serialize the included data and merge it with the data.
if ($serializer->sideloadIncludes()) {
//Filter out any relation that wasn't requested
$rawIncludedData = array_map(array($this, 'filterFieldsets'), $rawIncludedData);
$includedData = $serializer->includedData($this->resource, $rawIncludedData);
// If the serializer wants to inject additional information
@@ -273,17 +277,55 @@ class Scope
// Pull out all of OUR metadata and any custom meta data to merge with the main level data
$meta = $serializer->meta($this->resource->getMeta());
// in case of returning NullResource we should return null and not to go with array_merge
if (is_null($data)) {
if (!empty($meta)) {
return $meta;
}
return null;
}
return array_merge($data, $meta);
}
/**
* Convert the current data for this scope to JSON.
*
* @param int $options
*
* @return string
*/
public function toJson()
public function toJson($options = 0)
{
return json_encode($this->toArray());
return json_encode($this->toArray(), $options);
}
/**
* Transformer a primitive resource
*
* @return mixed
*/
public function transformPrimitiveResource()
{
if (! ($this->resource instanceof Primitive)) {
throw new InvalidArgumentException(
'Argument $resource should be an instance of League\Fractal\Resource\Primitive'
);
}
$transformer = $this->resource->getTransformer();
$data = $this->resource->getData();
if (null === $transformer) {
$transformedData = $data;
} elseif (is_callable($transformer)) {
$transformedData = call_user_func($transformer, $data);
} else {
$transformer->setCurrentScope($this);
$transformedData = $transformer->transform($data);
}
return $transformedData;
}
/**
@@ -370,6 +412,9 @@ class Scope
$transformedData = $this->manager->getSerializer()->mergeIncludes($transformedData, $includedData);
}
//Stick only with requested fields
$transformedData = $this->filterFieldsets($transformedData);
return [$transformedData, $includedData];
}
@@ -420,4 +465,67 @@ class Scope
{
return empty($this->parentScopes);
}
/**
* Filter the provided data with the requested filter fieldset for
* the scope resource
*
* @internal
*
* @param array $data
*
* @return array
*/
protected function filterFieldsets(array $data)
{
if (!$this->hasFilterFieldset()) {
return $data;
}
$serializer = $this->manager->getSerializer();
$requestedFieldset = iterator_to_array($this->getFilterFieldset());
//Build the array of requested fieldsets with the mandatory serializer fields
$filterFieldset = array_flip(
array_merge(
$serializer->getMandatoryFields(),
$requestedFieldset
)
);
return array_intersect_key($data, $filterFieldset);
}
/**
* Return the requested filter fieldset for the scope resource
*
* @internal
*
* @return \League\Fractal\ParamBag|null
*/
protected function getFilterFieldset()
{
return $this->manager->getFieldset($this->getResourceType());
}
/**
* Check if there are requested filter fieldsets for the scope resource.
*
* @internal
*
* @return bool
*/
protected function hasFilterFieldset()
{
return $this->getFilterFieldset() !== null;
}
/**
* Return the scope resource type.
*
* @internal
*
* @return string
*/
protected function getResourceType()
{
return $this->resource->getResourceKey();
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <me@philsturgeon.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal;
use League\Fractal\Resource\ResourceInterface;
class ScopeFactory implements ScopeFactoryInterface
{
/**
* @param Manager $manager
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
{
return new Scope($manager, $resource, $scopeIdentifier);
}
/**
* @param Manager $manager
* @param Scope $parentScopeInstance
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createChildScopeFor(Manager $manager, Scope $parentScopeInstance, ResourceInterface $resource, $scopeIdentifier = null)
{
$scopeInstance = $this->createScopeFor($manager, $resource, $scopeIdentifier);
// This will be the new children list of parents (parents parents, plus the parent)
$scopeArray = $parentScopeInstance->getParentScopes();
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();
$scopeInstance->setParentScopes($scopeArray);
return $scopeInstance;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <me@philsturgeon.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Fractal;
use League\Fractal\Resource\ResourceInterface;
/**
* ScopeFactoryInterface
*
* Creates Scope Instances for resources
*/
interface ScopeFactoryInterface
{
/**
* @param Manager $manager
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null);
/**
* @param Manager $manager
* @param Scope $parentScope
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createChildScopeFor(Manager $manager, Scope $parentScope, ResourceInterface $resource, $scopeIdentifier = null);
}

View File

@@ -72,10 +72,23 @@ class JsonApiSerializer extends ArraySerializer
unset($resource['data']['attributes']['id']);
if(isset($resource['data']['attributes']['links'])) {
$custom_links = $data['links'];
unset($resource['data']['attributes']['links']);
}
if (isset($resource['data']['attributes']['meta'])){
$resource['data']['meta'] = $data['meta'];
unset($resource['data']['attributes']['meta']);
}
if ($this->shouldIncludeLinks()) {
$resource['data']['links'] = [
'self' => "{$this->baseUrl}/$resourceKey/$id",
];
if(isset($custom_links)) {
$resource['data']['links'] = array_merge($custom_links, $resource['data']['links']);
}
}
return $resource;
@@ -171,16 +184,7 @@ class JsonApiSerializer extends ArraySerializer
}
$includeObjects = $this->createIncludeObjects($includeObject);
foreach ($includeObjects as $object) {
$includeType = $object['type'];
$includeId = $object['id'];
$cacheKey = "$includeType:$includeId";
if (!array_key_exists($cacheKey, $linkedIds)) {
$serializedData[] = $object;
$linkedIds[$cacheKey] = $object;
}
}
list($serializedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey($includeObjects, $linkedIds, $serializedData);
}
}
@@ -244,6 +248,16 @@ class JsonApiSerializer extends ArraySerializer
return $includedData;
}
/**
* Get the mandatory fields for the serializer
*
* @return array
*/
public function getMandatoryFields()
{
return ['id'];
}
/**
* Filter function to delete root objects from array.
*
@@ -381,16 +395,7 @@ class JsonApiSerializer extends ArraySerializer
foreach ($data as $value) {
foreach ($value as $includeObject) {
if (isset($includeObject['included'])) {
foreach ($includeObject['included'] as $object) {
$includeType = $object['type'];
$includeId = $object['id'];
$cacheKey = "$includeType:$includeId";
if (!array_key_exists($cacheKey, $linkedIds)) {
$includedData[] = $object;
$linkedIds[$cacheKey] = $object;
}
}
list($includedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey($includeObject['included'], $linkedIds, $includedData);
}
}
}
@@ -564,4 +569,25 @@ class JsonApiSerializer extends ArraySerializer
return $relationship;
}
/**
* @param $includeObjects
* @param $linkedIds
* @param $serializedData
*
* @return array
*/
private function serializeIncludedObjectsWithCacheKey($includeObjects, $linkedIds, $serializedData)
{
foreach ($includeObjects as $object) {
$includeType = $object['type'];
$includeId = $object['id'];
$cacheKey = "$includeType:$includeId";
if (!array_key_exists($cacheKey, $linkedIds)) {
$serializedData[] = $object;
$linkedIds[$cacheKey] = $object;
}
}
return [$serializedData, $linkedIds];
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace League\Fractal\Serializer;
use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Resource\ResourceInterface;
interface Serializer
{
/**
* Serialize a collection.
*
* @param string $resourceKey
* @param array $data
*
* @return array
*/
public function collection($resourceKey, array $data);
/**
* Serialize an item.
*
* @param string $resourceKey
* @param array $data
*
* @return array
*/
public function item($resourceKey, array $data);
/**
* Serialize null resource.
*
* @return array
*/
public function null();
/**
* Serialize the included data.
*
* @param ResourceInterface $resource
* @param array $data
*
* @return array
*/
public function includedData(ResourceInterface $resource, array $data);
/**
* Serialize the meta.
*
* @param array $meta
*
* @return array
*/
public function meta(array $meta);
/**
* Serialize the paginator.
*
* @param PaginatorInterface $paginator
*
* @return array
*/
public function paginator(PaginatorInterface $paginator);
/**
* Serialize the cursor.
*
* @param CursorInterface $cursor
*
* @return array
*/
public function cursor(CursorInterface $cursor);
public function mergeIncludes($transformedData, $includedData);
/**
* Indicates if includes should be side-loaded.
*
* @return bool
*/
public function sideloadIncludes();
/**
* Hook for the serializer to inject custom data based on the relationships of the resource.
*
* @param array $data
* @param array $rawIncludedData
*
* @return array
*/
public function injectData($data, $rawIncludedData);
/**
* Hook for the serializer to modify the final list of includes.
*
* @param array $includedData
* @param array $data
*
* @return array
*/
public function filterIncludes($includedData, $data);
}

View File

@@ -15,7 +15,7 @@ use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Resource\ResourceInterface;
abstract class SerializerAbstract
abstract class SerializerAbstract implements Serializer
{
/**
* Serialize a collection.
@@ -127,4 +127,14 @@ abstract class SerializerAbstract
{
return $includedData;
}
/**
* Get the mandatory fields for the serializer
*
* @return array
*/
public function getMandatoryFields()
{
return [];
}
}

View File

@@ -14,6 +14,7 @@ namespace League\Fractal;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
use League\Fractal\Resource\Primitive;
use League\Fractal\Resource\ResourceInterface;
/**
@@ -98,7 +99,7 @@ abstract class TransformerAbstract
foreach ($includes as $include) {
if ($scope->isExcluded($include)) {
$includes = array_diff($includes, [$include]);
$includes = array_diff($includes, [$include]);
}
}
@@ -155,7 +156,11 @@ abstract class TransformerAbstract
if ($resource = $this->callIncludeMethod($scope, $include, $data)) {
$childScope = $scope->embedChildScope($include, $resource);
$includedData[$include] = $childScope->toArray();
if ($childScope->getResource() instanceof Primitive) {
$includedData[$include] = $childScope->transformPrimitiveResource();
} else {
$includedData[$include] = $childScope->toArray();
}
}
return $includedData;
@@ -243,6 +248,20 @@ abstract class TransformerAbstract
return $this;
}
/**
* Create a new primitive resource object.
*
* @param mixed $data
* @param callable|null $transformer
* @param string $resourceKey
*
* @return Primitive
*/
protected function primitive($data, $transformer = null, $resourceKey = null)
{
return new Primitive($data, $transformer, $resourceKey);
}
/**
* Create a new item resource object.
*