Skip to content

Commit d4f6211

Browse files
committed
Microoptimization
1 parent f2d5af4 commit d4f6211

10 files changed

+46
-16
lines changed

src/core/etl/src/Flow/ETL/PHP/Type/Caster/ArrayCastingHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public function supports(Type $type) : bool
2020
public function value(mixed $value, Type $type, Caster $caster) : mixed
2121
{
2222
try {
23-
if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {
24-
return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR);
25-
}
26-
2723
if (\is_array($value)) {
2824
return $value;
2925
}
3026

27+
if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {
28+
return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR);
29+
}
30+
3131
if ($value instanceof \DOMDocument) {
3232
return (new XMLConverter())->toArray($value);
3333
}

src/core/etl/src/Flow/ETL/PHP/Type/Caster/BooleanCastingHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21+
if (\is_bool($value)) {
22+
return $value;
23+
}
24+
2125
if (\is_string($value)) {
2226
if (\in_array(\mb_strtolower($value), ['true', '1', 'yes', 'on'], true)) {
2327
return true;

src/core/etl/src/Flow/ETL/PHP/Type/Caster/EnumCastingHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21+
/** @var EnumType $type */
22+
if ($value instanceof $type->class) {
23+
return $value;
24+
}
25+
2126
try {
2227
/** @var EnumType $type */
2328
$enumClass = $type->class;

src/core/etl/src/Flow/ETL/PHP/Type/Caster/FloatCastingHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21+
if (\is_float($value)) {
22+
return $value;
23+
}
24+
2125
if ($value instanceof \DateTimeImmutable) {
2226
return (float) $value->format('Uu');
2327
}

src/core/etl/src/Flow/ETL/PHP/Type/Caster/IntegerCastingHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21+
if (\is_int($value)) {
22+
return $value;
23+
}
24+
2125
if ($value instanceof \DateTimeImmutable) {
2226
return (int) $value->format('Uu');
2327
}

src/core/etl/src/Flow/ETL/PHP/Type/Caster/ObjectCastingHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function supports(Type $type) : bool
1919

2020
public function value(mixed $value, Type $type, Caster $caster) : mixed
2121
{
22+
if (\is_object($value)) {
23+
return $value;
24+
}
25+
2226
/** @var ObjectType $type */
2327
try {
2428
$object = (object) $value;

src/core/etl/src/Flow/ETL/PHP/Type/Caster/StringCastingHandler.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21-
if ($value === null) {
22-
return null;
23-
}
24-
2521
if (\is_string($value)) {
2622
return $value;
2723
}

src/core/etl/src/Flow/ETL/PHP/Type/Caster/StructureCastingHandler.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public function supports(Type $type) : bool
1818

1919
public function value(mixed $value, Type $type, Caster $caster) : mixed
2020
{
21-
if ($value === null && !$type->nullable()) {
22-
throw new CastingException($value, $type);
23-
}
24-
2521
/** @var StructureType $type */
2622
try {
2723
if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {

src/core/etl/src/Flow/ETL/PHP/Type/Caster/UuidCastingHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function supports(Type $type) : bool
1919

2020
public function value(mixed $value, Type $type, Caster $caster) : mixed
2121
{
22+
if ($value instanceof Uuid) {
23+
return $value;
24+
}
25+
2226
if (\is_string($value)) {
2327
return new Uuid($value);
2428
}

src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Flow\ETL\PHP\Type\Caster;
66

7+
use function Flow\ETL\DSL\type_string;
78
use function Flow\ETL\DSL\type_xml;
89
use Flow\ETL\Exception\CastingException;
910
use Flow\ETL\PHP\Type\Caster;
@@ -19,6 +20,10 @@ public function supports(Type $type) : bool
1920

2021
public function value(mixed $value, Type $type, Caster $caster) : mixed
2122
{
23+
if ($value instanceof \DOMDocument) {
24+
return $value;
25+
}
26+
2227
if (\is_string($value)) {
2328
$doc = new \DOMDocument();
2429

@@ -29,10 +34,18 @@ public function value(mixed $value, Type $type, Caster $caster) : mixed
2934
return $doc;
3035
}
3136

32-
if ($value instanceof \DOMDocument) {
33-
return $value;
34-
}
37+
try {
38+
$stringValue = $caster->to(type_string())->value($value);
3539

36-
throw new CastingException($value, $type);
40+
$doc = new \DOMDocument();
41+
42+
if (!@$doc->loadXML($stringValue)) {
43+
throw new CastingException($stringValue, type_xml());
44+
}
45+
46+
return $doc;
47+
} catch (CastingException $e) {
48+
throw new CastingException($value, type_xml(), $e);
49+
}
3750
}
3851
}

0 commit comments

Comments
 (0)