Skip to content

Commit

Permalink
Added PHP 7.4 types (#544)
Browse files Browse the repository at this point in the history
* Added type hints

* Added all types in src

* Fixed type errors in tests

* fixess

* Fixed testss

* cs fixes
  • Loading branch information
Nyholm authored Mar 7, 2022
1 parent f560f36 commit 5bf32b5
Show file tree
Hide file tree
Showing 35 changed files with 369 additions and 1,176 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-20.04]
php: [7.2, 7.3, 7.4, 8.0, 8.1]
php: [7.4, 8.0, 8.1]

name: League - PHP ${{ matrix.php }} on ${{ matrix.os }}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"sort-packages": true
},
"require": {
"php": ">=7.2"
"php": ">=7.4"
},
"require-dev": {
"doctrine/orm": "^2.5",
"illuminate/contracts": "~5.0",
"mockery/mockery": "^1.3",
"pagerfanta/pagerfanta": "~1.0.0",
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "~3.4",
"zendframework/zend-paginator": "~2.3"
},
Expand Down
142 changes: 27 additions & 115 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Serializer\DataArraySerializer;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\Serializer\Serializer;

/**
* Manager
Expand All @@ -26,123 +26,79 @@ class Manager
{
/**
* Array of scope identifiers for resources to include.
*
* @var array
*/
protected $requestedIncludes = [];
protected array $requestedIncludes = [];

/**
* Array of scope identifiers for resources to exclude.
*
* @var array
*/
protected $requestedExcludes = [];
protected array $requestedExcludes = [];

/**
* Array of requested fieldsets.
*
* @var array
*/
protected $requestedFieldsets = [];
protected array $requestedFieldsets = [];

/**
* Array containing modifiers as keys and an array value of params.
*
* @var array
*/
protected $includeParams = [];
protected array $includeParams = [];

/**
* The character used to separate modifier parameters.
*
* @var string
*/
protected $paramDelimiter = '|';
protected string $paramDelimiter = '|';

/**
* Upper limit to how many levels of included data are allowed.
*
* @var int
*/
protected $recursionLimit = 10;
protected int $recursionLimit = 10;

/**
* @var SerializerAbstract|null
*/
protected $serializer;
protected ?Serializer $serializer = null;

/**
* Factory used to create new configured scopes.
*
* @var ScopeFactoryInterface
*/
private $scopeFactory;
private ScopeFactoryInterface $scopeFactory;

public function __construct(ScopeFactoryInterface $scopeFactory = null)
{
$this->scopeFactory = $scopeFactory ?: new ScopeFactory();
}

/**
* Create Data.
*
* Main method to kick this all off. Make a resource then pass it over, and use toArray()
*
* @param ResourceInterface $resource
* @param string $scopeIdentifier
* @param Scope $parentScopeInstance
*
* @return Scope
*/
public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
{
public function createData(
ResourceInterface $resource,
?string $scopeIdentifier = null,
Scope $parentScopeInstance = null
): Scope {
if ($parentScopeInstance !== null) {
return $this->scopeFactory->createChildScopeFor($this, $parentScopeInstance, $resource, $scopeIdentifier);
}

return $this->scopeFactory->createScopeFor($this, $resource, $scopeIdentifier);
}

/**
* Get Include Params.
*
* @param string $include
*
* @return \League\Fractal\ParamBag
*/
public function getIncludeParams($include)
public function getIncludeParams(string $include): ParamBag
{
$params = isset($this->includeParams[$include]) ? $this->includeParams[$include] : [];

return new ParamBag($params);
}

/**
* Get Requested Includes.
*
* @return array
*/
public function getRequestedIncludes()
public function getRequestedIncludes(): array
{
return $this->requestedIncludes;
}

/**
* Get Requested Excludes.
*
* @return array
*/
public function getRequestedExcludes()
public function getRequestedExcludes(): array
{
return $this->requestedExcludes;
}

/**
* Get Serializer.
*
* @return SerializerAbstract
*/
public function getSerializer()
public function getSerializer(): Serializer
{
if (! $this->serializer) {
$this->serializer = new DataArraySerializer();
Expand All @@ -152,13 +108,9 @@ public function getSerializer()
}

/**
* Parse Include String.
*
* @param array|string $includes Array or csv string of resources to include
*
* @return $this
*/
public function parseIncludes($includes)
public function parseIncludes($includes): self
{
// Wipe these before we go again
$this->requestedIncludes = $this->includeParams = [];
Expand Down Expand Up @@ -231,10 +183,8 @@ public function parseIncludes($includes)
* @param array $fieldsets Array of fields to include. It must be an array whose keys
* are resource types and values an array or a string
* of the fields to return, separated by a comma
*
* @return $this
*/
public function parseFieldsets(array $fieldsets)
public function parseFieldsets(array $fieldsets): self
{
$this->requestedFieldsets = [];
foreach ($fieldsets as $type => $fields) {
Expand All @@ -247,39 +197,25 @@ public function parseFieldsets(array $fieldsets)
}
return $this;
}

/**
* Get requested fieldsets.
*
* @return array
*/
public function getRequestedFieldsets()
public function getRequestedFieldsets(): array
{
return $this->requestedFieldsets;
}

/**
* Get fieldset params for the specified type.
*
* @param string $type
*
* @return \League\Fractal\ParamBag|null
*/
public function getFieldset($type)
public function getFieldset(string $type): ?ParamBag
{
return !isset($this->requestedFieldsets[$type]) ?
null :
new ParamBag($this->requestedFieldsets[$type]);
}

/**
* Parse Exclude String.
*
* @param array|string $excludes Array or csv string of resources to exclude
*
* @return $this
*/
public function parseExcludes($excludes)
public function parseExcludes($excludes): self
{
$this->requestedExcludes = [];

Expand All @@ -306,45 +242,27 @@ public function parseExcludes($excludes)
return $this;
}

/**
* Set Recursion Limit.
*
* @param int $recursionLimit
*
* @return $this
*/
public function setRecursionLimit($recursionLimit)
public function setRecursionLimit(int $recursionLimit): self
{
$this->recursionLimit = $recursionLimit;

return $this;
}

/**
* Set Serializer
*
* @param SerializerAbstract $serializer
*
* @return $this
*/
public function setSerializer(SerializerAbstract $serializer)
public function setSerializer(Serializer $serializer): self
{
$this->serializer = $serializer;

return $this;
}

/**
* Auto-include Parents
*
* Look at the requested includes and automatically include the parents if they
* are not explicitly requested. E.g: [foo, bar.baz] becomes [foo, bar, bar.baz]
*
* @internal
*
* @return void
*/
protected function autoIncludeParents()
protected function autoIncludeParents(): void
{
$parsed = [];

Expand All @@ -364,18 +282,12 @@ protected function autoIncludeParents()
}

/**
* Trim to Acceptable Recursion Level
*
* Strip off any requested resources that are too many levels deep, to avoid DiCaprio being chased
* by trains or whatever the hell that movie was about.
*
* @internal
*
* @param string $includeName
*
* @return string
*/
protected function trimToAcceptableRecursionLevel($includeName)
protected function trimToAcceptableRecursionLevel(string $includeName): string
{
return implode('.', array_slice(explode('.', $includeName), 0, $this->recursionLimit));
}
Expand Down
Loading

0 comments on commit 5bf32b5

Please sign in to comment.