Skip to content

Commit 05957b3

Browse files
committed
Add a new HTMLType
1 parent 9ff2ce3 commit 05957b3

20 files changed

Lines changed: 570 additions & 37 deletions

File tree

rector.tests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Flow\ETL\FlowContext;
2828
use Flow\Types\Type\Logical\DateTimeType;
2929
use Flow\Types\Type\Logical\DateType;
30+
use Flow\Types\Type\Logical\HTMLType;
3031
use Flow\Types\Type\Logical\JsonType;
3132
use Flow\Types\Type\Logical\ListType;
3233
use Flow\Types\Type\Logical\MapType;
@@ -165,6 +166,7 @@
165166
new NewObjectToFunction(UuidType::class, 'Flow\ETL\DSL\type_uuid'),
166167
new NewObjectToFunction(XMLElementType::class, 'Flow\ETL\DSL\type_xml_element'),
167168
new NewObjectToFunction(XMLType::class, 'Flow\ETL\DSL\type_xml'),
169+
new NewObjectToFunction(HTMLType::class, 'Flow\ETL\DSL\type_html'),
168170

169171
// Extractors
170172
new NewObjectToFunction(CacheExtractor::class, 'from_cache'),

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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ enum_entry,
2020
uuid_entry,
2121
xml_element_entry,
2222
xml_entry};
23-
use function Flow\Types\DSL\{type_date, type_datetime, type_json, type_optional, type_string, type_time, type_uuid, type_xml, type_xml_element};
23+
use function Flow\Types\DSL\{type_date,
24+
type_datetime,
25+
type_html,
26+
type_json,
27+
type_optional,
28+
type_string,
29+
type_time,
30+
type_uuid,
31+
type_xml,
32+
type_xml_element};
2433
use Flow\ETL\Exception\{InvalidArgumentException,
2534
RuntimeException,
2635
SchemaDefinitionNotFoundException};
@@ -91,13 +100,11 @@ public function create(string $entryName, mixed $value, Schema|Definition|null $
91100

92101
if ($stringChecker->isJson()) {
93102
$valueType = type_json();
94-
}
95-
96-
if ($stringChecker->isUuid()) {
103+
} elseif ($stringChecker->isUuid()) {
97104
$valueType = type_uuid();
98-
}
99-
100-
if ($stringChecker->isXML()) {
105+
} elseif ($stringChecker->isHTML()) {
106+
$valueType = type_html();
107+
} elseif ($stringChecker->isXML()) {
101108
$valueType = type_xml();
102109
}
103110
}

src/core/etl/src/Flow/ETL/Schema/Definition.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,35 @@
55
namespace Flow\ETL\Schema;
66

77
use function Flow\ETL\DSL\is_nullable;
8-
use function Flow\Types\DSL\{type_array, type_boolean, type_date, type_datetime, type_enum, type_equals, type_float, type_integer, type_is, type_is_any, type_json, type_list, type_map, type_mixed, type_optional, type_string, type_structure, type_time, type_uuid, type_xml, type_xml_element, types};
8+
use function Flow\Types\DSL\{type_array,
9+
type_boolean,
10+
type_date,
11+
type_datetime,
12+
type_enum,
13+
type_equals,
14+
type_float,
15+
type_html,
16+
type_integer,
17+
type_is,
18+
type_is_any,
19+
type_json,
20+
type_list,
21+
type_map,
22+
type_mixed,
23+
type_optional,
24+
type_string,
25+
type_structure,
26+
type_time,
27+
type_uuid,
28+
type_xml,
29+
type_xml_element,
30+
types};
931
use Flow\ETL\Exception\{InvalidArgumentException, RuntimeException};
1032
use Flow\ETL\Row\{Entry, EntryReference, Reference};
1133
use Flow\Types\Type;
1234
use Flow\Types\Type\Logical\{ListType, MapType, OptionalType, StructureType};
1335
use Flow\Types\Type\{Native\FloatType, Native\IntegerType, Native\UnionType, TypeFactory};
14-
use Flow\Types\Value\Uuid;
36+
use Flow\Types\Value\{HTMLDocument, Uuid};
1537

1638
/**
1739
* @template-covariant T
@@ -117,6 +139,14 @@ public static function fromArray(array $definition) : self
117139
);
118140
}
119141

142+
/**
143+
* @return Definition<HTMLDocument>
144+
*/
145+
public static function html(string|Reference $entry, bool $nullable = false, ?Metadata $metadata = null) : self
146+
{
147+
return new self($entry, type_html(), $nullable, $metadata);
148+
}
149+
120150
/**
121151
* @return Definition<int>
122152
*/

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
UuidType,
2121
XMLElementType,
2222
XMLType};
23+
use Flow\Types\Type\Logical\HTMLType;
2324
use Flow\Types\Type\Native\{BooleanType, EnumType, FloatType, IntegerType, StringType};
2425

2526
final readonly class PHPSchemaFormatter implements SchemaFormatter
@@ -114,6 +115,7 @@ private function formatSchema(Schema $schema, int $level = 1) : string
114115
TimeType::class,
115116
JsonType::class,
116117
UuidType::class,
118+
HTMLType::class,
117119
XMLType::class,
118120
XMLElementType::class,
119121
DateTimeType::class => $this->simpleType($definition),
@@ -174,16 +176,17 @@ private function mapType(Definition $definition) : string
174176
private function simpleType(Definition $definition) : string
175177
{
176178
$reflection = match ($definition->type()::class) {
177-
StringType::class => new \ReflectionFunction("\Flow\ETL\DSL\string_schema"),
178-
IntegerType::class => new \ReflectionFunction("\Flow\ETL\DSL\integer_schema"),
179-
BooleanType::class => new \ReflectionFunction("\Flow\ETL\DSL\bool_schema"),
180-
DateType::class => new \ReflectionFunction("\Flow\ETL\DSL\date_schema"),
181-
DateTimeType::class => new \ReflectionFunction("\Flow\ETL\DSL\datetime_schema"),
182-
TimeType::class => new \ReflectionFunction("\Flow\ETL\DSL\\time_schema"),
183-
JsonType::class => new \ReflectionFunction("\Flow\ETL\DSL\\json_schema"),
184-
UuidType::class => new \ReflectionFunction("\Flow\ETL\DSL\\uuid_schema"),
185-
XMLType::class => new \ReflectionFunction("\Flow\ETL\DSL\\xml_schema"),
186-
XMLElementType::class => new \ReflectionFunction("\Flow\ETL\DSL\\xml_element_schema"),
179+
StringType::class => new \ReflectionFunction('\Flow\ETL\DSL\string_schema'),
180+
IntegerType::class => new \ReflectionFunction('\Flow\ETL\DSL\integer_schema'),
181+
BooleanType::class => new \ReflectionFunction('\Flow\ETL\DSL\bool_schema'),
182+
DateType::class => new \ReflectionFunction('\Flow\ETL\DSL\date_schema'),
183+
DateTimeType::class => new \ReflectionFunction('\Flow\ETL\DSL\datetime_schema'),
184+
TimeType::class => new \ReflectionFunction('\Flow\ETL\DSL\time_schema'),
185+
JsonType::class => new \ReflectionFunction('\Flow\ETL\DSL\json_schema'),
186+
UuidType::class => new \ReflectionFunction('\Flow\ETL\DSL\uuid_schema'),
187+
XMLType::class => new \ReflectionFunction('\Flow\ETL\DSL\xml_schema'),
188+
XMLElementType::class => new \ReflectionFunction('\Flow\ETL\DSL\xml_element_schema'),
189+
HTMLType::class => new \ReflectionFunction('\Flow\ETL\DSL\html_schema'),
187190
default => throw new RuntimeException('Type ' . $definition->type()->toString() . ' is not a simple definition'),
188191
};
189192

src/core/etl/tests/Flow/ETL/Tests/Unit/Function/CastTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function cast_provider() : array
4444
'xml_to_array' => [$xml, 'array', ['root' => ['foo' => ['@attributes' => ['baz' => 'buz'], '@value' => 'bar']]]],
4545
'string_to_xml' => [$xmlString, 'xml', $xml],
4646
'xml_to_string' => [$xml, 'string', '<root><foo baz="buz">bar</foo></root>'],
47+
'full_xml_to_string' => [$fullXMLString, 'string', '<root><foo baz="buz">bar</foo></root>'],
4748
'datetime' => [new \DateTimeImmutable('2023-01-01 00:00:00 UTC'), 'string', '2023-01-01T00:00:00+00:00'],
4849
'datetime_to_date' => [new \DateTimeImmutable('2023-01-01 00:01:00 UTC'), 'date', new \DateTimeImmutable('2023-01-01T00:00:00+00:00')],
4950
'string_to_timezone' => ['UTC', 'timezone', new \DateTimeZone('UTC')],

src/core/etl/tests/Flow/ETL/Tests/Unit/Row/EntryFactoryTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum_schema,
3636
use function Flow\Types\DSL\{type_datetime, type_float, type_integer, type_list, type_map, type_string, type_structure, type_time_zone};
3737
use Flow\ETL\Exception\{InvalidArgumentException, SchemaDefinitionNotFoundException};
3838
use Flow\ETL\Row\Entry\TimeEntry;
39-
use Flow\ETL\Row\EntryFactory;
39+
use Flow\ETL\Row\{Entry, EntryFactory};
4040
use Flow\ETL\Schema\Metadata;
4141
use Flow\ETL\Tests\Fixtures\Enum\BackedIntEnum;
4242
use PHPUnit\Framework\Attributes\DataProvider;
@@ -47,6 +47,24 @@ final class EntryFactoryTest extends TestCase
4747
{
4848
private EntryFactory $entryFactory;
4949

50+
public static function provide_recognized_data() : \Generator
51+
{
52+
yield 'json' => [
53+
$json = '{"id":1}',
54+
json_entry('e', $json),
55+
];
56+
57+
yield 'xml' => [
58+
$xml = '<root><foo>1</foo><bar>2</bar><baz>3</baz></root>',
59+
xml_entry('e', $xml),
60+
];
61+
62+
yield 'uuid' => [
63+
$uuid = '00000000-0000-0000-0000-000000000000',
64+
uuid_entry('e', $uuid),
65+
];
66+
}
67+
5068
public static function provide_unrecognized_data() : \Generator
5169
{
5270
yield 'json alike' => [
@@ -369,6 +387,18 @@ public function test_object() : void
369387
$this->entryFactory->create('e', new \ArrayIterator([1, 2]));
370388
}
371389

390+
/**
391+
* @param Entry<mixed> $entry
392+
*/
393+
#[DataProvider('provide_recognized_data')]
394+
public function test_recognized_data_set_same_as_provided(string $input, Entry $entry) : void
395+
{
396+
self::assertEquals(
397+
$entry,
398+
$this->entryFactory->create('e', $input)
399+
);
400+
}
401+
372402
public function test_string() : void
373403
{
374404
self::assertEquals(

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/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/AutoCastTransformerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace Flow\ETL\Tests\Unit\Transformer;
66

77
use function Flow\ETL\DSL\{array_to_rows, config, flow_context};
8-
use Flow\ETL\Tests\FlowTestCase;
98
use Flow\ETL\Transformer\AutoCastTransformer;
109
use Flow\Types\Type\AutoCaster;
10+
use PHPUnit\Framework\TestCase;
1111

12-
final class AutoCastTransformerTest extends FlowTestCase
12+
final class AutoCastTransformerTest extends TestCase
1313
{
1414
public function test_transforming_row() : void
1515
{

src/lib/types/src/Flow/Types/DSL/functions.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
UuidType,
2727
XMLElementType,
2828
XMLType};
29+
use Flow\Types\Type\Logical\HTMLType;
2930
use Flow\Types\Type\Native\{ArrayType,
3031
BooleanType,
3132
CallableType,
@@ -39,7 +40,7 @@
3940
ResourceType,
4041
StringType,
4142
UnionType};
42-
use Flow\Types\Value\Uuid;
43+
use Flow\Types\Value\{HTMLDocument, Uuid};
4344
use UnitEnum;
4445

4546
/**
@@ -435,6 +436,15 @@ function type_literal(bool|float|int|string $value) : LiteralType
435436
return new LiteralType($value);
436437
}
437438

439+
/**
440+
* @return Type<HTMLDocument>
441+
*/
442+
#[DocumentationDSL(module: Module::TYPES, type: DSLType::TYPE)]
443+
function type_html() : Type
444+
{
445+
return new HTMLType();
446+
}
447+
438448
/**
439449
* @template T
440450
*

0 commit comments

Comments
 (0)