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
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-latest, windows-latest, macOS-latest]
php-versions: ['8.2', '8.3']
php-versions: ['8.2', '8.3', '8.4']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Set autocrlf on windows
Expand All @@ -26,8 +26,8 @@ jobs:
with:
composer-options: "--prefer-dist --optimize-autoloader"
- name: Coding standard
env:
PHP_CS_FIXER_IGNORE_ENV: true
run: composer cs:check
- name: Psalm issues
run: composer analytics
- name: Test suite
run: composer test
92 changes: 0 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ You can configure the converters by passing an associative array to the construc
The available options are the following:

- [mergeAttributes](#mergeattributes): boolean, default true
- [idAsKey](#idaskey): boolean, default true
- [typesAsString](#typesasstring): boolean, default false
- [preserveFirstTag](#preservefirsttag): boolean, default false

Expand Down Expand Up @@ -271,97 +270,6 @@ $array = Converter::create(['mergeAttributes' => false])->convert($xmlString);
*/
```

### idAsKey

> Default: __true__

When this option is set to true, the value of an _id_ attribute or tag is considered as the key of an associative array, i.e.:

```php
<?php declare(strict_types=1);

$xmlString = "
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>Star Wars</title>
<starred>True</starred>
<actor id=\"actorH\" name=\"Harrison Ford\" />
<actor id=\"actorM\" name=\"Mark Hamill\" />
<actor>
<id>actorC</id>
<name>Carrie Fisher</name>
</actor>
</movie>
</movies>";

//idAsKey is true by default
$array = Converter::create()->convert($xmlString);

/*
* $array now contains the following array:
*
* 'movie' => [
* 0 => [
* 'title' => 'Star Wars',
* 'starred' => true,
* 'actorH' => ['name' => 'Harrison Ford'],
* 'actorM' => ['name' => 'Mark Hamill'],
* 'actorC' => ['name' => 'Carrie Fisher']
* ]
* ]
*/
```

Otherwise, if you set this option to _false_ no magic happens:

```php
<?php declare(strict_types=1);

$xmlString = "
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>Star Wars</title>
<starred>True</starred>
<actor id=\"actorH\" name=\"Harrison Ford\" />
<actor id=\"actorM\" name=\"Mark Hamill\" />
<actor>
<id>actorC</id>
<name>Carrie Fisher</name>
</actor>
</movie>
</movies>";

$converter = new Converter(['idAsKey' => false]);
$array = $converter->convert($xmlString);

/*
* $array now contains the following array:
*
* 'movie' => [
* 0 => [
* 'title' => 'Star Wars',
* 'starred' => true,
* 'actor' => [
* 0 => [
* 'id' => 'actorH',
* 'name' => 'Harrison Ford'
* ],
* 1 => [
* 'id' => 'actorM',
* 'name' => 'Mark Hamill'
* ],
* 2 => [
* 'id' => 'actorC',
* 'name' => 'Carrie Fisher'
* ]
* ]
* ]
* ]
*/
```

### typesAsString

> Default: __false__
Expand Down
49 changes: 0 additions & 49 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function convert(string $xmlToParse): array
$array = $this->options['mergeAttributes'] === true ? $this->mergeAttributes($array) : $array;
$array = $this->options['typesAsString'] === false ? $this->convertBool($array) : $array;
$array = $this->options['typesAsString'] === false ? $this->convertEmptyArrayToNull($array) : $this->convertEmptyArrayToNull($array, true);
$array = $this->options['idAsKey'] === true ? $this->idToKey($array) : $array;

return $array;
}
Expand Down Expand Up @@ -102,13 +101,11 @@ private function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'mergeAttributes' => true,
'idAsKey' => true,
'typesAsString' => false,
'preserveFirstTag' => false
]);

$resolver->setAllowedTypes('mergeAttributes', 'bool');
$resolver->setAllowedTypes('idAsKey', 'bool');
$resolver->setAllowedTypes('typesAsString', 'bool');
$resolver->setAllowedTypes('preserveFirstTag', 'bool');
}
Expand Down Expand Up @@ -189,52 +186,6 @@ private function convertEmptyArrayToNull(array $array, bool $toString = false):
}, $array);
}

/**
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArrayAssignment
* @psalm-suppress MixedArrayOffset
*/
private function idToKey(array $array): array
{
$out = [];

foreach ($array as $key => $value) {
if (!is_array($value)) {
$out[$key] = $value;
continue;
}

if (!$this->hasIdToMerge($value)) {
$out[$key] = $this->idToKey($value);
continue;
}

/** @var mixed $v */
foreach ($value as $k => $v) {
if (!is_array($v)) {
$out[$key][$k] = $v;
continue;
}

$out[$v['id']] = array_diff_key($v, ['id' => true]);
}
}

return $out;
}

private function hasIdToMerge(array $array): bool
{
/** @var mixed $value */
foreach ($array as $value) {
if (is_array($value) && array_key_exists('id', $value)) {
return true;
}
}

return false;
}

private function normalizeXml(string $xml): string
{
$xml = preg_replace_callback_array([
Expand Down
59 changes: 0 additions & 59 deletions tests/Datasets/NoIdConversion.php

This file was deleted.

133 changes: 0 additions & 133 deletions tests/Datasets/TestId.php

This file was deleted.

Loading
Loading