Skip to content

Commit

Permalink
Merge pull request #5 from eclipxe13/master
Browse files Browse the repository at this point in the history
Version 0.3.3
  • Loading branch information
eclipxe13 authored Feb 18, 2020
2 parents 572a3db + a1b83a2 commit 4cb0a3d
Show file tree
Hide file tree
Showing 24 changed files with 247 additions and 64 deletions.
26 changes: 13 additions & 13 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
* text=auto

# Do not put this files on a distribution package (by .gitignore)
/vendor export-ignore
/build export-ignore
/composer.lock export-ignore
/vendor export-ignore
/composer.lock export-ignore

# Do not put this files on a distribution package
/tests/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/build-phar export-ignore
/phpcs.xml.dist export-ignore
phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/build/ export-ignore
/development/ export-ignore
/tests/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# do not include this files on git
/vendor
/build
/composer.lock
9 changes: 7 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ filter:
- 'tests/'
- 'vendor/'

# see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/
build:
dependencies:
override:
- composer self-update --no-interaction --no-progress
- composer remove squizlabs/php_codesniffer friendsofphp/php-cs-fixer phpstan/phpstan phpunit/phpunit --dev --no-interaction --no-progress --no-update
- composer install --no-interaction
nodes:
php:
tests:
override:
- php-scrutinizer-run --enable-security-analysis

tools:
external_code_coverage: true
external_code_coverage: true
38 changes: 24 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,64 @@
language: php

php: ["7.0", "7.1", "7.2", "7.3"]
php: ["7.0", "7.1", "7.2", "7.3", "7.4"]

jobs:
allow_failures:
- php: 7.4 # there is a problem with html2pdf, see https://github.com/spipu/html2pdf/issues/530

addons:
apt:
packages:
- poppler-utils

env:
- FULL_BUILD_PHP_VERSION="7.3"
global:
- BUILD_COVERAGE_PHP_VERSION="7.3"
- BUILD_PHAR_PHP_VERSION="7.3"
- BUILD_PHAR_OUTPUT="build/cfditopdf.phar"

before_script:
- phpenv config-rm xdebug.ini
- phpenv config-rm xdebug.ini || true
- travis_retry composer install --no-progress --no-interaction --prefer-dist

script:
- mkdir -p build/tests/
- vendor/bin/phpcs -sp bin/ src/ tests/ bin/ templates/
- vendor/bin/php-cs-fixer fix --using-cache=no --dry-run --verbose
- |
if [[ $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION ]]; then
php -d zend_extension=xdebug.so vendor/bin/phpunit --testdox --coverage-clover=build/tests/coverage.xml
if [[ $TRAVIS_PHP_VERSION == $BUILD_COVERAGE_PHP_VERSION ]]; then
php -d zend_extension=xdebug.so vendor/bin/phpunit --testdox --coverage-clover=build/coverage.xml
else
vendor/bin/phpunit --testdox
fi
- vendor/bin/phpstan analyse --level max src/ tests/ bin/
- | # do not run on phpstan on PHP 7.0 because it fail with array annotations
if [[ $TRAVIS_PHP_VERSION != "7.0" ]]; then
vendor/bin/phpstan analyse --no-progress --level max src/ tests/ bin/
fi
# build phar
- |
if [[ $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION ]]; then
bash build-phar
test -f build/cfditopdf.phar
if [[ $TRAVIS_PHP_VERSION == $BUILD_PHAR_PHP_VERSION ]]; then
rm -f "${BUILD_PHAR_OUTPUT}"
bash development/build-phar "${BUILD_PHAR_OUTPUT}"
test -f "${BUILD_PHAR_OUTPUT}"
fi
after_script:
# upload test covegare to scrutinizer
- |
if [[ $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION ]]; then
if [[ $TRAVIS_PHP_VERSION == $BUILD_COVERAGE_PHP_VERSION ]]; then
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover build/tests/coverage.xml
php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
fi
deploy:
provider: releases
api_key:
secure: V++o2L8UWr9GLicxWDnT3JFf+VnbxAAbkd1py8y14ISar7XzgfJ4A06KvwvQz2CYZpeHNeG3fakl4HyFuVl/cU+UxZtMAgMTpQs7d33pTpaFSKT8eOesNJQzRqhLr8PDUy6zAoDz+gNjMs7laQqh4PhK9+DoM2+EReno86pWqo6uWdSrv+PNYmIHObrWkAKJo4eZcmCV+4BnpVKmcuyKqYvXaTfSF3gPqZMsByGYn7jitiVu/9/t004BIlahXh0oTj8dpvNEUyWg2LTIa88PrlI6MFrS9vBs+4FVBrElxZxNWTtY/vj3FkDuxn84Yjn9s3q+6pinbmM4240O95VVU/qhlrdsx0RDAfgxZ+yH7fx8aD9zcrVG7tD7FuwgIbTUEJxTj0TBlQIB7zkxDoIygxvBqLgpPQ+/8TjCsja0fwqmrbfB6t2MtgJtLFBc5tAFaHeKkky5FvrR2fJaDg8WzsFR70pLbTqQ8BXjkiXIMKLV2NxQaPpbvlBis/Jq9oa39zy9W2OQB6OJs+2xPIL46mw3W5ZANpste4sUPdalx2vTA+gOrokEKYvzkoSf0zJAjGpO8D0PEObtc/AOybpfgFcXFQ15HiA4U65tdgOEZwmrRRzm/kAa8TewP3KaYM3BaEZvz9+3XP2bZuHLfJdjhiWmBoxFkC4opiLVAUoQ3N0=
skip_cleanup: true
file: build/cfditopdf.phar
file: "${BUILD_PHAR_OUTPUT}"
on:
repo: phpcfdi/cfditopdf
condition: $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION
condition: $TRAVIS_PHP_VERSION == $BUILD_PHAR_PHP_VERSION
tags: true

notifications:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018, 2019 PHPCFDI
Copyright (c) 2018 - 2020 PHPCFDI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ $converter = new \PhpCfdi\CfdiToPdf\Converter(
$converter->createPdfAs($cfdiData, 'output.pdf');
```

To change the way data is translated from `CfdiData` to HTML you could provide a specialized translator to
`Html2PdfBuilder` when the object is constructed.

In the following example is using the default HTML translator that uses Plates, only changing the directory
where templates are located and the template name. The expected result must be compatible with Html2Pdf.

```php
<?php declare(strict_types=1);
$htmlTranslator = new \PhpCfdi\CfdiToPdf\Builders\HtmlTranslators\PlatesHtmlTranslator(
'directory_where_templates_are_located',
'main_template_name'
);
$converter = new \PhpCfdi\CfdiToPdf\Converter(
new \PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder($htmlTranslator)
);
```

## PHP Support

This library is compatible with PHP versions 7.0 and above.
Expand All @@ -85,15 +102,15 @@ and licensed for use under the MIT License (MIT). Please see [LICENSE][] for mor
[source]: https://github.com/phpcfdi/cfditopdf
[release]: https://github.com/phpcfdi/cfditopdf/releases
[license]: https://github.com/phpcfdi/cfditopdf/blob/master/LICENSE
[build]: https://travis-ci.org/phpcfdi/cfditopdf?branch=master
[build]: https://travis-ci.com/phpcfdi/cfditopdf?branch=master
[quality]: https://scrutinizer-ci.com/g/phpcfdi/cfditopdf/
[coverage]: https://scrutinizer-ci.com/g/phpcfdi/cfditopdf/code-structure/master/code-coverage/src
[downloads]: https://packagist.org/packages/phpcfdi/cfditopdf

[badge-source]: https://img.shields.io/badge/source-phpcfdi/cfditopdf-blue?style=flat-square
[badge-release]: https://img.shields.io/github/release/phpcfdi/cfditopdf?style=flat-square
[badge-license]: https://img.shields.io/github/license/phpcfdi/cfditopdf?style=flat-square
[badge-build]: https://img.shields.io/travis/phpcfdi/cfditopdf/master?style=flat-square
[badge-build]: https://img.shields.io/travis/com/phpcfdi/cfditopdf/master?style=flat-square
[badge-quality]: https://img.shields.io/scrutinizer/g/phpcfdi/cfditopdf/master?style=flat-square
[badge-coverage]: https://img.shields.io/scrutinizer/coverage/g/phpcfdi/cfditopdf/master?style=flat-square
[badge-downloads]: https://img.shields.io/packagist/dt/phpcfdi/cfditopdf?style=flat-square
2 changes: 2 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"phpunit/phpunit": "^6.5|^7.0",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^2.4",
"phpstan/phpstan-shim": "^0.9|^0.10|^0.11"
"phpstan/phpstan": "^0.9|^0.10|^0.11|^0.12"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion build-phar → development/build-phar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PHAR_BUILDER_LOCATION="https://github.com/MacFJA/PharBuilder/releases/latest/dow
PHAR_BUILDER_BIN="build/phar-builder"

# this is set in composer.json
PHAR_DESTINATION="build/cfditopdf.phar"
PHAR_DESTINATION="$1"

if [ ! -e "$PHAR_BUILDER_BIN" ]; then
mkdir -p "$(dirname "$PHAR_BUILDER_BIN")"
Expand Down
10 changes: 10 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Version 0.3.3 2020-02-18

- Isolate the translation from CFDI to HTML into an interface `HtmlTranslatorInterface`.
- Implements `PlatesHtmlTranslator` with the current code to translate CFDI to HTML.
- Can setup the `Html2PdfBuilder` by changing the `HtmlTranslatorInterface` to use other templates or other engine.
- Update license year.
- Update Travis-CI & Scrutinizer CI.
- Update phpstan (version 0.12, do not use phpstan-shim).
- Update todo list.

# Version 0.3.2 2019-11-14

- Add *Complemento de pagos* to the generated HTML, thanks @blacktrue
Expand Down
38 changes: 25 additions & 13 deletions docs/TODO.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
# phpcfdi/cfditopdf To Do List

- [X] Otros nodos de los conceptos
- [X] Cadena de origen del TFD
- [X] Liga del código QR
- [X] Formato del código QR
- [X] Construcción de objetos
- [ ] Catálogos para expresar las claves
- [ ] Impresión genérica de complementos
- [ ] Crear nuevos métodos para generar el archivo PDF
- [X] Depend on new version of CfdiUtils
- [X] Move PdfToText to a different library and require it only on development
- [X] Improve testing
- [X] When macfja/phar-builder reaches phive move it from composer script requiere run remove
to phive dependence
Intro: Al completar una tarea pasarla de *Planeada* a *Finalizada*. Escribir fecha o versión del cambio.

# Planeadas

- Revisar que <https://github.com/spipu/html2pdf/issues/530> esté solucionado para desactivar "permitir fallos"
en PHP 7.4 porque `spipu/html2pdf` tiene un bug.
- Cambiar a PHP 7.2 o mayor.
- Al cambiar de versión de PHP, actualizar los métodos que retornan void a que explícitamente lo hangan.
- Modificar `NodeInterface<NodeInterface>` para cuando `eclipxe/cfdiutils` ya lo incluya por sí mismo.
- Catálogos para expresar las claves.
- Impresión genérica de complementos.
- Crear nuevos métodos para generar el archivo PDF.

## Finalizadas

### En versión 0.3.2 o anteriores:

- Otros nodos de los conceptos.
- Cadena de origen del TFD.
- Liga del código QR.
- Formato del código QR.
- Construcción de objetos.
- Depend on new version of CfdiUtils.
- Move PdfToText to a different library and require it only on development.
- Improve testing.
- When macfja/phar-builder reaches phive move it from composer script requiere run remove to phive dependence.
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true
ignoreErrors:
- '#Method PhpCfdi\\CfdiToPdf\\Tests\\\S+::test\S+ has no return typehint specified\.#'
- '#Method PhpCfdi\\CfdiToPdf\\Tests\\\S+::(setUp|tearDown)\(\) has no return typehint specified\.#'

25 changes: 21 additions & 4 deletions src/Builders/Html2PdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@

namespace PhpCfdi\CfdiToPdf\Builders;

use League\Plates\Engine as PlatesEngine;
use PhpCfdi\CfdiToPdf\Builders\HtmlTranslators\HtmlTranslatorInterface;
use PhpCfdi\CfdiToPdf\CfdiData;
use RuntimeException;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Html2Pdf;

class Html2PdfBuilder implements BuilderInterface
{
/** @var HtmlTranslators\HtmlTranslatorInterface */
private $htmlTranslator;

/**
* Html2PdfBuilder constructor.
*
* @param HtmlTranslatorInterface|null $htmlTranslator If NULL will use a generic translator
*/
public function __construct(HtmlTranslatorInterface $htmlTranslator = null)
{
if (null === $htmlTranslator) {
$htmlTranslator = new HtmlTranslators\PlatesHtmlTranslator(
dirname(__DIR__, 2) . '/templates/', // __DIR__ is src/Builders
'generic'
);
}
$this->htmlTranslator = $htmlTranslator;
}

public function build(CfdiData $data, string $destination)
{
file_put_contents($destination, $this->buildPdf($data));
Expand Down Expand Up @@ -46,8 +65,6 @@ public function convertHtmlToPdf(string $html): string

public function convertNodeToHtml(CfdiData $cfdiData): string
{
// __DIR__ is src/Builders
$plates = new PlatesEngine(dirname(__DIR__, 2) . '/templates/');
return $plates->render('generic', ['cfdiData' => $cfdiData]);
return $this->htmlTranslator->translate($cfdiData);
}
}
18 changes: 18 additions & 0 deletions src/Builders/HtmlTranslators/HtmlTranslatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PhpCfdi\CfdiToPdf\Builders\HtmlTranslators;

use PhpCfdi\CfdiToPdf\CfdiData;

interface HtmlTranslatorInterface
{
/**
* Transform CfdiData contents to HTML content
*
* @param CfdiData $cfdiData
* @return string
*/
public function translate(CfdiData $cfdiData): string;
}
46 changes: 46 additions & 0 deletions src/Builders/HtmlTranslators/PlatesHtmlTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace PhpCfdi\CfdiToPdf\Builders\HtmlTranslators;

use League\Plates\Engine as PlatesEngine;
use PhpCfdi\CfdiToPdf\CfdiData;

class PlatesHtmlTranslator implements HtmlTranslatorInterface
{
/** @var string */
private $directory;

/** @var string */
private $template;

/**
* PlatesHtmlTranslator constructor.
*
* @param string $directory
* @param string $template
*/
public function __construct(string $directory, string $template)
{
$this->directory = $directory;
$this->template = $template;
}

public function translate(CfdiData $cfdiData): string
{
// __DIR__ is src/Builders
$plates = new PlatesEngine($this->directory());
return $plates->render($this->template(), ['cfdiData' => $cfdiData]);
}

public function directory(): string
{
return $this->directory;
}

public function template(): string
{
return $this->template;
}
}
Loading

0 comments on commit 4cb0a3d

Please sign in to comment.