Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"php-standard-library/str": "^6.1",
"php-standard-library/type": "^6.1",
"php-standard-library/vec": "^6.1",
"veewee/reflecta": "^1.0.0",
"veewee/reflecta": "^1.1",
"veewee/xml": "^4.10",
"php-soap/engine": "^2.20",
"php-soap/wsdl": "^1.19",
Expand Down
66 changes: 66 additions & 0 deletions src/Coerce/Scalar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Coerce;

use function ltrim;
use function preg_match;

/**
* @internal Non-throwing equivalents of Psl's int, float and bool coercion for XML text values.
*/
final class Scalar
{
/**
* Accepts exactly the strings Psl\Type\int()->coerce() accepts.
*/
public static function int(string $value): ?int
{
$int = (int) $value;
if ($value === (string) $int) {
return $int;
}

// Psl trims leading zeros before comparing, which also turns '0-5' into -5.
$trimmed = ltrim($value, '0');
$int = (int) $trimmed;
if ($trimmed === (string) $int) {
return $int;
}

if ($trimmed === '' && $value !== '') {
return 0;
}

return null;
}

/**
* Accepts exactly the strings Psl\Type\float()->coerce() accepts.
*/
public static function float(string $value): ?float
{
if (preg_match('/^[+-]?(\d+([.]\d*)?([eE][+-]?\d+)?|[.]\d+([eE][+-]?\d+)?)$/', $value) === 1) {
return (float) $value;
}

return null;
}

public static function bool(string $value): ?bool
{
return match ($value) {
'true' => true,
'false' => false,
default => null,
};
}

/**
* Returns the first of int, float and bool that accepts the value, else the value itself.
*/
public static function guess(string $value): int|float|bool|string
{
return self::int($value) ?? self::float($value) ?? self::bool($value) ?? $value;
}
}
40 changes: 24 additions & 16 deletions src/Encoder/ObjectAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use VeeWee\Reflecta\Iso\IsoInterface;
use VeeWee\Reflecta\Lens\LensInterface;
use function Psl\Vec\sort_by;
use function VeeWee\Reflecta\Lens\index;
use function VeeWee\Reflecta\Lens\optional;
use function VeeWee\Reflecta\Lens\property;
use function VeeWee\Reflecta\Lens\optional_index;
use function VeeWee\Reflecta\Lens\optional_property;

final class ObjectAccess
{
Expand Down Expand Up @@ -89,8 +89,10 @@ private static function build(Context $context): self
$shouldLensBeOptional = self::shouldLensBeOptional($propertyTypeMeta);
$normalizedProperties[$normalizedName] = $property;

$encoderLenses[$normalizedName] = self::createEncoderLensForType($shouldLensBeOptional, $normalizedName, $encoder, $type, $property);
$decoderLenses[$normalizedName] = self::createDecoderLensForType($shouldLensBeOptional, $name, $encoder, $type, $property);
$encoderLenses[$normalizedName] = self::createCustomEncoderLens($shouldLensBeOptional, $encoder, $type, $property)
?? optional_property($normalizedName);
$decoderLenses[$normalizedName] = self::createCustomDecoderLens($shouldLensBeOptional, $encoder, $type, $property)
?? optional_index($name);
$isos[$normalizedName] = $encoder->iso($propertyContext);

$isAnyPropertyQualified = $isAnyPropertyQualified || $propertyTypeMeta->isQualified()->unwrapOr(false);
Expand All @@ -106,40 +108,46 @@ private static function build(Context $context): self
}

/**
* @return LensInterface<object|null, object|null, mixed, mixed>
* @return LensInterface<object|null, object|null, mixed, mixed>|null
*/
private static function createEncoderLensForType(
private static function createCustomEncoderLens(
bool $shouldLensBeOptional,
string $normalizedName,
XmlEncoder $encoder,
Type $type,
Property $property,
): LensInterface {
): ?LensInterface {
$lens = match (true) {
$encoder instanceof Feature\DecoratingEncoder => self::createEncoderLensForType($shouldLensBeOptional, $normalizedName, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\DecoratingEncoder => self::createCustomEncoderLens($shouldLensBeOptional, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\ProvidesObjectEncoderLens => $encoder::createObjectEncoderLens($type, $property),
default => property($normalizedName)
default => null,
};

if ($lens === null) {
return null;
}

return $shouldLensBeOptional ? optional($lens) : $lens;
}

/**
* @return LensInterface<array<array-key, mixed>|null, array<array-key, mixed>|null, mixed, mixed>
* @return LensInterface<array<array-key, mixed>|null, array<array-key, mixed>|null, mixed, mixed>|null
*/
private static function createDecoderLensForType(
private static function createCustomDecoderLens(
bool $shouldLensBeOptional,
string $name,
XmlEncoder $encoder,
Type $type,
Property $property,
): LensInterface {
): ?LensInterface {
$lens = match(true) {
$encoder instanceof Feature\DecoratingEncoder => self::createDecoderLensForType($shouldLensBeOptional, $name, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\DecoratingEncoder => self::createCustomDecoderLens($shouldLensBeOptional, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\ProvidesObjectDecoderLens => $encoder::createObjectDecoderLens($type, $property),
default => index($name),
default => null,
};

if ($lens === null) {
return null;
}

return $shouldLensBeOptional ? optional($lens) : $lens;
}

Expand Down
33 changes: 2 additions & 31 deletions src/Encoder/SimpleType/ScalarTypeEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Soap\Encoding\Encoder\SimpleType;

use Psl\Type;
use Soap\Encoding\Coerce\Scalar;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\XmlEncoder;
use Soap\Encoding\Exception\RestrictionException;
Expand Down Expand Up @@ -41,36 +41,7 @@ public function iso(Context $context): Iso
is_bool($value) => (new BoolTypeEncoder())->iso($context)->to($value),
default => throw RestrictionException::unsupportedValueType($context->type, $value)
},
static function (string $value) use ($context): mixed {
try {
return Type\int()->coerce($value);
} catch (Type\Exception\CoercionException) {
}

try {
return Type\float()->coerce($value);
} catch (Type\Exception\CoercionException) {
}

try {
return Type\converted(
Type\string(),
Type\bool(),
static fn (string $value): bool => match ($value) {
'true' => true,
'false' => false,
default => throw RestrictionException::unexpectedEnumType(
$context->type,
['true', 'false'],
$value
)
}
)->coerce($value);
} catch (Type\Exception\CoercionException) {
}

return Type\string()->coerce($value);
}
Scalar::guess(...),
));
}
}
73 changes: 73 additions & 0 deletions tests/Fixture/Encoder/ThrowingLensEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\Fixture\Encoder;

use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\Feature;
use Soap\Encoding\Encoder\XmlEncoder;
use Soap\Encoding\Xml\Node\Element;
use Soap\Engine\Metadata\Model\Property;
use Soap\Engine\Metadata\Model\Type;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Reflecta\Lens\Lens;
use function VeeWee\Reflecta\Lens\index;
use function VeeWee\Reflecta\Lens\property;

/**
* A third-party encoder whose object lenses throw when the property is missing, like reflecta's index() and property() do.
*
* @implements XmlEncoder<string|null, string, string, Element|string>
* @template-implements Feature\ProvidesObjectEncoderLens<object, object, mixed, mixed>
* @template-implements Feature\ProvidesObjectDecoderLens<array, array, mixed, mixed>
*/
final class ThrowingLensEncoder implements
Feature\DisregardXsiInformation,
Feature\ProvidesObjectDecoderLens,
Feature\ProvidesObjectEncoderLens,
XmlEncoder
{
public static int $encoderLensReads = 0;
public static int $decoderLensReads = 0;

/**
* @return Lens<object, object, mixed, mixed>
*/
public static function createObjectEncoderLens(Type $parentType, Property $currentProperty): Lens
{
$lens = property($currentProperty->getName());

/** @var Lens<object, object, mixed, mixed> */
return Lens::readonly(static function (object $data) use ($lens): mixed {
self::$encoderLensReads++;

return $lens->get($data);
});
}

/**
* @return Lens<array, array, mixed, mixed>
*/
public static function createObjectDecoderLens(Type $parentType, Property $currentProperty): Lens
{
$lens = index($currentProperty->getName());

/** @var Lens<array, array, mixed, mixed> */
return Lens::readonly(static function (array $data) use ($lens): mixed {
self::$decoderLensReads++;

return $lens->get($data);
});
}

/**
* @return Iso<string|null, string, string, Element|string>
*/
public function iso(Context $context): Iso
{
return new Iso(
static fn (?string $value): string => $value === null ? '' : '<custom>' . $value . '</custom>',
static fn (Element|string $xml): string => ($xml instanceof Element ? $xml : Element::fromString($xml))->element()->textContent,
);
}
}
9 changes: 9 additions & 0 deletions tests/Integration/IntermediateThrowScenariosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public static function provideScenarios(): iterable
),
];

yield 'encode object of a class that does not declare the properties' => [
'operation' => static fn (): string => $literal->encode('test', [new class {
public int $id = 1;
}])->getRequest(),
'expected' => self::literalRequest(
'<testParam><id>1</id><address xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/></testParam>'
),
];

yield 'encode uninitialised typed properties' => [
'operation' => static function () use ($typed): string {
$item = new Item();
Expand Down
Loading
Loading