Skip to content

Commit

Permalink
Microoptimization
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Jan 21, 2024
1 parent f2d5af4 commit d4f6211
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function supports(Type $type) : bool
public function value(mixed $value, Type $type, Caster $caster) : mixed
{
try {
if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {
return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR);
}

if (\is_array($value)) {
return $value;
}

if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {
return \json_decode($value, true, 512, \JSON_THROW_ON_ERROR);
}

if ($value instanceof \DOMDocument) {
return (new XMLConverter())->toArray($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if (\is_bool($value)) {
return $value;
}

if (\is_string($value)) {
if (\in_array(\mb_strtolower($value), ['true', '1', 'yes', 'on'], true)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
/** @var EnumType $type */
if ($value instanceof $type->class) {
return $value;
}

try {
/** @var EnumType $type */
$enumClass = $type->class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if (\is_float($value)) {
return $value;
}

if ($value instanceof \DateTimeImmutable) {
return (float) $value->format('Uu');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if (\is_int($value)) {
return $value;
}

if ($value instanceof \DateTimeImmutable) {
return (int) $value->format('Uu');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if (\is_object($value)) {
return $value;
}

/** @var ObjectType $type */
try {
$object = (object) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if ($value === null) {
return null;
}

if (\is_string($value)) {
return $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if ($value === null && !$type->nullable()) {
throw new CastingException($value, $type);
}

/** @var StructureType $type */
try {
if (\is_string($value) && (\str_starts_with($value, '{') || \str_starts_with($value, '['))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function supports(Type $type) : bool

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if ($value instanceof Uuid) {
return $value;
}

if (\is_string($value)) {
return new Uuid($value);
}
Expand Down
21 changes: 17 additions & 4 deletions src/core/etl/src/Flow/ETL/PHP/Type/Caster/XMLCastingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\ETL\PHP\Type\Caster;

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

public function value(mixed $value, Type $type, Caster $caster) : mixed
{
if ($value instanceof \DOMDocument) {
return $value;
}

if (\is_string($value)) {
$doc = new \DOMDocument();

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

if ($value instanceof \DOMDocument) {
return $value;
}
try {
$stringValue = $caster->to(type_string())->value($value);

throw new CastingException($value, $type);
$doc = new \DOMDocument();

if (!@$doc->loadXML($stringValue)) {
throw new CastingException($stringValue, type_xml());
}

return $doc;
} catch (CastingException $e) {
throw new CastingException($value, type_xml(), $e);
}
}
}

0 comments on commit d4f6211

Please sign in to comment.