forked from CuyZ/Valinor
-
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.
misc: narrow union types during node build
- Loading branch information
Showing
3 changed files
with
47 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Tree\Builder; | ||
|
||
use CuyZ\Valinor\Mapper\Tree\Node; | ||
use CuyZ\Valinor\Mapper\Tree\Shell; | ||
use CuyZ\Valinor\Type\Resolver\Union\UnionNarrower; | ||
use CuyZ\Valinor\Type\Types\UnionType; | ||
|
||
/** @internal */ | ||
final class UnionNodeBuilder implements NodeBuilder | ||
{ | ||
private NodeBuilder $delegate; | ||
|
||
private UnionNarrower $unionNarrower; | ||
|
||
public function __construct(NodeBuilder $delegate, UnionNarrower $unionNarrower) | ||
{ | ||
$this->delegate = $delegate; | ||
$this->unionNarrower = $unionNarrower; | ||
} | ||
|
||
public function build(Shell $shell, RootNodeBuilder $rootBuilder): Node | ||
{ | ||
$type = $shell->type(); | ||
$value = $shell->value(); | ||
|
||
if (! $type instanceof UnionType) { | ||
return $this->delegate->build($shell, $rootBuilder); | ||
} | ||
|
||
$narrowedType = $this->unionNarrower->narrow($type, $value); | ||
|
||
return $rootBuilder->build($shell->withType($narrowedType)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.