Skip to content

Commit

Permalink
Improve nullable formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Sep 29, 2023
1 parent 541792a commit 83533bc
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Formatter/XsdTypeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
namespace Soap\WsdlReader\Formatter;

use Soap\Engine\Metadata\Model\XsdType;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;

final class XsdTypeFormatter
{
public function __invoke(XsdType $xsdType): string
{
$meta = $xsdType->getMeta();
$isList = $meta->isList()->unwrapOr(false) && !$meta->isAlias()->unwrapOr(false);
$isNullable = $meta->isNullable()->unwrapOr(false);
$isNullable = (new IsConsideredNullableType())($meta);
$min = $meta->minOccurs()->unwrapOr(1);
$max = $meta->maxOccurs()->unwrapOr(1);

Expand Down
29 changes: 29 additions & 0 deletions src/Metadata/Predicate/IsConsideredNullableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Metadata\Predicate;

use Soap\Engine\Metadata\Model\TypeMeta;

final class IsConsideredNullableType
{
public function __invoke(TypeMeta $meta): bool
{
if ($meta->isNullable()->unwrapOr(false)) {
return true;
}

if ($meta->isNil()->unwrapOr(false)) {
return true;
}

if ($meta->isAttribute()->unwrapOr(false)) {
// If no 'use' is defined, XSD attributes get an implicit default "optional" value
if ($meta->use()->unwrapOr('optional') === 'optional') {
return true;
}
}

return false;
}
}
15 changes: 15 additions & 0 deletions src/Metadata/Predicate/IsConsideredScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Metadata\Predicate;

use Soap\Engine\Metadata\Model\TypeMeta;

final class IsConsideredScalarType
{
public function __invoke(TypeMeta $meta): bool
{
return $meta->isSimple()->unwrapOr(false)
|| $meta->isAttribute()->unwrapOr(false);
}
}
60 changes: 60 additions & 0 deletions tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Test\Unit\Metadata\Predicate;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;

final class IsConsideredNullableTypeTest extends TestCase
{
/**
* @dataProvider provideTests
*
*/
public function test_it_knows_if_a_type_is_considered_nullable(TypeMeta $meta, bool $expected): void
{
static::assertSame($expected, (new IsConsideredNullableType())($meta));
}

public static function provideTests()
{
yield 'empty' => [
(new TypeMeta()),
false,
];
yield 'nullable' => [
(new TypeMeta())->withIsNullable(true),
true,
];
yield 'not-nullable' => [
(new TypeMeta())->withIsNullable(false),
false,
];
yield 'nillable' => [
(new TypeMeta())->withIsNil(true),
true,
];
yield 'not-nillable' => [
(new TypeMeta())->withIsNil(false),
false,
];
yield 'default-attribute' => [
(new TypeMeta())->withIsAttribute(true),
true,
];
yield 'optional-attribute' => [
(new TypeMeta())->withIsAttribute(true)->withUse('optional'),
true,
];
yield 'required-attribute' => [
(new TypeMeta())->withIsAttribute(true)->withUse('required'),
false,
];
yield 'prohibit-attribute' => [
(new TypeMeta())->withIsAttribute(true)->withUse('prohibit'),
false,
];
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Metadata/Predicate/IsConsideredScalarTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Test\Unit\Metadata\Predicate;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredScalarType;

final class IsConsideredScalarTypeTest extends TestCase
{
/**
* @dataProvider provideTests
*
*/
public function test_it_knows_if_a_type_is_considered_scalar(TypeMeta $meta, bool $expected): void
{
static::assertSame($expected, (new IsConsideredScalarType())($meta));
}

public static function provideTests()
{
yield 'not' => [
(new TypeMeta()),
false,
];
yield 'simple' => [
(new TypeMeta())->withIsSimple(true),
true,
];
yield 'attribute' => [
(new TypeMeta())->withIsSimple(true),
true,
];
}
}

0 comments on commit 83533bc

Please sign in to comment.