Skip to content

Commit

Permalink
Use native union types
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrjones committed Apr 28, 2024
1 parent 94d0a69 commit 509a9c6
Show file tree
Hide file tree
Showing 20 changed files with 25 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
php-versions: ['8.0', '8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3']

runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^8.0|^8.1",
"php": "^8.1",
"symfony/http-client": "^5.4|^6.0",
"spatie/yaml-front-matter": "^2.0",
"erusev/parsedown-extra": "^0.8.1",
Expand Down
4 changes: 1 addition & 3 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Strata\Data;

use Strata\Data\Helper\UnionTypes;
use Strata\Data\Pagination\Pagination;
use Strata\Data\Traits\IterableTrait;

Expand Down Expand Up @@ -35,9 +34,8 @@ public function setCollection(array $collection)
* Add an item to the collection
* @param array|object $item
*/
public function add($item)
public function add(array|object $item)
{
UnionTypes::assert('$item', $item, 'array', 'object');
$this->collection[] = $item;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface CollectionInterface extends \SeekableIterator, \Countable, \ArrayAcces
{
public function setCollection(array $collection);

public function add($item);
public function add(array|object $item);

public function setPagination(Pagination $pagination);

Expand Down
93 changes: 0 additions & 93 deletions src/Helper/UnionTypes.php

This file was deleted.

5 changes: 1 addition & 4 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Strata\Data\Exception\HttpNotFoundException;
use Strata\Data\Exception\InvalidHttpMethodException;
use Strata\Data\Helper\ContentHasher;
use Strata\Data\Helper\UnionTypes;
use Strata\Data\Http\Response\CacheableResponse;
use Strata\Data\Http\Response\DecoratedResponseTrait;
use Strata\Data\Http\Response\SuppressErrorResponse;
Expand Down Expand Up @@ -269,10 +268,8 @@ public function setCacheableMethods(array $methods)
* @throws \InvalidArgumentException
* @throws InvalidHttpMethodException
*/
public static function validMethod($methods, bool $throw = false): bool
public static function validMethod(array|string $methods, bool $throw = false): bool
{
UnionTypes::assert('$methods', $methods, 'array', 'string');

if (!is_array($methods)) {
$methods = [$methods];
}
Expand Down
6 changes: 2 additions & 4 deletions src/Mapper/MapCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Strata\Data\CollectionInterface;
use Strata\Data\Exception\MapperException;
use Strata\Data\Exception\PaginationException;
use Strata\Data\Helper\UnionTypes;
use Strata\Data\Pagination\Pagination;
use Strata\Data\Traits\PaginationPropertyTrait;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
Expand All @@ -21,12 +20,11 @@ class MapCollection extends MapperAbstract implements MapperInterface

/**
* Set data to extract pagination information from
* @param $data
* @param array|object $data
* @return $this
*/
public function fromPaginationData($data): MapCollection
public function fromPaginationData(array|object $data): MapCollection
{
UnionTypes::assert('$data', $data, 'array', 'object');
$this->paginationData = $data;
return $this;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Mapper/MappingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Strata\Data\Mapper;

use Strata\Data\Exception\MapperException;
use Strata\Data\Helper\UnionTypes;
use Strata\Data\Transform\Data\CallableData;
use Strata\Data\Transform\PropertyAccessorInterface;
use Strata\Data\Transform\PropertyAccessorTrait;
Expand Down Expand Up @@ -71,9 +70,8 @@ public function getPropertyPaths(): array
* @param array|object $item Destination item to map data to
* @return mixed
*/
public function mapItem(array $data, $item)
public function mapItem(array $data, array|object $item)
{
UnionTypes::assert('$item', $item, 'array', 'object');
$propertyAccessor = $this->getPropertyAccessor();

// Loop through property paths to map to new item (destination => source)
Expand Down Expand Up @@ -111,7 +109,7 @@ public function mapItem(array $data, $item)
}

// Invalid source type
if (!UnionTypes::is($source, 'string', 'array')) {
if (!is_string($source) && !is_array($source)) {
$type = gettype($source);
if ($type === 'object') {
$type = get_class($source);
Expand Down
5 changes: 3 additions & 2 deletions src/Mapper/MappingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use Symfony\Component\PropertyAccess\PropertyAccessor;

interface MappingStrategyInterface
interface
MappingStrategyInterface
{
public function setPropertyAccessor(PropertyAccessor $propertyAccessor);
public function getPropertyAccessor(): PropertyAccessor;
Expand All @@ -18,5 +19,5 @@ public function getPropertyAccessor(): PropertyAccessor;
* @param array|object $item
* @return mixed
*/
public function mapItem(array $data, $item);
public function mapItem(array $data, array|object $item);
}
10 changes: 2 additions & 8 deletions src/Mapper/WildcardMappingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Strata\Data\Mapper;

use Strata\Data\Helper\UnionTypes;

class WildcardMappingStrategy extends MappingStrategy
{
private array $mapping = [];
Expand Down Expand Up @@ -70,10 +68,8 @@ public function getMappingByRootElement(string $field): array
*
* @param array|string $field
*/
public function addIgnore($field)
public function addIgnore(array|string $field)
{
UnionTypes::assert('$field', $field, 'array', 'string');

if (is_array($field)) {
foreach ($field as $item) {
$this->ignore[] = $this->normaliseFieldName($item);
Expand Down Expand Up @@ -102,10 +98,8 @@ public function isRootElementInIgnore(string $field): bool
* @param array|object $item
* @return mixed
*/
public function mapItem(array $data, $item)
public function mapItem(array $data, array|object $item)
{
UnionTypes::assert('$item', $item, 'array', 'object');

// Loop through all root data to build property path mapping
$propertyPaths = [];
foreach ($data as $field => $value) {
Expand Down
6 changes: 0 additions & 6 deletions src/Query/GraphQLQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public function getDataProvider(): GraphQL
return $this->dataProvider;
}

public function setDataProvider(GraphQL $dataProvider): QueryAbstract
{
parent::setDataProvider($dataProvider);
return $this;
}

/**
* Return query name
*
Expand Down
10 changes: 3 additions & 7 deletions src/Traits/PaginationPropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Strata\Data\Traits;

use Strata\Data\Helper\UnionTypes;
use Strata\Data\Query\QueryInterface;

/**
Expand All @@ -21,9 +20,8 @@ trait PaginationPropertyTrait
* @param string|int $totalResults Property path to retrieve data from, or actual value
* @return $this Fluent interface
*/
public function setTotalResults($totalResults)
public function setTotalResults(string|int $totalResults)
{
UnionTypes::assert('$totalResults', $totalResults, 'string', 'int');
$this->totalResults = $totalResults;
return $this;
}
Expand All @@ -42,9 +40,8 @@ public function getTotalResults()
* @param string|int $resultsPerPage Property path to retrieve data from, or actual value
* @return $this Fluent interface
*/
public function setResultsPerPage($resultsPerPage)
public function setResultsPerPage(string|int $resultsPerPage)
{
UnionTypes::assert('$resultsPerPage', $resultsPerPage, 'string', 'int');
$this->resultsPerPage = $resultsPerPage;
return $this;
}
Expand All @@ -63,9 +60,8 @@ public function getResultsPerPage()
* @param string|int $currentPage Property path to retrieve data from, or actual value
* @return $this Fluent interface
*/
public function setCurrentPage($currentPage)
public function setCurrentPage(string|int $currentPage)
{
UnionTypes::assert('currentPage', $currentPage, 'string', 'int');
$this->currentPage = $currentPage;
return $this;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Transform/Data/CallableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Strata\Data\Transform\Data;

use Strata\Data\Helper\UnionTypes;

class CallableData extends DataAbstract
{
private $callable;
Expand Down Expand Up @@ -53,7 +51,7 @@ public function getCallable(): callable
*/
public function canTransform($data): bool
{
return UnionTypes::is($data, 'array', 'object');
return (is_array($data) || is_object($data));
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Transform/Data/Concatenate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
declare(strict_types=1);

namespace Strata\Data\Transform\Data;

use Strata\Data\Helper\UnionTypes;

class Concatenate extends DataAbstract
{
private array $propertyPaths;
Expand All @@ -23,7 +20,7 @@ public function __construct(...$propertyPaths)
*/
public function canTransform($data): bool
{
return UnionTypes::is($data, 'array', 'object');
return (is_array($data) || is_object($data));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Transform/Data/MapValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Strata\Data\Transform\Data;

use Strata\Data\Helper\UnionTypes;
use Strata\Data\Transform\NotTransformedInterface;
use Strata\Data\Transform\NotTransformedTrait;

Expand Down Expand Up @@ -77,7 +76,7 @@ public function setMapping(array $mapping)
*/
public function canTransform($data): bool
{
return UnionTypes::is($data, 'array', 'object');
return (is_array($data) || is_object($data));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Transform/Data/RenameFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Strata\Data\Transform\Data;

use Strata\Data\Helper\UnionTypes;
use Strata\Data\Transform\NotTransformedInterface;
use Strata\Data\Transform\NotTransformedTrait;
use Symfony\Component\PropertyAccess\PropertyPath;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function setPropertyPaths(array $propertyPaths)
*/
public function canTransform($data): bool
{
return UnionTypes::is($data, 'array', 'object');
return (is_array($data) || is_object($data));
}

/**
Expand Down
Loading

0 comments on commit 509a9c6

Please sign in to comment.