Skip to content

Commit 26518a7

Browse files
committed
[FEATURE] Add MigrateEmailFlagToEmailTypeFlexFormFractor
1 parent b9c2ae6 commit 26518a7

22 files changed

+597
-41
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
"nette/utils": "^4.0",
1919
"php": "^8.2",
2020
"sebastian/diff": "^5.0",
21+
"shanethehat/pretty-xml": "^1.0",
2122
"symfony/config": "^6.4",
2223
"symfony/console": "^6.4",
2324
"symfony/dependency-injection": "^6.4",
2425
"symfony/filesystem": "^6.4",
2526
"symfony/finder": "^6.4",
27+
"symfony/string": "^7.0",
2628
"symfony/yaml": "^6.0",
2729
"webmozart/assert": "^1.11"
2830
},

packages/fractor-xml/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ext-xml": "*",
1717
"a9f/fractor": "^0.2",
1818
"a9f/fractor-extension-installer": "^0.2",
19+
"shanethehat/pretty-xml": "^1.0",
1920
"webmozart/assert": "^1.11"
2021
},
2122
"require-dev": {

packages/fractor-xml/src/AbstractXmlFractor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
abstract class AbstractXmlFractor implements DomNodeVisitor, XmlFractor
1414
{
15-
private ?File $file = null;
15+
protected ?File $file = null;
1616

17-
public function beforeTraversal(File $file, \DOMNode $rootNode): void
17+
public function beforeTraversal(File $file, \DOMDocument $rootNode): void
1818
{
1919
$this->file = $file;
2020
}
@@ -41,7 +41,7 @@ public function leaveNode(\DOMNode $node): void
4141
// no-op for now
4242
}
4343

44-
public function afterTraversal(\DOMNode $rootNode): void
44+
public function afterTraversal(\DOMDocument $rootNode): void
4545
{
4646
// no-op for now
4747
}

packages/fractor-xml/src/Contract/DomNodeVisitor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
interface DomNodeVisitor
1414
{
15-
public function beforeTraversal(File $file, \DOMNode $rootNode): void;
15+
public function beforeTraversal(File $file, \DOMDocument $rootNode): void;
1616

1717
/**
1818
* @return \DOMNode|DomDocumentIterator::*
@@ -21,5 +21,5 @@ public function enterNode(\DOMNode $node): \DOMNode|int;
2121

2222
public function leaveNode(\DOMNode $node): void;
2323

24-
public function afterTraversal(\DOMNode $rootNode): void;
24+
public function afterTraversal(\DOMDocument $rootNode): void;
2525
}

packages/fractor-xml/src/XmlFileProcessor.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use a9f\Fractor\Exception\ShouldNotHappenException;
1010
use a9f\FractorXml\Contract\XmlFractor;
1111
use a9f\FractorXml\ValueObjectFactory\DomDocumentFactory;
12+
use PrettyXml\Formatter as PrettyXMLFormatter;
1213

1314
/**
1415
* @implements FileProcessor<XmlFractor>
@@ -20,6 +21,7 @@
2021
*/
2122
public function __construct(
2223
private DomDocumentFactory $domDocumentFactory,
24+
private PrettyXMLFormatter $formatter,
2325
private iterable $rules
2426
) {
2527
}
@@ -42,6 +44,8 @@ public function handle(File $file, iterable $appliedRules): void
4244
$iterator->traverseDocument($file, $document);
4345

4446
$newXml = $this->saveXml($document);
47+
// TODO make the indentation configurable in fractor config
48+
$newXml = $this->formatter->format($newXml);
4549

4650
if ($newXml === $originalXml) {
4751
return;

packages/fractor-xml/tests/DomDocumentIteratorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function __construct(
231231
) {
232232
}
233233

234-
public function beforeTraversal(File $file, \DOMNode $rootNode): void
234+
public function beforeTraversal(File $file, \DOMDocument $rootNode): void
235235
{
236236
$this->calls[] = sprintf('%s:beforeTraversal:%s', $this->visitorName, $rootNode->nodeName);
237237
}
@@ -247,7 +247,7 @@ public function leaveNode(\DOMNode $node): void
247247
$this->calls[] = sprintf('%s:leaveNode:%s', $this->visitorName, $node->nodeName);
248248
}
249249

250-
public function afterTraversal(\DOMNode $rootNode): void
250+
public function afterTraversal(\DOMDocument $rootNode): void
251251
{
252252
$this->calls[] = sprintf('%s:afterTraversal:%s', $this->visitorName, $rootNode->nodeName);
253253
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Helper;
6+
7+
class ArrayUtility
8+
{
9+
/**
10+
* @return string[]
11+
*
12+
* @see GeneralUtility::trimExplode in TYPO3 Core
13+
*/
14+
public static function trimExplode(string $delimiter, string $string): array
15+
{
16+
if ($delimiter === '') {
17+
throw new \InvalidArgumentException('Please define a correct delimiter');
18+
}
19+
20+
return array_map('trim', explode($delimiter, $string));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Helper;
6+
7+
class StringUtility
8+
{
9+
/**
10+
* Check if an item exists in a comma-separated list of items.
11+
*
12+
* @param string $list Comma-separated list of items (string)
13+
* @param string $item Item to check for
14+
* @return bool TRUE if $item is in $list
15+
*
16+
* @see GeneralUtility::inList in TYPO3 Core
17+
*/
18+
public static function inList(string $list, string $item): bool
19+
{
20+
return str_contains(',' . $list . ',', ',' . $item . ',');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Tests\Helper;
6+
7+
use a9f\Fractor\Helper\ArrayUtility;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ArrayUtilityTest extends TestCase
13+
{
14+
/**
15+
* @param string[] $expectedResult
16+
*/
17+
#[DataProvider('trimExplodeReturnsCorrectResultDataProvider')]
18+
#[Test]
19+
public function trimExplodeReturnsCorrectResult(string $delimiter, string $testString, array $expectedResult): void
20+
{
21+
self::assertSame($expectedResult, ArrayUtility::trimExplode($delimiter, $testString));
22+
}
23+
24+
public static function trimExplodeReturnsCorrectResultDataProvider(): array
25+
{
26+
return [
27+
'spaces at element start and end' => [
28+
',',
29+
' a , b , c ,d ,, e,f,',
30+
['a', 'b', 'c', 'd', '', 'e', 'f', ''],
31+
],
32+
];
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Tests\Helper;
6+
7+
use a9f\Fractor\Helper\StringUtility;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class StringUtilityTest extends TestCase
13+
{
14+
#[DataProvider('inListForItemContainedReturnsTrueDataProvider')]
15+
#[Test]
16+
public function inListForItemContainedReturnsTrue(string $haystack): void
17+
{
18+
self::assertTrue(StringUtility::inList($haystack, 'findme'));
19+
}
20+
21+
/**
22+
* Data provider for inListForItemContainedReturnsTrue.
23+
*/
24+
public static function inListForItemContainedReturnsTrueDataProvider(): array
25+
{
26+
return [
27+
'Element as second element of four items' => ['one,findme,three,four'],
28+
'Element at beginning of list' => ['findme,one,two'],
29+
'Element at end of list' => ['one,two,findme'],
30+
'One item list' => ['findme'],
31+
];
32+
}
33+
34+
#[DataProvider('inListForItemNotContainedReturnsFalseDataProvider')]
35+
#[Test]
36+
public function inListForItemNotContainedReturnsFalse(string $haystack): void
37+
{
38+
self::assertFalse(StringUtility::inList($haystack, 'findme'));
39+
}
40+
41+
/**
42+
* Data provider for inListForItemNotContainedReturnsFalse.
43+
*/
44+
public static function inListForItemNotContainedReturnsFalseDataProvider(): array
45+
{
46+
return [
47+
'Four item list' => ['one,two,three,four'],
48+
'One item list' => ['one'],
49+
'Empty list' => [''],
50+
];
51+
}
52+
}

packages/typo3-fractor/composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "a9f/typo3-fractor",
3-
"description": "TYPO3 extension for the File Read-Analyse-Change TOol. Allows modifying XML files",
3+
"description": "TYPO3 extension for the File Read-Analyse-Change Tool. Allows modifying XML files",
44
"license": "MIT",
55
"type": "fractor-extension",
66
"authors": [
@@ -12,12 +12,15 @@
1212
],
1313
"require": {
1414
"php": "^8.2",
15+
"ext-dom": "*",
1516
"a9f/fractor": "^0.2",
1617
"a9f/fractor-doc-generator": "^0.2",
1718
"a9f/fractor-extension-installer": "^0.2",
1819
"a9f/fractor-fluid": "^0.2",
1920
"a9f/fractor-xml": "^0.2",
20-
"a9f/fractor-yaml": "^0.2"
21+
"a9f/fractor-yaml": "^0.2",
22+
"shanethehat/pretty-xml": "^1.0",
23+
"symfony/string": "^7.0"
2124
},
2225
"require-dev": {
2326
"ergebnis/composer-normalize": "^2.42",

packages/typo3-fractor/config/typo3-11.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5+
use FlexForm\MigrateEmailFlagToEmailTypeFlexFormFractor;
56
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
67

78
return static function (ContainerConfigurator $containerConfigurator): void {
89
$services = $containerConfigurator->services();
910
$services->defaults()
1011
->autoconfigure()
1112
->autowire();
13+
$services->set(MigrateEmailFlagToEmailTypeFlexFormFractor::class);
1214
};

0 commit comments

Comments
 (0)