Skip to content

Commit

Permalink
CS Fixing...
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Pollastri committed Nov 13, 2023
1 parent a0416d3 commit 8c16121
Show file tree
Hide file tree
Showing 27 changed files with 393 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.phpunit.result.cache
.phpunit.result.cache
.php-cs-fixer.cache
44 changes: 44 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

$header = <<<'EOF'
This file is part of PHP CS Fixer.
(c) Fabien Potencier <[email protected]>
Dariusz Rumiński <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

$finder = (new PhpCsFixer\Finder())
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->exclude(['dev-tools/phpstan', 'tests/Fixtures'])
->in(__DIR__);

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PHP74Migration' => true,
'@PHP74Migration:risky' => true,
'@PHPUnit100Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']], // one should use PHPUnit built-in method instead
'header_comment' => ['header' => $header],
'modernize_strpos' => true, // needs PHP 8+ or polyfill
'no_useless_concat_operator' => false, // TODO switch back on when the `src/Console/Application.php` no longer needs the concat
])
->setFinder($finder);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/validator": "^6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.19",
"friendsofphp/php-cs-fixer": "^3.38",
"phpunit/phpunit": "^9.0"
},
"autoload": {
Expand Down
12 changes: 12 additions & 0 deletions src/Abstracts/Model.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Abstracts;

use Axiostudio\FatturaElettronica\Contracts\Model as ModelInterface;
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/Model.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Contracts;

use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down
53 changes: 32 additions & 21 deletions src/FatturaElettronica.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica;

use Axiostudio\FatturaElettronica\Handlers\Model as ModelHandler;
use Axiostudio\FatturaElettronica\Handlers\Xml as XmlHandler;
use Axiostudio\FatturaElettronica\Handlers\XmlDatiBeniServizi as DatiBeniServiziHandler;
use Axiostudio\FatturaElettronica\Handlers\Model as ModelHandler;
use Axiostudio\FatturaElettronica\Models\FatturaElettronica as Fattura;

class FatturaElettronica
{
use DatiBeniServiziHandler;
use ModelHandler;
use XmlHandler;
use DatiBeniServiziHandler;

public function compose(
array $FatturaElettronicaHeader,
array $FatturaElettronicaBody,
array $DettaglioLinee,
array $DatiRiepilogo
): array {
return $this->create(
$FatturaElettronicaHeader,
$FatturaElettronicaBody,
$DettaglioLinee,
$DatiRiepilogo
);
}

protected function fileName($header): string
{
$idPaese = (is_array($header[0][0]) && isset($header[0][0][1])) ? $header[0][0][1] : Settings::IdPaeseDefault();
$idCodice = (is_array($header[0][0])) ? $header[0][0][0] : $header[0][0];
$idPaese = (\is_array($header[0][0]) && isset($header[0][0][1])) ? $header[0][0][1] : Settings::IdPaeseDefault();
$idCodice = (\is_array($header[0][0])) ? $header[0][0][0] : $header[0][0];
$progressivoInvio = $header[0][1];

return $idPaese . $idCodice . '_' . $progressivoInvio . '.xml';
return $idPaese.$idCodice.'_'.$progressivoInvio.'.xml';
}

protected function fileContent($header, $body): string
Expand All @@ -35,27 +61,12 @@ protected function create(
array $DettaglioLinee,
array $DatiRiepilogo
): array {

$xmlContent = $this->fileContent($FatturaElettronicaHeader, $FatturaElettronicaBody);
$xmlContent = $this->addDatiBeniServiziToXml($xmlContent, $DettaglioLinee, $DatiRiepilogo);

return [
'fileContent' => $xmlContent,
'fileName' => $this->fileName($FatturaElettronicaHeader)
'fileName' => $this->fileName($FatturaElettronicaHeader),
];
}

public function compose(
array $FatturaElettronicaHeader,
array $FatturaElettronicaBody,
array $DettaglioLinee,
array $DatiRiepilogo
): array {
return $this->create(
$FatturaElettronicaHeader,
$FatturaElettronicaBody,
$DettaglioLinee,
$DatiRiepilogo
);
}
}
22 changes: 18 additions & 4 deletions src/Handlers/Model.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Handlers;

use Symfony\Component\Validator\Validation;
use Axiostudio\FatturaElettronica\Contracts\Model as ModelInterface;
use Symfony\Component\Validator\Validation;

trait Model
{
public function createModel(ModelInterface $model, bool $toArray = false): array|ModelInterface
{
$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->getValidator();
->getValidator()
;

$errors = $validator->validate($model);

if (count($errors) > 0) {
if (\count($errors) > 0) {
$errorsString = (string) $errors;

throw new \Exception($errorsString);
}

Expand All @@ -31,4 +45,4 @@ protected function modelToArray(ModelInterface $model): array
{
return json_decode(json_encode($model), true);
}
};
}
32 changes: 21 additions & 11 deletions src/Handlers/Xml.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Handlers;

use Spatie\ArrayToXml\ArrayToXml;
use Axiostudio\FatturaElettronica\Settings;
use Spatie\ArrayToXml\ArrayToXml;

trait Xml
{
Expand All @@ -14,28 +26,26 @@ public function createXml(array $array): string
return $this->updateXml($xml);
}

protected function updateXml(string $xml): string
public function createXmlBlock(array $array): string
{
$xml = str_replace('<?xml version="1.0"?>', Settings::XmlStart(), $xml);
$xml = str_replace('<root>', '', $xml);
$xml = str_replace('</root>', Settings::XmlEnd(), $xml);
$xml = ArrayToXml::convert($array);

return $xml;
return $this->updateXmlBlock($xml);
}

public function createXmlBlock(array $array): string
protected function updateXml(string $xml): string
{
$xml = ArrayToXml::convert($array);
$xml = str_replace('<?xml version="1.0"?>', Settings::XmlStart(), $xml);
$xml = str_replace('<root>', '', $xml);

return $this->updateXmlBlock($xml);
return str_replace('</root>', Settings::XmlEnd(), $xml);
}

protected function updateXmlBlock(string $xml): string
{
$xml = str_replace('<?xml version="1.0"?>', '', $xml);
$xml = str_replace('<root>', '', $xml);
$xml = str_replace('</root>', '', $xml);

return $xml;
return str_replace('</root>', '', $xml);
}
}
24 changes: 16 additions & 8 deletions src/Handlers/XmlDatiBeniServizi.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Handlers;

use Axiostudio\FatturaElettronica\Handlers\Xml;
use Axiostudio\FatturaElettronica\Handlers\Model;
use Axiostudio\FatturaElettronica\Models\DatiRiepilogo;
use Axiostudio\FatturaElettronica\Models\DettaglioLinee;

trait XmlDatiBeniServizi
{
use Xml;
use Model;
use Xml;

public function addDatiBeniServiziToXml(string $Xml, array $DettaglioLinee = [], array $DatiRiepilogo = []): string
{
Expand All @@ -19,18 +29,16 @@ public function addDatiBeniServiziToXml(string $Xml, array $DettaglioLinee = [],
foreach ($DettaglioLinee as $numero => $linea) {
$data = $this->createModel(new DettaglioLinee(...$linea), true);
$data['NumeroLinea'] = $numero + 1;
$DettaglioLineeBlock .= '<DettaglioLinee>' . $this->createXmlBlock($data) . '</DettaglioLinee>';
$DettaglioLineeBlock .= '<DettaglioLinee>'.$this->createXmlBlock($data).'</DettaglioLinee>';
}

$DatiRiepilogoBlock = '';

foreach ($DatiRiepilogo as $dato) {
$data = $this->createModel(new DatiRiepilogo(...$dato), true);
$DatiRiepilogoBlock .= '<DatiRiepilogo>' . $this->createXmlBlock($data) . '</DatiRiepilogo>';
$DatiRiepilogoBlock .= '<DatiRiepilogo>'.$this->createXmlBlock($data).'</DatiRiepilogo>';
}

$xml = str_replace('<XmlDatiBeniServizi></XmlDatiBeniServizi>', $DettaglioLineeBlock . $DatiRiepilogoBlock, $Xml);

return $xml;
return str_replace('<XmlDatiBeniServizi></XmlDatiBeniServizi>', $DettaglioLineeBlock.$DatiRiepilogoBlock, $Xml);
}
}
17 changes: 13 additions & 4 deletions src/Models/Anagrafica.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Axiostudio\FatturaElettronica\Models;

use Axiostudio\FatturaElettronica\Abstracts\Model;
Expand All @@ -26,8 +38,5 @@ public function __construct(...$args)
}
}

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
//
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void {}
}
Loading

0 comments on commit 8c16121

Please sign in to comment.