diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f817611..8b9c019 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,11 +18,11 @@ jobs: runs-on: "ubuntu-latest" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: none tools: composer-normalize env: @@ -35,11 +35,11 @@ jobs: runs-on: "ubuntu-latest" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: none tools: composer-require-checker env: @@ -49,7 +49,7 @@ jobs: shell: bash run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} @@ -64,11 +64,11 @@ jobs: runs-on: "ubuntu-latest" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: none tools: cs2pr, phpcs env: @@ -81,11 +81,11 @@ jobs: runs-on: "ubuntu-latest" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: none tools: cs2pr, php-cs-fixer env: @@ -98,11 +98,11 @@ jobs: runs-on: "ubuntu-latest" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: none tools: composer:v2, phpstan env: @@ -112,7 +112,7 @@ jobs: shell: bash run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} @@ -122,16 +122,16 @@ jobs: - name: PHPStan run: phpstan analyse --no-progress --verbose - tests: + phpunit: name: PHP ${{ matrix.php-version }} on ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }} strategy: matrix: operating-system: ["windows-latest", "ubuntu-latest"] - php-version: ['8.2', '8.3', '8.4'] + php-version: ['8.2', '8.3', '8.4', '8.5'] steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Setup PHP uses: shivammathur/setup-php@v2 with: @@ -150,7 +150,7 @@ jobs: shell: bash run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} diff --git a/.github/workflows/sonarqube-cloud.yml b/.github/workflows/sonarqube-cloud.yml index 9abdcbf..1a4fdfa 100644 --- a/.github/workflows/sonarqube-cloud.yml +++ b/.github/workflows/sonarqube-cloud.yml @@ -6,7 +6,7 @@ on: # Actions # shivammathur/setup-php@v2 https://github.com/marketplace/actions/setup-php-action -# SonarSource/sonarqube-scan-action@v6 https://github.com/marketplace/actions/official-sonarqube-scan +# SonarSource/sonarqube-scan-action@v8 https://github.com/marketplace/actions/official-sonarqube-scan jobs: @@ -21,13 +21,13 @@ jobs: exit 1 fi - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Unshallow clone to provide blame information run: git fetch --unshallow - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.4' + php-version: '8.5' coverage: xdebug tools: composer:v2 - name: Install poppler-utils @@ -38,7 +38,7 @@ jobs: id: composer-cache run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} @@ -52,6 +52,6 @@ jobs: sed 's#'$GITHUB_WORKSPACE'#/github/workspace#g' build/coverage/junit.xml > build/sonar-junit.xml sed 's#'$GITHUB_WORKSPACE'#/github/workspace#g' build/coverage/clover.xml > build/sonar-coverage.xml - name: SonarCloud Scan - uses: SonarSource/sonarqube-scan-action@v6 + uses: SonarSource/sonarqube-scan-action@v8 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.phive/phars.xml b/.phive/phars.xml index f95968a..f0deddc 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,9 +1,9 @@ - - - - - + + + + + diff --git a/LICENSE b/LICENSE index 84985b8..7214122 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2022 - 2025 PhpCfdi https://www.phpcfdi.com/ +Copyright (c) 2022 - 2026 PhpCfdi https://www.phpcfdi.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index 3479612..0c71001 100644 --- a/composer.json +++ b/composer.json @@ -32,12 +32,12 @@ "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^7.4", - "phpcfdi/rfc": "^1.1", + "guzzlehttp/guzzle": "^7.4 || ^8.3", + "phpcfdi/rfc": "^1.2", "psr/http-client": "^1.0", - "symfony/css-selector": "^7.0", - "symfony/dom-crawler": "^7.0", - "symfony/process": "^7.0" + "symfony/css-selector": "^7.0 || ^8.0", + "symfony/dom-crawler": "^7.0 || ^8.0", + "symfony/process": "^7.0 || ^8.0" }, "require-dev": { "phpunit/phpunit": "^11.5" @@ -89,6 +89,6 @@ "dev:check-style": "DEV: search for code style errors using composer-normalize, php-cs-fixer and phpcs", "dev:coverage": "DEV: run phpunit with xdebug and storage coverage in build/coverage/html/", "dev:fix-style": "DEV: fix code style errors using composer-normalize, php-cs-fixer and phpcbf", - "dev:test": "DEV: run @dev:check-style, phpunit and phpstan" + "dev:test": "DEV: run @dev:check-style, phpunit, phpstan and composer-require-checker" } } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4c8f56d..9b7af78 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,6 +11,28 @@ versión, aunque sí su incorporación en la rama principal de trabajo, generalm ## Listado de cambios +### Versión 0.2.1 2026-10-21 + +- Se asegura la compatibilidad con PHP 8.5. +- Se agrega compatibilidad con Symfony 8 (`symfony/css-selector`, `symfony/dom-crawler` y `symfony/process`). +- Se agrega compatibilidad con Guzzle 8, se mantiene la compatibilidad con Guzzle 7. +- Se actualiza la dependencia mínima de `phpcfdi/rfc` a la versión 1.2. +- Se corrige la lectura del RFC y del idCIF desde el PDF de la Constancia de Situación Fiscal cuando `pdftotext` + agrega el separador de página (`\f`) pegado al texto (encontrado en la versión de poppler-utils 26.10.0 para Mac). +- Se actualiza el año de la licencia a 2026. +- Se agrega un script funcional de ejemplo en `tests/bin/csf-info-pdf.php` que sirve para obtener la información en + formato JSON del archivo PDF dado. + +Cambios al entorno de desarrollo: + +- En los flujos de trabajo: + - Se actualizan las versiones de las acciones. + - Se agrega PHP 8.5 a la matriz de pruebas. + - Se ejecutan los trabajos en PHP 8.5. + - Se renombra el trabajo `tests` a `phpunit`. +- Se corrige la descripción de `composer dev:test` para incluir `composer-require-checker`. +- Se actualizan las herramientas de desarrollo. + ### Versión 0.2.0 2025-11-10 - Se asegura la compatibilidad con PHP 8.3 y PHP 8.4. diff --git a/src/PdfReader/CsfExtractor.php b/src/PdfReader/CsfExtractor.php index 6edc146..767088f 100644 --- a/src/PdfReader/CsfExtractor.php +++ b/src/PdfReader/CsfExtractor.php @@ -18,7 +18,7 @@ public function __construct(string $contents) if ('' === $contents) { throw new EmptyPdfContentException('Cannot read pdf contents.'); } - $this->lines = explode("\n", $contents); + $this->lines = preg_split('/[\n\f]/', $contents) ?: []; } public function getRfc(): ?string diff --git a/tests/Unit/PdfReader/CsfExtractor/GetCifIdTest.php b/tests/Unit/PdfReader/CsfExtractor/GetCifIdTest.php index b0b3eb4..d91b8ec 100644 --- a/tests/Unit/PdfReader/CsfExtractor/GetCifIdTest.php +++ b/tests/Unit/PdfReader/CsfExtractor/GetCifIdTest.php @@ -15,6 +15,12 @@ public function test_obtains_cif(): void $this->assertSame('12345678', $csfExtractor->getCifId()); } + public function test_obtains_cif_from_content_with_page_breaks(): void + { + $csfExtractor = new CsfExtractor($this->fileContents('csf-content-form-feed.txt')); + $this->assertSame('12345678', $csfExtractor->getCifId()); + } + public function test_bad_content_returns_empty_rfc(): void { $csfExtractor = new CsfExtractor($this->fileContents('csf-bad-content.txt')); diff --git a/tests/Unit/PdfReader/CsfExtractor/GetRfcTest.php b/tests/Unit/PdfReader/CsfExtractor/GetRfcTest.php index 4e85641..9151e12 100644 --- a/tests/Unit/PdfReader/CsfExtractor/GetRfcTest.php +++ b/tests/Unit/PdfReader/CsfExtractor/GetRfcTest.php @@ -17,6 +17,12 @@ public function test_obtains_rfc(): void $this->assertSame('DIM8701081LA', $csfExtractor->getRfc()); } + public function test_obtains_rfc_from_content_with_page_breaks(): void + { + $csfExtractor = new CsfExtractor($this->fileContents('csf-content-form-feed.txt')); + $this->assertSame('DIM8701081LA', $csfExtractor->getRfc()); + } + public function test_bad_content_returns_empty_rfc(): void { $csfExtractor = new CsfExtractor($this->fileContents('csf-bad-content.txt')); diff --git a/tests/_files/csf-content-form-feed.txt b/tests/_files/csf-content-form-feed.txt new file mode 100644 index 0000000..56e8159 --- /dev/null +++ b/tests/_files/csf-content-form-feed.txt @@ -0,0 +1,2 @@ +CONSTANCIA DE SITUACION FISCAL idCIF: 12345678 +RFC: DIM8701081LA \ No newline at end of file diff --git a/tests/bin/csf-info-pdf.php b/tests/bin/csf-info-pdf.php new file mode 100644 index 0000000..f47a6a1 --- /dev/null +++ b/tests/bin/csf-info-pdf.php @@ -0,0 +1,107 @@ + */ + public array $arguments; + + public function __construct(string $command, string ...$arguments) + { + $this->debug = false; + $this->command = $command; + foreach ($arguments as $key => $value) { + if (in_array($value, ['-d', '--debug'])) { + $this->debug = true; + unset($arguments[$key]); + } + } + $this->arguments = array_values($arguments); + } + + public function printHelp(): void + { + $command = basename($this->command); + echo implode(PHP_EOL, [ + "$command - Obtiene la información con base en un archivo PDF", + 'Sintaxis:', + " $command help|-h|--help", + " $command [-d|--debug] CSF-FILE", + 'Argumentos:', + ' --debug|-d Si se encuentra un error, muestra la excepción y todas las excepciones previas.', + ' CSF-FILE Archivo PDF con la constancia de situación fiscal.', + 'Salida:', + ' Información extraída del sitio del SAT en formato JSON', + 'Licencia y autoría:', + ' La misma del proyecto https://github.com/phpcfdi/csf-scraper', + 'Advertencia:', + ' Este es un ejemplo de uso y este script pruebe cambiar en cualquier momento.', + ' Use su propia implementación para evitar conflictos entre versiones.', + '', + '', + ]); + } + + public function __invoke(): int + { + if ([] !== array_intersect($this->arguments, ['--help', '-h', 'help'])) { + $this->printHelp(); + return 0; + } + try { + $this->execute(); + return 0; + } catch (Throwable $exception) { + if ($this->debug) { + for ($current = $exception; null !== $current; $current = $current->getPrevious()) { + $this->printError(sprintf('Exception (%s) %s', $exception::class, $exception->getMessage())); + $this->printError(sprintf('At %s(%s)', $exception->getFile(), $exception->getLine())); + $this->printError($exception->gettraceAsString()); + } + } else { + $this->printError(sprintf('ERROR: %s', $exception->getMessage())); + } + return is_int($exception->getCode()) && $exception->getCode() > 0 ? $exception->getCode() : 1; + } + } + + private function printError(string ...$messages): void + { + foreach ($messages as $message) { + file_put_contents('php://stderr', $message, FILE_APPEND); + file_put_contents('php://stderr', PHP_EOL, FILE_APPEND); + } + } + + public function execute(): void + { + $filename = ''; + + $arguments = $this->arguments; + foreach ($arguments as $argument) { + if ('' === $filename) { + $filename = $argument; + } else { + throw new RuntimeException(sprintf('Unexpected argument "%s".', $argument)); + } + } + if ('' === $filename) { + throw new RuntimeException('Expected argument CSF-FILE is not set.'); + } + if (! file_exists($filename)) { + throw new RuntimeException(sprintf('File "%s" does not exists.', $filename)); + } + + $scraper = Scraper::create(); + $person = $scraper->obtainFromPdfPath($filename); + echo json_encode($person, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE), PHP_EOL; + } +}));