-
-
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.
Move some TypeUtil methods to TypeResolver for optimization opportuni…
…ties
- Loading branch information
Showing
12 changed files
with
132 additions
and
95 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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\TypeResolver; | ||
|
||
use Rekalogika\Mapper\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\Model\MixedType; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Rekalogika\Mapper\Util\TypeUtil; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class TypeResolver implements TypeResolverInterface | ||
{ | ||
public static function guessTypeFromVariable(mixed $variable): Type | ||
{ | ||
if (is_object($variable)) { | ||
return TypeFactory::objectOfClass($variable::class); | ||
} | ||
|
||
if (is_null($variable)) { | ||
return TypeFactory::null(); | ||
} | ||
|
||
if (is_array($variable)) { | ||
return TypeFactory::array(); | ||
} | ||
|
||
if (is_bool($variable)) { | ||
return TypeFactory::bool(); | ||
} | ||
|
||
if (is_int($variable)) { | ||
return TypeFactory::int(); | ||
} | ||
|
||
if (is_float($variable)) { | ||
return TypeFactory::float(); | ||
} | ||
|
||
if (is_string($variable)) { | ||
return TypeFactory::string(); | ||
} | ||
|
||
if (is_resource($variable)) { | ||
return TypeFactory::resource(); | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf( | ||
'Cannot determine type of variable "%s"', | ||
get_debug_type($variable), | ||
)); | ||
} | ||
|
||
public function getApplicableTypeStrings(array|Type|MixedType $type): array | ||
{ | ||
if ($type instanceof MixedType) { | ||
$type = ['mixed']; | ||
return $type; | ||
} | ||
|
||
if ($type instanceof Type) { | ||
$type = [$type]; | ||
} | ||
|
||
$typeStrings = []; | ||
|
||
foreach ($type as $type) { | ||
$typeStrings = array_merge($typeStrings, TypeUtil::getAllTypeStrings($type, true)); | ||
} | ||
|
||
return $typeStrings; | ||
} | ||
} |
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.