Skip to content

Commit 43df084

Browse files
committed
Enforce instance of EntryFactory in the join methods
1 parent 365e58e commit 43df084

6 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/core/etl/src/Flow/ETL/DSL/functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
UnionType
204204
};
205205
use Flow\Types\Type\Types;
206+
use Flow\Types\Value\HTMLDocument;
206207
use UnitEnum;
207208

208209
/**
@@ -1957,6 +1958,15 @@ function json_schema(string $name, bool $nullable = false, ?Metadata $metadata =
19571958
return Definition::json($name, $nullable, $metadata);
19581959
}
19591960

1961+
/**
1962+
* @return Definition<HTMLDocument>
1963+
*/
1964+
#[DocumentationDSL(module: Module::CORE, type: DSLType::SCHEMA)]
1965+
function html_schema(string $name, bool $nullable = false, ?Metadata $metadata = null) : Definition
1966+
{
1967+
return Definition::html($name, $nullable, $metadata);
1968+
}
1969+
19601970
/**
19611971
* @return Definition<\DOMDocument>
19621972
*/

src/core/etl/src/Flow/ETL/Row/EntryFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ enum_entry,
1414
json_object_entry,
1515
list_entry,
1616
map_entry,
17-
str_entry,
1817
string_entry,
1918
struct_entry,
2019
time_entry,
@@ -156,7 +155,7 @@ public function createAs(string $entryName, mixed $value, Definition|Type $defin
156155

157156
if (null === $value && $type instanceof OptionalType) {
158157
return match ($type->base()::class) {
159-
StringType::class => str_entry($entryName, null, $metadata),
158+
StringType::class => string_entry($entryName, null, $metadata),
160159
IntegerType::class => int_entry($entryName, null, $metadata),
161160
FloatType::class => float_entry($entryName, null, $metadata),
162161
BooleanType::class => bool_entry($entryName, null, $metadata),
@@ -186,7 +185,7 @@ public function createAs(string $entryName, mixed $value, Definition|Type $defin
186185
}
187186

188187
if ($type instanceof StringType) {
189-
return str_entry($entryName, type_optional($type)->cast($value), $metadata);
188+
return string_entry($entryName, type_optional($type)->cast($value), $metadata);
190189
}
191190

192191
if ($type instanceof IntegerType) {

src/core/etl/src/Flow/ETL/Schema/Formatter/PHPSchemaFormatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private function formatSchema(Schema $schema, int $level = 1) : string
115115
TimeType::class,
116116
JsonType::class,
117117
UuidType::class,
118+
HTMLType::class,
118119
XMLType::class,
119120
XMLElementType::class,
120121
DateTimeType::class => $this->simpleType($definition),

src/core/etl/tests/Flow/ETL/Tests/Unit/Schema/Formatter/PHPSchemaFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
datetime_schema,
1010
enum_schema,
1111
float_schema,
12+
html_schema,
1213
int_schema,
1314
json_schema,
1415
list_schema,
@@ -107,6 +108,7 @@ public function test_formatting_simple_schema() : void
107108
\\Flow\\ETL\\DSL\\uuid_schema("uuid", nullable: true, metadata: \\Flow\\ETL\\DSL\\schema_metadata()),
108109
\\Flow\\ETL\\DSL\\xml_schema("xml", nullable: true, metadata: \\Flow\\ETL\\DSL\\schema_metadata()),
109110
\\Flow\\ETL\\DSL\\xml_element_schema("xml_element", nullable: true, metadata: \\Flow\\ETL\\DSL\\schema_metadata()),
111+
\\Flow\\ETL\\DSL\\html_schema("html", nullable: true, metadata: \\Flow\\ETL\\DSL\\schema_metadata()),
110112
);
111113
PHP,
112114
(new PHPSchemaFormatter())->format(schema(
@@ -120,6 +122,7 @@ public function test_formatting_simple_schema() : void
120122
uuid_schema('uuid', nullable: true),
121123
xml_schema('xml', nullable: true),
122124
xml_element_schema('xml_element', nullable: true),
125+
html_schema('html', nullable: true),
123126
))
124127
);
125128
}

src/lib/types/src/Flow/Types/Type/Logical/NonEmptyStringType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function cast(mixed $value) : string
5050
}
5151

5252
if ($value instanceof \DOMDocument) {
53-
return $this->assert(($value->saveXML($value->documentElement) ?: $value->saveHtml($value->documentElement)) ?: '');
53+
return $this->assert($value->saveXML($value->documentElement) ?: '');
5454
}
5555

5656
if ($value instanceof \DOMElement) {

src/lib/types/src/Flow/Types/Type/Logical/XMLType.php

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

55
namespace Flow\Types\Type\Logical;
66

7-
use function Flow\Types\DSL\type_string;
7+
use function Flow\Types\DSL\{type_string, type_xml};
88
use Flow\Types\Exception\{CastingException, InvalidTypeException};
99
use Flow\Types\Type;
1010

@@ -28,24 +28,26 @@ public function cast(mixed $value) : \DOMDocument
2828
return $value;
2929
}
3030

31-
$document = new \DOMDocument();
32-
3331
if (\is_string($value)) {
34-
if (!$this->isXml($document, $value)) {
35-
throw new CastingException($value, $this);
32+
$doc = new \DOMDocument();
33+
34+
if (!@$doc->loadXML($value)) {
35+
throw new CastingException($value, type_xml());
3636
}
3737

38-
return $document;
38+
return $doc;
3939
}
4040

4141
try {
4242
$stringValue = type_string()->cast($value);
4343

44-
if (!$this->isXml($document, $stringValue)) {
44+
$doc = new \DOMDocument();
45+
46+
if (!@$doc->loadXML((string) $stringValue)) {
4547
throw new CastingException($stringValue, $this);
4648
}
4749

48-
return $document;
50+
return $doc;
4951
} catch (CastingException $e) {
5052
throw new CastingException($value, $this, $e);
5153
}
@@ -54,7 +56,7 @@ public function cast(mixed $value) : \DOMDocument
5456
public function isValid(mixed $value) : bool
5557
{
5658
if ($value instanceof \DOMDocument) {
57-
return $this->isXml($value, (string) $value->saveXML());
59+
return true;
5860
}
5961

6062
return false;
@@ -71,9 +73,4 @@ public function toString() : string
7173
{
7274
return 'xml';
7375
}
74-
75-
private function isXml(\DOMDocument $document, string $value) : bool
76-
{
77-
return (bool) @$document->loadXML($value);
78-
}
7976
}

0 commit comments

Comments
 (0)