This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
generated from spaceonfire/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from spaceonfire/develop
1.3.0
- Loading branch information
Showing
30 changed files
with
439 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.2-dev" | ||
"dev-master": "1.3-dev" | ||
} | ||
}, | ||
"config": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spaceonfire\Type; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Webmozart\Assert\Assert; | ||
|
||
abstract class AbstractAggregatedType implements Type, IteratorAggregate | ||
{ | ||
/** | ||
* @var Type[] | ||
*/ | ||
protected $types; | ||
/** | ||
* @var string | ||
*/ | ||
protected $delimiter; | ||
|
||
public function __construct(iterable $types, string $delimiter) | ||
{ | ||
$types = array_values($this->prepareTypes($types)); | ||
|
||
Assert::allIsInstanceOf($types, Type::class); | ||
Assert::minCount($types, 2); | ||
|
||
$this->types = $types; | ||
$this->delimiter = $delimiter; | ||
} | ||
|
||
private function prepareTypes(iterable $types): array | ||
{ | ||
$output = []; | ||
|
||
foreach ($types as $type) { | ||
if ($type instanceof static) { | ||
$output += $this->prepareTypes($type); | ||
continue; | ||
} | ||
|
||
$output[(string)$type] = $type; | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->types); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return implode($this->delimiter, $this->types); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spaceonfire\Type\Factory; | ||
|
||
abstract class AbstractAggregatedTypeFactory implements TypeFactoryInterface | ||
{ | ||
use TypeFactoryTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $delimiter; | ||
|
||
public function __construct(string $delimiter) | ||
{ | ||
$this->delimiter = $delimiter; | ||
} | ||
|
||
private function split(string $string): array | ||
{ | ||
return (explode($this->delimiter, $string, 2) ?: []) + ['', '']; | ||
} | ||
|
||
final protected function parse(string $type): ?array | ||
{ | ||
if ($this->parent === null) { | ||
return null; | ||
} | ||
|
||
$type = $this->removeWhitespaces($type); | ||
|
||
[$left, $right] = $this->split($type); | ||
|
||
if ($left === '' || $right === '') { | ||
return null; | ||
} | ||
|
||
while (!$this->parent->supports($left)) { | ||
[$appendLeft, $right] = $this->split($right); | ||
|
||
if ($appendLeft !== '') { | ||
$left .= $this->delimiter . $appendLeft; | ||
} | ||
|
||
if ($right === '') { | ||
break; | ||
} | ||
} | ||
|
||
if (!$this->parent->supports($right) || !$this->parent->supports($left)) { | ||
return null; | ||
} | ||
|
||
return [$left, $right]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
final public function supports(string $type): bool | ||
{ | ||
return $this->parse($type) !== null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.