Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a FileConverter class #12

Merged
merged 2 commits into from
Sep 9, 2024
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
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
36 changes: 4 additions & 32 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@
/**
* Class to convert an xml string to array
*/
final class Converter
class Converter
{
private array $options;

/**
* Static constructor.
*
* @psalm-suppress PossiblyUnusedMethod Public api
*/
public static function create(array $options = []): self
public static function create(array $options = []): static
{
return new self($options);
return new static($options);
}

public function __construct(array $options = [])
final public function __construct(array $options = [])
{
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
Expand All @@ -51,8 +49,6 @@ public function __construct(array $options = [])
* @return array
*
* @throws ConverterException If errors while parsing XML.
*
* @psalm-suppress PossiblyUnusedMethod Public api
*/
public function convert(string $xmlToParse): array
{
Expand All @@ -70,30 +66,6 @@ public function convert(string $xmlToParse): array
return $array;
}

/**
* Create a PHP array from an XML file.
*
* @param string $filename The XML file to parse.
*
* @return array
*
* @throws \RuntimeException If the file does not exist or it's not readable
*
* @psalm-suppress PossiblyUnusedMethod Public api
*/
public function convertFile(string $filename): array
{
if (!file_exists($filename)) {
throw new \RuntimeException("The file `$filename` does not exist.");
}

if (!is_readable($filename)) {
throw new \RuntimeException("The file `$filename` is not readable: do you have the correct permissions?");
}

return $this->convert(file_get_contents($filename));
}

private function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
Expand Down
44 changes: 44 additions & 0 deletions src/FileConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
/*
* Copyright (c) Cristiano Cinotti 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Susina\XmlToArray;

class FileConverter extends Converter
{
/**
* Create a PHP array from an XML file.
*
* @param string $filename The XML file to parse.
*
* @return array
*
* @throws \RuntimeException If the file does not exist or it's not readable.
*
* @psalm-suppress ParamNameMismatch It's a desired behavior, since in this method the parameter
* is a filename while in the parent methods it's the xml to convert.
*/
public function convert(string $filename): array
{
if (!file_exists($filename)) {
throw new \RuntimeException("The file `$filename` does not exist.");
}

if (!is_readable($filename)) {
throw new \RuntimeException("The file `$filename` is not readable: do you have the correct permissions?");
}

return parent::convert(file_get_contents($filename));
}
}
2 changes: 1 addition & 1 deletion tests/Functional/ConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$xmlFile = __DIR__ . "/../Fixtures/$file.xml";

$expected = include($phpFile);
$actual = Converter::create()->convertFile($xmlFile);
$actual = Converter::create()->convert(file_get_contents($xmlFile));

expect($actual)->toBe($expected);
})->with(['joomla', 'propel']);
16 changes: 0 additions & 16 deletions tests/Unit/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,3 @@
$actual = Converter::create(['preserveFirstTag' => true])->convert($xml);
expect($actual)->toBe($expected);
});

it('converts an XML file to an array', function (string $xml, array $expected) {
$file = vfsStream::newFile("test_file.xml")->at($this->getRoot())->setContent($xml);
$actual = $this->converter->convertFile($file->url());

expect($expected)->toBe($actual);
})->with('Xml');

it('try to convert a not existent file', function () {
$this->converter->convertFile('vfs://root/notexistent.xml');
})->throws(\RuntimeException::class, 'The file `vfs://root/notexistent.xml` does not exist.');

it('try to convert a not readable file', function () {
$file = vfsStream::newFile('notreadable.xml', 200)->at($this->getRoot())->setContent("<root></root>");
$this->converter->convertFile($file->url());
})->throws(\RuntimeException::class, 'The file `vfs://root/notreadable.xml` is not readable: do you have the correct permissions?');
38 changes: 38 additions & 0 deletions tests/Unit/FileConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);
/*
* Copyright (c) Cristiano Cinotti 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use org\bovigo\vfs\vfsStream;
use Susina\XmlToArray\FileConverter;

it('instantiate the correct object via static method', function () {
expect(FileConverter::create())->toBeInstanceOf(FileConverter::class);
});

it('converts an XML file to an array', function (string $xml, array $expected) {
$file = vfsStream::newFile("test_file.xml")->at($this->getRoot())->setContent($xml);
$actual = FileConverter::create()->convert($file->url());

expect($expected)->toBe($actual);
})->with('Xml');

it('try to convert a not existent file', function () {
$converter = new FileConverter();
$converter->convert('vfs://root/notexistent.xml');
})->throws(\RuntimeException::class, 'The file `vfs://root/notexistent.xml` does not exist.');

it('try to convert a not readable file', function () {
$file = vfsStream::newFile('notreadable.xml', 200)->at($this->getRoot())->setContent("<root></root>");
FileConverter::create()->convert($file->url());
})->throws(\RuntimeException::class, 'The file `vfs://root/notreadable.xml` is not readable: do you have the correct permissions?');