-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrii Savluk
committed
Apr 4, 2023
1 parent
427ad80
commit 61ebcdd
Showing
8 changed files
with
183 additions
and
11 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
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,14 @@ | ||
<?php | ||
|
||
namespace Savks\Negotiator\Support\DTO; | ||
|
||
/** | ||
* @template TOriginalValueType | ||
*/ | ||
abstract class ConstValue extends Value | ||
{ | ||
/** | ||
* @return TOriginalValueType | ||
*/ | ||
abstract public function originalValue(): mixed; | ||
} |
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,101 @@ | ||
<?php | ||
|
||
namespace Savks\Negotiator\Support\DTO; | ||
|
||
use BackedEnum; | ||
use Closure; | ||
use RuntimeException; | ||
use Savks\Negotiator\Exceptions\UnexpectedValue; | ||
|
||
use Savks\Negotiator\Support\Types\{ | ||
BooleanType, | ||
ConstBooleanType, | ||
ConstNumberType, | ||
ConstStringType, | ||
NumberType, | ||
StringType, | ||
Types | ||
}; | ||
|
||
class OneOfConstValue extends NullableValue | ||
{ | ||
/** | ||
* @param ConstValue[] $values | ||
*/ | ||
public function __construct( | ||
protected readonly mixed $source, | ||
protected readonly array $values, | ||
protected readonly string|Closure|null $accessor = null | ||
) { | ||
} | ||
|
||
protected function finalize(): string|int|float|bool|null | ||
{ | ||
$value = $this->resolveValueFromAccessor( | ||
$this->accessor, | ||
$this->source, | ||
$this->sourcesTrace | ||
); | ||
|
||
if ($this->accessor && last($this->sourcesTrace) !== $this->source) { | ||
$this->sourcesTrace[] = $this->source; | ||
} | ||
|
||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
$isMatched = null; | ||
|
||
foreach ($this->values as $constValue) { | ||
$isMatched = $constValue->originalValue() === $value; | ||
|
||
if ($isMatched) { | ||
break; | ||
} | ||
} | ||
|
||
if (! $isMatched) { | ||
$types = []; | ||
|
||
foreach ($this->values as $constValue) { | ||
if ($constValue instanceof ConstEnumValue) { | ||
$types[] = 'BackedEnum<' . $constValue->originalValue()::class . '>'; | ||
} else { | ||
foreach ($constValue->compileTypes()->types as $type) { | ||
$types[] = match (true) { | ||
$type instanceof ConstBooleanType => $type->value ? 'true' : 'false', | ||
|
||
$type instanceof ConstNumberType, $type instanceof ConstStringType => $type->value, | ||
|
||
$type instanceof BooleanType => 'bool', | ||
$type instanceof NumberType => 'numeric', | ||
$type instanceof StringType => 'string', | ||
|
||
default => throw new RuntimeException( | ||
\sprintf('Unprocessed type "%s".', $type::class) | ||
) | ||
}; | ||
} | ||
} | ||
} | ||
|
||
throw new UnexpectedValue($types, $value); | ||
} | ||
|
||
return $value instanceof BackedEnum ? $value->value : $value; | ||
} | ||
|
||
protected function types(): Types | ||
{ | ||
$types = []; | ||
|
||
foreach ($this->values as $value) { | ||
$types[] = $value->compileTypes()->types; | ||
} | ||
|
||
return new Types( | ||
\array_merge(...$types) | ||
); | ||
} | ||
} |