From 9c09d00cb39d149bd42082918a3aa0c204174bbe Mon Sep 17 00:00:00 2001 From: ruff Date: Sat, 14 Sep 2024 13:32:12 +0200 Subject: [PATCH] Initial commit --- .github/ISSUE_TEMPLATE/bug_report.md | 28 + .github/ISSUE_TEMPLATE/config.yml | 1 + .github/ISSUE_TEMPLATE/feature.md | 20 + .github/ISSUE_TEMPLATE/question.md | 17 + .github/dependabot.yml | 13 + .github/pull_request_template.md | 38 ++ .github/workflows/build.php73.ant.yml | 44 ++ .github/workflows/build.php74.ant.yml | 44 ++ .github/workflows/build.php80.ant.yml | 44 ++ .github/workflows/build.php81.ant.yml | 44 ++ .github/workflows/build.php81.release.ant.yml | 44 ++ .github/workflows/build.php82.ant.yml | 44 ++ .github/workflows/build.php83.ant.yml | 44 ++ .github/workflows/maintain-issues-stale.yml | 24 + .gitignore | 85 +++ CODE_OF_CONDUCT.md | 128 +++++ CONTRIBUTING.md | 116 ++++ LICENSE | 21 + README.md | 123 +++++ SECURITY.md | 10 + build.ant.xml | 263 +++++++++ build.xml | 281 ++++++++++ build/phpcsrules.xml | 351 ++++++++++++ build/phpcsrules_psr1.xml | 47 ++ build/phpcsrules_psr12.xml | 268 +++++++++ build/phpcsrules_psr2.xml | 218 ++++++++ build/phpcsrules_squiz.xml | 132 +++++ build/phpdoc.xml | 26 + build/phpmd.xml | 13 + build/phpstan.neon | 9 + build/phpunit.xml | 25 + composer.json | 59 ++ src/ZugferdMailReader.php | 176 ++++++ src/config/ZugferdMailAccount.php | 511 ++++++++++++++++++ src/config/ZugferdMailConfig.php | 154 ++++++ src/handlers/ZugferdMailHandlerCli.php | 64 +++ .../ZugferdMailHandlerCopyMessage.php | 81 +++ .../ZugferdMailHandlerDeleteMessage.php | 36 ++ src/handlers/ZugferdMailHandlerInterface.php | 32 ++ .../ZugferdMailHandlerMoveMessage.php | 81 +++ src/handlers/ZugferdMailHandlerNull.php | 35 ++ src/handlers/ZugferdMailHandlerSaveToFile.php | 126 +++++ tests/TestCase.php | 170 ++++++ tests/testcases/BasicTest.php | 13 + 44 files changed, 4103 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/feature.md create mode 100644 .github/ISSUE_TEMPLATE/question.md create mode 100644 .github/dependabot.yml create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/build.php73.ant.yml create mode 100644 .github/workflows/build.php74.ant.yml create mode 100644 .github/workflows/build.php80.ant.yml create mode 100644 .github/workflows/build.php81.ant.yml create mode 100644 .github/workflows/build.php81.release.ant.yml create mode 100644 .github/workflows/build.php82.ant.yml create mode 100644 .github/workflows/build.php83.ant.yml create mode 100644 .github/workflows/maintain-issues-stale.yml create mode 100644 .gitignore create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 SECURITY.md create mode 100644 build.ant.xml create mode 100644 build.xml create mode 100644 build/phpcsrules.xml create mode 100644 build/phpcsrules_psr1.xml create mode 100644 build/phpcsrules_psr12.xml create mode 100644 build/phpcsrules_psr2.xml create mode 100644 build/phpcsrules_squiz.xml create mode 100644 build/phpdoc.xml create mode 100644 build/phpmd.xml create mode 100644 build/phpstan.neon create mode 100644 build/phpunit.xml create mode 100644 composer.json create mode 100644 src/ZugferdMailReader.php create mode 100644 src/config/ZugferdMailAccount.php create mode 100644 src/config/ZugferdMailConfig.php create mode 100644 src/handlers/ZugferdMailHandlerCli.php create mode 100644 src/handlers/ZugferdMailHandlerCopyMessage.php create mode 100644 src/handlers/ZugferdMailHandlerDeleteMessage.php create mode 100644 src/handlers/ZugferdMailHandlerInterface.php create mode 100644 src/handlers/ZugferdMailHandlerMoveMessage.php create mode 100644 src/handlers/ZugferdMailHandlerNull.php create mode 100644 src/handlers/ZugferdMailHandlerSaveToFile.php create mode 100644 tests/TestCase.php create mode 100644 tests/testcases/BasicTest.php diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..16b0ea6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,28 @@ +--- +name: Bug Report +about: Create a report to help us improve +title: "[BUG]" +labels: bug +assignees: horstoeko + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Additional information:** + - OS: [e.g. Windows/Limux/etc.] + - OS-Version [e.g. Windows10,Debian10,etc.] + - PHP-Version [e.g. PHP7.x,PHP8.x,etc.] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..3ba13e0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: false diff --git a/.github/ISSUE_TEMPLATE/feature.md b/.github/ISSUE_TEMPLATE/feature.md new file mode 100644 index 0000000..9c759bf --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature.md @@ -0,0 +1,20 @@ +--- +name: Feature Request +about: Create a feature request +title: "[FEATURE]" +labels: enhancement +assignees: horstoeko + +--- + +**Describe the feature** +A clear and concise description of what feature/improvement you like to have + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your feature request. + +**Additional context** +Add any other context about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md new file mode 100644 index 0000000..e7e07f4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/question.md @@ -0,0 +1,17 @@ +--- +name: Question +about: Ask a question +title: "[QUESTION]" +labels: question +assignees: horstoeko + +--- + +**Your question** +A clear and concise question + +**Screenshots** +If applicable, add screenshots to help explain your question. + +**Additional context** +Add any other context about the question here. diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..be006de --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +# Keep GitHub Actions up to date with GitHub's Dependabot... +# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot +# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + groups: + github-actions: + patterns: + - "*" # Group all Actions updates into a single larger pull request + schedule: + interval: weekly diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..ab105e8 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,38 @@ +# Description + +Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. + +Fixes # (issue) + +## Type of change + +Please delete options that are not relevant. + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] This change requires a documentation update + +# How Has This Been Tested? + +Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration + +- [ ] Test A +- [ ] Test B + +**Test Configuration**: + +* OS: +* OS Version: +* PHP Version: + +# Checklist: + +- [ ] My code follows the style guidelines of this project +- [ ] I have performed a self-review of my own code +- [ ] I have commented my code, particularly in hard-to-understand areas +- [ ] I have made corresponding changes to the documentation +- [ ] My changes generate no new warnings +- [ ] I have added tests that prove my fix is effective or that my feature works +- [ ] New and existing unit tests pass locally with my changes +- [ ] Any dependent changes have been merged and published in downstream modules \ No newline at end of file diff --git a/.github/workflows/build.php73.ant.yml b/.github/workflows/build.php73.ant.yml new file mode 100644 index 0000000..8d60a57 --- /dev/null +++ b/.github/workflows/build.php73.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 7.3) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '7.3' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/build.php74.ant.yml b/.github/workflows/build.php74.ant.yml new file mode 100644 index 0000000..7829f82 --- /dev/null +++ b/.github/workflows/build.php74.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 7.4) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/build.php80.ant.yml b/.github/workflows/build.php80.ant.yml new file mode 100644 index 0000000..5e0b10a --- /dev/null +++ b/.github/workflows/build.php80.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 8.0) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/build.php81.ant.yml b/.github/workflows/build.php81.ant.yml new file mode 100644 index 0000000..4af02b3 --- /dev/null +++ b/.github/workflows/build.php81.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 8.1) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/build.php81.release.ant.yml b/.github/workflows/build.php81.release.ant.yml new file mode 100644 index 0000000..24f9667 --- /dev/null +++ b/.github/workflows/build.php81.release.ant.yml @@ -0,0 +1,44 @@ +name: Release + +on: + push: + tags: + - "v*" + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: always() + with: + files: "./build/logs/junit.xml" + - name: Make Release + uses: "marvinpinto/action-automatic-releases@latest" + with: + repo_token: "${{ secrets.GITHUB_TOKEN }}" + prerelease: false + files: | + build/dist/doc.zip + build/dist/builddoc.zip diff --git a/.github/workflows/build.php82.ant.yml b/.github/workflows/build.php82.ant.yml new file mode 100644 index 0000000..26567a9 --- /dev/null +++ b/.github/workflows/build.php82.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 8.2) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '8.2' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/build.php83.ant.yml b/.github/workflows/build.php83.ant.yml new file mode 100644 index 0000000..10a244d --- /dev/null +++ b/.github/workflows/build.php83.ant.yml @@ -0,0 +1,44 @@ +name: CI (Ant, PHP 8.3) + +on: + push: + tags-ignore: + - '**' + branches: + - '**' + paths-ignore: + - '**.md' + - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: + +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup PHP with PECL extension + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: imagick, swoole + coverage: xdebug + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + - name: Build with Ant + run: ant -noinput -buildfile build.ant.xml + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: github.event_name != 'pull_request' + with: + files: "./build/logs/junit.xml" diff --git a/.github/workflows/maintain-issues-stale.yml b/.github/workflows/maintain-issues-stale.yml new file mode 100644 index 0000000..922b1d1 --- /dev/null +++ b/.github/workflows/maintain-issues-stale.yml @@ -0,0 +1,24 @@ +name: Close inactive issues + +on: + schedule: + - cron: "0 5 * * *" + workflow_dispatch: + +jobs: + close-issues: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + steps: + - uses: actions/stale@v5 + with: + days-before-issue-stale: 30 + days-before-issue-close: 14 + stale-issue-label: "stale" + stale-issue-message: "This issue is stale because it has been open for 30 days with no activity." + close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale." + days-before-pr-stale: -1 + days-before-pr-close: -1 + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05e2351 --- /dev/null +++ b/.gitignore @@ -0,0 +1,85 @@ +#ignore thumbnails created by windows +Thumbs.db +#Ignore files build by Visual Studio +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +*.dcu +*.dll +*.ini +*.um +[B|b]ackup/* +*/[C|c]ompiled/* +*/_[I|i]n[F|f]uture/* +*.~* +*.$$$ +[P|p][R|r][E|e][F|f][S|s]/* +[B|b]ackup*/* +[D|d]cu/* +[E|e]xport/* +*.key +*.KEY +*.log +*.ini +*.INI +*.um +*.UM +*.$$$ +*.123 +*.jpg +*.png +*.bmp +*.gif +cgi-bin +_backup* +*.zip +*.tar +*.tar.gz +[D|d][E|e][P|p][L|l][O|o][Y|y]/* + +.htpasswd +/nbproject/private/ +/nbproject/ + +!/setup/.htaccess +.vscode +.idea + +/vendor/ +/logs/* +.phpunit.result.cache +/.phpdoc/ +/build/api/ +/build/coverage/ +/build/coverage-html/ +/build/doc/ +/build/logs/ +/build/pdepend/ +/build/phpdox/ +/build/phpdoc/ +/*.phar +/composer.lock + +/examples/Example1.php diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..18c9147 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8e6eae2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,116 @@ + +# Contributing to horstoeko/zugferdmail + +First off, thanks for taking the time to contribute! ❤️ + +All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉 + +> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: +> - Star the project +> - Tweet about it +> - Refer this project in your project's readme +> - Mention the project at local meetups and tell your friends/colleagues + + +## Table of Contents + +- [I Have a Question](#i-have-a-question) +- [I Want To Contribute](#i-want-to-contribute) + - [Reporting Bugs](#reporting-bugs) + - [Suggesting Enhancements](#suggesting-enhancements) + + + +## I Have a Question + +> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/horstoeko/zugferdmail). + +Before you ask a question, it is best to search for existing [Issues](https://github.com/horstoeko/zugferdmail/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first. + +If you then still feel the need to ask a question and need clarification, we recommend the following: + +- Open an [Issue](https://github.com/horstoeko/zugferdmail/issues/new?assignees=horstoeko&labels=question&projects=&template=question.md&title=%5BQUESTION%5D). +- Provide as much context as you can about what you're running into. +- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant. + +We will then take care of the issue as soon as possible. + +## I Want To Contribute + +> ### Legal Notice +> When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license. + +### Reporting Bugs + + +#### Before Submitting a Bug Report + +A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. + +- Make sure that you are using the latest version. +- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/horstoeko/zugferdmail). If you are looking for support, you might want to check [this section](#i-have-a-question)). +- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/horstoeko/zugferdmail/issues?q=label%3Abug). +- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue. +- Collect information about the bug: + - Stack trace (Traceback) + - OS, Platform and Version (Windows, Linux, macOS, x86, ARM) + - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. + - Possibly your input and the output + - Can you reliably reproduce the issue? And can you also reproduce it with older versions? + + +#### How Do I Submit a Good Bug Report? + +> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to . + + +We use GitHub issues to track bugs and errors. If you run into an issue with the project: + +- Open an [Issue](https://github.com/horstoeko/zugferdmail/issues/new?assignees=horstoeko&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D). +- Explain the behavior you would expect and the actual behavior. +- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. +- Provide the information you collected in the previous section. + +Once it's filed: + +- The project team will label the issue accordingly. +- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. +- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution). + + + + +### Suggesting Enhancements + +This section guides you through submitting an enhancement suggestion for horstoeko/zugferdmail, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. + + +#### Before Submitting an Enhancement + +- Make sure that you are using the latest version. +- Read the [documentation](https://github.com/horstoeko/zugferdmail) carefully and find out if the functionality is already covered, maybe by an individual configuration. +- Perform a [search](https://github.com/horstoeko/zugferdmail/issues?q=label%3Aenhancement) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. +- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library. + + +#### How Do I Submit a Good Enhancement Suggestion? + +- Open an [issue](https://github.com/zugferdmail/zugferd/issues/new?assignees=horstoeko&labels=enhancement&projects=&template=feature.md&title=%5BFEATURE%5D). +- Use a **clear and descriptive title** for the issue to identify the suggestion. +- Provide a **step-by-step description of the suggested enhancement** in as many details as possible. +- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. +- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. +- **Explain why this enhancement would be useful** to most horstoeko/zugferdmail users. You may also want to point out the other projects that solved it better and which could serve as inspiration. + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..df7f051 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 horstoeko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..59059c7 --- /dev/null +++ b/README.md @@ -0,0 +1,123 @@ +# Title + +[![Latest Stable Version](https://poser.pugx.org/horstoeko/zugferdmail/v/stable.png)](https://packagist.org/packages/horstoeko/zugferdmail) [![Total Downloads](https://poser.pugx.org/horstoeko/zugferdmail/downloads.png)](https://packagist.org/packages/horstoeko/zugferdmail) [![Latest Unstable Version](https://poser.pugx.org/horstoeko/zugferdmail/v/unstable.png)](https://packagist.org/packages/horstoeko/zugferdmail) [![License](https://poser.pugx.org/horstoeko/zugferdmail/license.png)](https://packagist.org/packages/horstoeko/zugferdmail) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/horstoeko/zugferdmail) + +[![CI (Ant, PHP 7.3)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php73.ant.yml/badge.svg)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php73.ant.yml) [![CI (Ant, PHP 7.4)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php74.ant.yml/badge.svg)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php74.ant.yml) [![CI (PHP 8.0)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php80.ant.yml/badge.svg)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php80.ant.yml) [![CI (PHP 8.1)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php81.ant.yml/badge.svg)](https://github.com/horstoeko/zugferdmail/actions/workflows/build.php81.ant.yml) + +## Table of Contents + +- [Title](#title) + - [Table of Contents](#table-of-contents) + - [Note](#note) + - [License](#license) + - [Overview](#overview) + - [Dependencies](#dependencies) + - [Installation](#installation) + - [Usage](#usage) + - [Configuration](#configuration) + - [Check mail accounts for matching mails:](#check-mail-accounts-for-matching-mails) + - [Predefined handlers](#predefined-handlers) + - [Implement your own handler](#implement-your-own-handler) + +## Note + +> [!CAUTION] +> This library is currently still considered experimental and should therefore be used with caution. I would be happy for an issue to be posted if bugs are found. + +## License + +The code in this project is provided under the [MIT](https://opensource.org/licenses/MIT) license. + +## Overview + +With this library it is possible to monitor mail accounts (IMAP and POP3) and to check and process incoming electronic invoice documents (ZUGFeRD PDF and XML). + +## Dependencies + +This package makes use of + +* [horstoeko/zugferd](https://github.com/horstoeko/zugferd) +* [Webklex/php-imap](https://github.com/Webklex/php-imap) + +## Installation + +There is one recommended way to install `horstoeko/zugferdmail` via [Composer](https://getcomposer.org/): + +```bash +composer require horstoeko/zugferdmail +``` + +## Usage + +For detailed eplanation you may have a look in the [examples](https://github.com/horstoeko/zugferdmail/tree/master/examples) of this package and the documentation attached to every release. + +First, it is necessary to configure the library so that it is aware of the mail accounts to be checked. For IMAP accounts, the folders to be monitored must also be defined. In addition, any actions (handlers) can be defined for each mail account. + +### Configuration + +First you need to create a configuration instance: + +```php +$config = new ZugferdMailConfig(); +``` + +The mail accounts to be monitored must then be defined. You can also specify the folders and mimetypes to be checked: + +```php +$account1 = $config->addAccount('demo', '192.168.1.1', 993, 'imap', 'ssl', false, 'demouser', 'demopassword'); +$account1->addFolderToWatch('INBOX'); +$account1->addFolderToWatch('somefolder/somesubfolder'); +$account1->addMmimeTypeToWatch('application/pdf'); +$account1->addMmimeTypeToWatch('text/xml'); +``` + +Last but not least, the actions (handlers) to be performed are specified for each mail account, which are executed when a ZUGFeRD or XML document is found. Some of these handlers are already available, but you can also define your own actions: + +```php +$account1->addHandler(new ZugferdMailHandlerMoveMessage('Invoice/Incoming')); +$account1->addHandler(new ZugferdMailHandlerSaveToFile('/tmp', 'invoice.att')); +``` + +### Check mail accounts for matching mails: + +To perform the actual handling of all mail accounts, instantiate the class ```ZugferdMailReader```, to which the configuration is passed. The monitoring is started by calling the method ```checkAllAccounts``` + +```php +$reader = new ZugferdMailReader($config); +$reader->checkAllAccounts(); +``` + +### Predefined handlers + +* ZugferdMailHandlerNull + _Does not perform any operations_ +* ZugferdMailHandlerCli + _Displays brief information about the e-invoice document found on the console_ +* ZugferdMailHandlerCopyMessage + _Copies the message to another (different) directory_ +* ZugferdMailHandlerMoveMessage + _Moves the message to another (different) directory_ +* ZugferdMailHandlerDeleteMessage + _Deletes the message_ +* ZugferdMailHandlerSaveToFile + _Saves the E-Document to a specified directory, optionally with a different filename_ + +### Implement your own handler + +It is quite easy to implement your own action (handler). To do this, define your own class which implements the interface ```ZugferdMailHandlerInterface```. This interface defines a single method ```handleDocument```, which is passed information about the folder, message, attachment and the e-invoice document: + +```php +public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document); +``` + +An example: + +```php +class MyOwnHandler implements ZugferdMailHandlerInterface +{ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + // Do some stuff + } +} +``` \ No newline at end of file diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..bfe3037 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,10 @@ +# Security Policy + +## Supported Versions + +Only the latest released version of horstoeko/zugferdmail is supported. +To facilitate upgrades we almost never make backwards-incompatible changes. + +## Reporting a Vulnerability + +Please report vulnerabilities over email, by sending an email to security-horstoeko-zugferdmail@erling.com.de. diff --git a/build.ant.xml b/build.ant.xml new file mode 100644 index 0000000..14a8a4f --- /dev/null +++ b/build.ant.xml @@ -0,0 +1,263 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..d02cbcb --- /dev/null +++ b/build.xml @@ -0,0 +1,281 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/phpcsrules.xml b/build/phpcsrules.xml new file mode 100644 index 0000000..71bb6e8 --- /dev/null +++ b/build/phpcsrules.xml @@ -0,0 +1,351 @@ + + + The PSR-12 coding standard. + + */src/entities/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + error + Method name "%s" must not be prefixed with an underscore to indicate visibility + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/phpcsrules_psr1.xml b/build/phpcsrules_psr1.xml new file mode 100644 index 0000000..65cbe20 --- /dev/null +++ b/build/phpcsrules_psr1.xml @@ -0,0 +1,47 @@ + + + The PSR1 coding standard. + + + + + + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/phpcsrules_psr12.xml b/build/phpcsrules_psr12.xml new file mode 100644 index 0000000..4f4dad3 --- /dev/null +++ b/build/phpcsrules_psr12.xml @@ -0,0 +1,268 @@ + + + The PSR-12 coding standard. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + error + Method name "%s" must not be prefixed with an underscore to indicate visibility + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/phpcsrules_psr2.xml b/build/phpcsrules_psr2.xml new file mode 100644 index 0000000..ba5bd4e --- /dev/null +++ b/build/phpcsrules_psr2.xml @@ -0,0 +1,218 @@ + + + The PSR-2 coding standard. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + + + + + + + 0 + + + 0 + + + + + + + + + + + + + 0 + + + 0 + + + + + + + 0 + + + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + diff --git a/build/phpcsrules_squiz.xml b/build/phpcsrules_squiz.xml new file mode 100644 index 0000000..87ab4c3 --- /dev/null +++ b/build/phpcsrules_squiz.xml @@ -0,0 +1,132 @@ + + + The Squiz coding standard. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + %2$s + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + + + + + + + + 0 + + + error + + + + + 0 + + + error + + diff --git a/build/phpdoc.xml b/build/phpdoc.xml new file mode 100644 index 0000000..ea30163 --- /dev/null +++ b/build/phpdoc.xml @@ -0,0 +1,26 @@ + + + ../build/doc + ./phpdoc + + + + + + src + + + + php + + true + + TODO + FIXME + + + + diff --git a/build/phpmd.xml b/build/phpmd.xml new file mode 100644 index 0000000..cce41cc --- /dev/null +++ b/build/phpmd.xml @@ -0,0 +1,13 @@ + + + Custom rule sets that checks your PHP code. + + + + + diff --git a/build/phpstan.neon b/build/phpstan.neon new file mode 100644 index 0000000..7a514b1 --- /dev/null +++ b/build/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + paths: + - ../src + - ../tests + level: 5 + treatPhpDocTypesAsCertain: false + checkMissingIterableValueType: false + excludePaths: + - ../src/entities/* diff --git a/build/phpunit.xml b/build/phpunit.xml new file mode 100644 index 0000000..2cb0adc --- /dev/null +++ b/build/phpunit.xml @@ -0,0 +1,25 @@ + + + + + ../tests/testcases/BasicTest.php + + + + + ../src + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7f7b4b8 --- /dev/null +++ b/composer.json @@ -0,0 +1,59 @@ +{ + "name": "horstoeko/zugferdmail", + "keywords": [], + "description": "A library", + "homepage": "https://github.com/horstoeko/zugferdmail", + "type": "package", + "license": "MIT", + "prefer-stable": true, + "authors": [ + { + "name": "Daniel Erling", + "email": "daniel@erling.com.de", + "role": "lead" + } + ], + "config": { + "optimize-autoloader": true, + "sort-packages": true + }, + "autoload": { + "psr-4": { + "horstoeko\\zugferdmail\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "horstoeko\\zugferdmail\\tests\\": "tests" + } + }, + "require": { + "php": "^7.3|^7.4|^8", + "horstoeko/zugferd": "^1.0", + "symfony/console": "^5.4", + "webklex/php-imap": "^4.1" + }, + "require-dev": { + "pdepend/pdepend": "^2", + "phploc/phploc": "^7", + "phpmd/phpmd": "^2", + "phpstan/phpstan": "^1.8", + "phpunit/phpunit": "^9", + "sebastian/phpcpd": "^6", + "squizlabs/php_codesniffer": "^3" + }, + "scripts": { + "tests": "./vendor/bin/phpunit ./tests/", + "testsreal": "./vendor/bin/phpunit --configuration ./build/phpunit.xml", + "phpcs": "./vendor/bin/phpcs --standard=./build/phpcsrules.xml --extensions=php --ignore=autoload.php ./src ./tests", + "phpcs12": "./vendor/bin/phpcs --standard=./build/phpcsrules_psr12.xml --extensions=php --ignore=autoload.php ./src ./tests", + "phpcbf": "./vendor/bin/phpcbf -q ./src ./tests", + "phpcbf1": "./vendor/bin/phpcbf --standard=./build/phpcsrules_psr1.xml -q ./src ./tests", + "phpcbf2": "./vendor/bin/phpcbf --standard=./build/phpcsrules_psr2.xml -q ./src ./tests", + "phpcbf12": "./vendor/bin/phpcbf --standard=./build/phpcsrules_psr12.xml -q ./src ./tests", + "phpcbfsq": "./vendor/bin/phpcbf --standard=./build/phpcsrules_squiz.xml -q ./src ./tests", + "phpstan": "./vendor/bin/phpstan analyze -c ./build/phpstan.neon --autoload-file=vendor/autoload.php --no-interaction --no-progress --xdebug", + "phpstan_cs": "./vendor/bin/phpstan analyze -c ./build/phpstan.neon --autoload-file=vendor/autoload.php --no-interaction --no-progress --error-format=checkstyle --xdebug", + "makedoc": "phing -f ./build.xml projectdoc" + } +} diff --git a/src/ZugferdMailReader.php b/src/ZugferdMailReader.php new file mode 100644 index 0000000..73111c7 --- /dev/null +++ b/src/ZugferdMailReader.php @@ -0,0 +1,176 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailReader +{ + /** + * The config + * + * @var ZugferdMailConfig + */ + protected $config; + + /** + * The client manager + * + * @var ClientManager + */ + protected $clientManager; + + /** + * Constructor + * + * @param ZugferdMailConfig $config + */ + public function __construct(ZugferdMailConfig $config) + { + $this->config = $config; + $this->clientManager = $this->config->getClientManager(); + } + + /** + * Check all defined accounts + * + * @return ZugferdMailReader + */ + public function checkAllAccounts(): ZugferdMailReader + { + foreach ($this->config->getAccounts() as $account) { + $this->checkSingleAccount($account); + } + + return $this; + } + + /** + * Checks a single account + * + * @param ZugferdMailAccount $account + * @return void + */ + protected function checkSingleAccount(ZugferdMailAccount $account): void + { + $this->clientManager->account($account->getIdentifier())->connect()->getFolders()->each( + function (Folder $folder) use ($account) { + $this->checkSingleAccountFolder($account, $folder); + } + ); + } + + /** + * Checks a single mail account folder + * + * @param ZugferdMailAccount $account + * @param Folder $folder + * @return void + */ + protected function checkSingleAccountFolder(ZugferdMailAccount $account, Folder $folder): void + { + if (in_array($folder->full_name, $account->getFoldersTowatch())) { + $folder->messages()->all()->get()->filter( + function (Message $message) { + return $message->hasAttachments(); + } + )->each( + function (Message $message) use ($account, $folder) { + $this->checkSingleMessage($account, $folder, $message); + } + ); + } + + collect($folder->children)->each( + function (Folder $subFolder) use ($account) { + $this->checkSingleAccountFolder($account, $subFolder); + } + ); + } + + /** + * Checks a single mail + * + * @param ZugferdMailAccount $account + * @param Folder $folder + * @param Message $message + * @return void + */ + protected function checkSingleMessage(ZugferdMailAccount $account, Folder $folder, Message $message): void + { + $message->attachments()->each( + function (Attachment $attachment) use ($account, $folder, $message) { + $this->checkSingleMessageAttachment($account, $folder, $message, $attachment); + } + ); + } + + /** + * Checks a single mail attachment + * + * @param ZugferdMailAccount $account + * @param Folder $folder + * @param Message $message + * @param Attachment $attachment + * @return void + */ + protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment): void + { + if (!in_array($attachment->getMimeType(), $account->getMmimeTypesToWatch())) { + return; + } + + try { + $document = ZugferdDocumentPdfReader::readAndGuessFromContent($attachment->getContent()); + $this->triggerHandlers($account, $folder, $message, $attachment, $document); + } catch (Throwable $e) { + try { + $document = ZugferdDocumentReader::readAndGuessFromContent($attachment->getContent()); + $this->triggerHandlers($account, $folder, $message, $attachment, $document); + } catch (Throwable $e) { + // Do nothing + } + } + } + + /** + * Internal trigger when attachment was found + * + * @param ZugferdMailAccount $account + * @param Folder $folder + * @param Message $message + * @param ZugferdDocument $document + * @return void + */ + protected function triggerHandlers(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocument $document): void + { + foreach ($account->getHandlers() as $handler) { + $handler->handleDocument($account, $folder, $message, $attachment, $document); + } + } +} diff --git a/src/config/ZugferdMailAccount.php b/src/config/ZugferdMailAccount.php new file mode 100644 index 0000000..d1eb054 --- /dev/null +++ b/src/config/ZugferdMailAccount.php @@ -0,0 +1,511 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailAccount +{ + /** + * The account identifier + * + * @var string + */ + protected $identifier = ""; + + /** + * The host to call + * + * @var string + */ + protected $host = ""; + + /** + * The port on the host to use + * + * @var integer + */ + protected $port = 0; + + /** + * The Protocol you want to utilize + * + * @var string + */ + protected $protocol = "imap"; + + /** + * Used encryption method. Use false to disable encryption. + * + * @var string|boolean + */ + protected $encryption = "ssl"; + + /** + * The certificate gets validated by default. You can turn this off, by setting it to false. + * + * @var boolean + */ + protected $validateCert = true; + + /** + * Account username + * + * @var string + */ + protected $username = ""; + + /** + * Account password + * + * @var string + */ + protected $password = ""; + + /** + * Set it to oauth if you want to authenticate using oAuth. You may provide your access token as password + * + * @var string|null + */ + protected $authentication = null; + + /** + * The timeout + * + * @var integer + */ + protected $timeout = 30; + + /** + * Folders to watch + * + * @var string[] + */ + protected $foldersToWatch = []; + + /** + * Mime types to watch + * + * @var array + */ + protected $mimeTypesToWatch = []; + + /** + * Handler for found documents + * + * @var array + */ + protected $handlers = []; + + /** + * Returns the account identifier + * + * @return string + */ + public function getIdentifier(): string + { + return $this->identifier; + } + + /** + * Sets the account identifier + * + * @param string $identifier + * @return ZugferdMailAccount + * @throws InvalidArgumentException + */ + public function setIdentifier(string $identifier): ZugferdMailAccount + { + if (empty($identifier)) { + throw new InvalidArgumentException("The identifier must not be ampty"); + } + + $this->identifier = $identifier; + + return $this; + } + + /** + * Returns the account's host + * + * @return string + */ + public function getHost(): string + { + return $this->host; + } + + /** + * Sets the account's host + * + * @param string $host + * @return ZugferdMailAccount + */ + public function setHost(string $host): ZugferdMailAccount + { + if (empty($host)) { + throw new InvalidArgumentException("The host must not be empty"); + } + + $this->host = $host; + + return $this; + } + + /** + * Returns the account's port + * + * @return integer + */ + public function getPort(): int + { + return $this->port; + } + + /** + * Sets the account's port + * + * @param integer $port + * @return ZugferdMailAccount + */ + public function setPort(string $port): ZugferdMailAccount + { + if ($port < 0 || $port > 65535) { + throw new InvalidArgumentException("The port must be between 0 and 65535"); + } + + $this->port = $port; + + return $this; + } + + /** + * Returns the protocol to use + * + * @return string + */ + public function getProtocol(): string + { + return $this->protocol; + } + + /** + * Sets the protocol to use + * + * @param string $protocol + * @return ZugferdMailAccount + */ + public function setProtocol(string $protocol): ZugferdMailAccount + { + if (!in_array($protocol, ['imap', 'legacy-imap', 'pop3', 'nntp'])) { + throw new InvalidArgumentException("The protocol must be one of imap, legacy-imap, pop3 or nntp"); + } + + $this->protocol = $protocol; + + return $this; + } + + /** + * Returns the encryption to use + * + * @return string|boolean + */ + public function getEncryption() + { + return $this->encryption; + } + + /** + * Set the encryption to use + * + * @param string|boolean $encryption + * @return ZugferdMailAccount + */ + public function setEncryption($encryption): ZugferdMailAccount + { + if (!in_array($encryption, [false, "ssl", "tls", "starttls", "notls"])) { + throw new InvalidArgumentException("The encryption must be false or one of ssl, tls, starttls, notls"); + } + + $this->encryption = $encryption; + + return $this; + } + + /** + * Returns true if the SSL certificates should be validated, otherwise fals + * + * @return boolean + */ + public function getValidateCert(): bool + { + return $this->validateCert; + } + + /** + * Set to true if SSL ceritificates should be validates, otherwise to fals + * + * @param boolean $validateCert + * @return ZugferdMailAccount + */ + public function setValidateCert(bool $validateCert): ZugferdMailAccount + { + $this->validateCert = $validateCert; + + return $this; + } + + /** + * Returns the account's username + * + * @return string + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * Sets the accounts user name + * + * @param string $username + * @return ZugferdMailAccount + */ + public function setUsername(string $username): ZugferdMailAccount + { + if (empty($username)) { + throw new InvalidArgumentException("The username must be not empty"); + } + + $this->username = $username; + + return $this; + } + + /** + * Returns the account's password + * + * @return string + */ + public function getPassword(): string + { + return $this->password; + } + + /** + * Sets the account's password + * + * @param string $password + * @return ZugferdMailAccount + */ + public function setPassword(string $password): ZugferdMailAccount + { + if (empty($password)) { + throw new InvalidArgumentException("The password must be not empty"); + } + + $this->password = $password; + + return $this; + } + + /** + * Returns the authentication method + * + * @return string|null + */ + public function getAuthentication(): ?string + { + return $this->authentication; + } + + /** + * Sets the account's authentication methid + * + * @param string|null $authentication + * @return ZugferdMailAccount + */ + public function setAuthentication(?string $authentication): ZugferdMailAccount + { + if (!in_array($authentication, [null, "oauth"])) { + throw new InvalidArgumentException("The authentication method must be one of null (NUL), oauth"); + } + + $this->authentication = $authentication; + + return $this; + } + + /** + * Returns the account's timeout + * + * @return integer + */ + public function getTimeout(): int + { + return $this->timeout; + } + + /** + * Sets the account's timeout + * + * @param integer $timeout + * @return ZugferdMailAccount + */ + public function setTimeout(int $timeout): ZugferdMailAccount + { + if ($timeout < 0) { + throw new InvalidArgumentException("The authentication method must be zero or greater"); + } + + $this->timeout = $timeout; + + return $this; + } + + /** + * Returns folders to watch for + * + * @return string[] + */ + public function getFoldersTowatch(): array + { + return $this->foldersToWatch; + } + + /** + * Sets folders to watch for + * + * @param array $folders + * @return ZugferdMailAccount + */ + public function setFoldersToWatch(array $folders): ZugferdMailAccount + { + $this->foldersToWatch = $folders; + + return $this; + } + + /** + * Add a folder to watch + * + * @param string $folderPath + * @return ZugferdMailAccount + */ + public function addFolderToWatch(string $folderPath): ZugferdMailAccount + { + $this->foldersToWatch[] = $folderPath; + + return $this; + } + + /** + * Returns a list of attachment mimetypes to watch + * + * @return array + */ + public function getMmimeTypesToWatch(): array + { + return $this->mimeTypesToWatch; + } + + /** + * Sets a list of attachment mimetypes to watch + * + * @param array $mimeTypesToWatch + * @return ZugferdMailAccount + */ + public function setMmimeTypesToWatch(array $mimeTypesToWatch): ZugferdMailAccount + { + $this->mimeTypesToWatch = $mimeTypesToWatch; + + return $this; + } + + /** + * Add a mimetype to watch + * + * @param string $mimeTypesToWatch + * @return ZugferdMailAccount + */ + public function addMmimeTypeToWatch(string $mimeTypesToWatch): ZugferdMailAccount + { + $this->mimeTypesToWatch[] = $mimeTypesToWatch; + + return $this; + } + + /** + * Returns the handler when a document was found + * + * @return array + */ + public function getHandlers(): array + { + return $this->handlers; + } + + /** + * Sets multiuple handlers to run when a document was found + * + * @param array $handlers + * @return ZugferdMailAccount + */ + public function setHandlers(array $handlers): ZugferdMailAccount + { + $this->handlers = $handlers; + + return $this; + } + + /** + * Sets the handler to execute when a document was found + * + * @param ZugferdMailHandlerInterface $handler + * @return ZugferdMailAccount + */ + public function addHandler(?ZugferdMailHandlerInterface $handler): ZugferdMailAccount + { + $this->handlers[] = $handler; + + return $this; + } + + /** + * Returns the account definitoon + * + * @return array + */ + public function getAccountDefinition(): array + { + return [ + 'host' => $this->getHost(), + 'port' => $this->getPort(), + 'protocol' => $this->getProtocol(), + 'encryption' => $this->getEncryption(), + 'validate_cert' => $this->getValidateCert(), + 'username' => $this->getUsername(), + 'password' => $this->getPassword(), + 'authentication' => $this->getAuthentication(), + "timeout" => $this->getTimeout(), + ]; + } +} diff --git a/src/config/ZugferdMailConfig.php b/src/config/ZugferdMailConfig.php new file mode 100644 index 0000000..d67aff4 --- /dev/null +++ b/src/config/ZugferdMailConfig.php @@ -0,0 +1,154 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailConfig +{ + /** + * The date format to use + * + * @var string + */ + protected $dateFormat = "d-M-Y"; + + /** + * List of defined accounts + * + * @var array + */ + protected $accounts = []; + + /** + * Get the date format to use + * + * @return string + */ + public function getDateFormat(): string + { + return $this->dateFormat; + } + + /** + * Set the date format to use + * + * @param string $dateFormat + * @return ZugferdMailConfig + * @throws InvalidArgumentException + */ + public function setDateFormat(string $dateFormat): ZugferdMailConfig + { + if (!in_array($dateFormat, ["d-M-Y", "d-M-y", "d M y"])) { + throw new InvalidArgumentException(sprintf("%s is not a valid date format", $dateFormat)); + } + + $this->dateFormat = $dateFormat; + + return $this; + } + + /** + * Add an account definition + * + * @param string $identifier + * @param string $host + * @param integer $port + * @param string $protocol + * @param string|false $encryption + * @param boolean $validateCert + * @param string $username + * @param string $password + * @param string|null $authentication + * @param integer $timeout + * @param array $foldersToWatch + * @param array $mimeTypesToWatch + * @return ZugferdMailAccount + */ + public function addAccount(string $identifier, string $host, int $port, string $protocol, $encryption, bool $validateCert, string $username, string $password, ?string $authentication = null, int $timeout = 30, array $foldersToWatch = [], array $mimeTypesToWatch = []): ZugferdMailAccount + { + $account = new ZugferdMailAccount(); + + $account->setIdentifier($identifier); + $account->setHost($host); + $account->setPort($port); + $account->setProtocol($protocol); + $account->setEncryption($encryption); + $account->setValidateCert($validateCert); + $account->setUsername($username); + $account->setPassword($password); + $account->setAuthentication($authentication); + $account->setTimeout($timeout); + $account->setFoldersToWatch($foldersToWatch); + $account->setMmimeTypesToWatch($mimeTypesToWatch); + + $this->accounts[] = $account; + + return $account; + } + + /** + * Remove an account definition by it's identifier + * + * @param string $identifier + * @return ZugferdMailConfig + */ + public function removeAccount(string $identifier): ZugferdMailConfig + { + $this->accounts = array_filter( + $this->accounts, + function ($account) use ($identifier) { + return strcasecmp($account->getIdentifier(), $identifier) != 0; + } + ); + + return $this; + } + + /** + * Get the list of defined accounts + * + * @return array + */ + public function getAccounts() + { + return $this->accounts; + } + + /** + * Build the client manager + * + * @return ClientManager + */ + public function getClientManager(): ClientManager + { + $config = []; + + $config['date_format'] = $this->getDateFormat(); + $config['default'] = false; + + foreach ($this->accounts as $account) { + $config['accounts'][$account->getIdentifier()] = $account->getAccountDefinition(); + } + + return new ClientManager($config); + } +} diff --git a/src/handlers/ZugferdMailHandlerCli.php b/src/handlers/ZugferdMailHandlerCli.php new file mode 100644 index 0000000..dd49b8e --- /dev/null +++ b/src/handlers/ZugferdMailHandlerCli.php @@ -0,0 +1,64 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerCli implements ZugferdMailHandlerInterface +{ + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + if (php_sapi_name() !== 'cli') { + return; + } + + $document->getDocumentInformation( + $documentno, + $documenttypecode, + $documentdate, + $invoiceCurrency, + $taxCurrency, + $documentname, + $documentlanguage, + $effectiveSpecifiedPeriod + ); + + $this->writeln("Found an invoice..."); + $this->writeln(sprintf(" - Invoice No. ............. %s", $documentno)); + $this->writeln(sprintf(" - Invoice Date ............ %s", $documentdate->format("d.m.Y"))); + } + + /** + * Write to screen with CR at the end + * + * @param string $str + * @return void + */ + protected function writeln(string $str): void + { + echo sprintf("%s\n", $str); + } +} diff --git a/src/handlers/ZugferdMailHandlerCopyMessage.php b/src/handlers/ZugferdMailHandlerCopyMessage.php new file mode 100644 index 0000000..9c9cc88 --- /dev/null +++ b/src/handlers/ZugferdMailHandlerCopyMessage.php @@ -0,0 +1,81 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerCopyMessage implements ZugferdMailHandlerInterface +{ + /** + * The folder to copy the message to + * + * @var string + */ + protected $copyToFolder = ""; + + /** + * Constructor + * + * @param string $copyToFolder + */ + public function __construct(string $copyToFolder) + { + $this->setCopyToFolder($copyToFolder); + } + + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + $message->copy($this->getCopyToFolder()); + } + + /** + * Returns the folder name to which the message should be copied + * + * @return string + */ + public function getCopyToFolder(): string + { + return $this->copyToFolder; + } + + /** + * Sets the folder name to which the message should be copied + * + * @param string $copyToFolder + * @return ZugferdMailHandlerCopyMessage + */ + public function setCopyToFolder(string $copyToFolder): ZugferdMailHandlerCopyMessage + { + if (empty($copyToFolder)) { + throw new InvalidArgumentException("The destination folder must not be empty"); + } + + $this->copyToFolder = $copyToFolder; + + return $this; + } +} diff --git a/src/handlers/ZugferdMailHandlerDeleteMessage.php b/src/handlers/ZugferdMailHandlerDeleteMessage.php new file mode 100644 index 0000000..cdcbe91 --- /dev/null +++ b/src/handlers/ZugferdMailHandlerDeleteMessage.php @@ -0,0 +1,36 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerDeleteMessage implements ZugferdMailHandlerInterface +{ + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + $message->delete(); + } +} diff --git a/src/handlers/ZugferdMailHandlerInterface.php b/src/handlers/ZugferdMailHandlerInterface.php new file mode 100644 index 0000000..bf3e2f3 --- /dev/null +++ b/src/handlers/ZugferdMailHandlerInterface.php @@ -0,0 +1,32 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerMoveMessage implements ZugferdMailHandlerInterface +{ + /** + * The folder to move the message to + * + * @var string + */ + protected $moveToFolder = ""; + + /** + * Constructor + * + * @param string $moveToFolder + */ + public function __construct(string $moveToFolder) + { + $this->setMoveToFolder($moveToFolder); + } + + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + $message->move($this->getMoveToFolder()); + } + + /** + * Returns the folder name to which the message should be moved + * + * @return string + */ + public function getMoveToFolder(): string + { + return $this->moveToFolder; + } + + /** + * Sets the folder name to which the message should be moved + * + * @param string $moveToFolder + * @return ZugferdMailHandlerMoveMessage + */ + public function setMoveToFolder(string $moveToFolder): ZugferdMailHandlerMoveMessage + { + if (empty($moveToFolder)) { + throw new InvalidArgumentException("The destination folder must not be empty"); + } + + $this->moveToFolder = $moveToFolder; + + return $this; + } +} diff --git a/src/handlers/ZugferdMailHandlerNull.php b/src/handlers/ZugferdMailHandlerNull.php new file mode 100644 index 0000000..f89896b --- /dev/null +++ b/src/handlers/ZugferdMailHandlerNull.php @@ -0,0 +1,35 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerNull implements ZugferdMailHandlerInterface +{ + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + } +} diff --git a/src/handlers/ZugferdMailHandlerSaveToFile.php b/src/handlers/ZugferdMailHandlerSaveToFile.php new file mode 100644 index 0000000..970c77d --- /dev/null +++ b/src/handlers/ZugferdMailHandlerSaveToFile.php @@ -0,0 +1,126 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferdmail + */ +class ZugferdMailHandlerSaveToFile implements ZugferdMailHandlerInterface +{ + /** + * The path to store the attachment (the invoice document) + * + * @var string + */ + protected $filePath = ""; + + /** + * The different filename to which the file is stored + * + * @var string|null + */ + protected $fileName = null; + + /** + * Constructor + * + * @param string $filePath + * @param string|null $fileName + */ + public function __construct(string $filePath, ?string $fileName) + { + $this->setFilePath($filePath); + $this->setFileName($fileName); + } + + /** + * @inheritDoc + */ + public function handleDocument(ZugferdMailAccount $account, Folder $folder, Message $message, Attachment $attachment, ZugferdDocumentReader $document) + { + $attachment->save($this->getFilePath(), $this->getFileName()); + } + + /** + * Returns the file path to which the attachment (the invoice document) is stored + * + * @return string + */ + public function getFilePath(): string + { + return $this->filePath; + } + + /** + * Sets the file path to which the attachment (the invoice document) is stored + * + * @param string $filePath + * @return ZugferdMailHandlerSaveToFile + */ + public function setFilePath($filePath): ZugferdMailHandlerSaveToFile + { + if (empty($filePath)) { + throw new InvalidArgumentException("The file path must not be empty"); + } + + $filePath = rtrim($filePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + + if (!is_dir($filePath)) { + throw new InvalidArgumentException(sprintf("The file path %s does not exist", $filePath)); + } + + $this->filePath = $filePath; + + return $this; + } + + /** + * Returns the different file name to which the attachment (the invoice document) is stored + * + * @return string|null + */ + public function getFileName() + { + return $this->fileName; + } + + /** + * Sets the different file name to which the attachment (the invoice document) is stored + * + * @param string|null $filename + * @return ZugferdMailHandlerSaveToFile + */ + public function setFileName(?string $filename): ZugferdMailHandlerSaveToFile + { + if (!is_null($filename)) { + if (empty($filename)) { + throw new InvalidArgumentException("The file name must not be empty"); + } + } + + $this->fileName = $filename; + + return $this; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..b831e08 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,170 @@ +registeredTestFiles = []; + } + + /** + * @inheritDoc + */ + protected function tearDown(): void + { + parent::tearDown(); + + foreach ($this->registeredTestFiles as $registeredTestFile) { + if (file_exists($registeredTestFile) && is_writeable($registeredTestFile)) { + @unlink($registeredTestFile); + } + } + + $this->registeredTestFiles = []; + } + + /** + * Register a file to delete in testcase teardown + * + * @param string $filename + * @return void + */ + public function registerFileForTestCaseTeardown(string $filename): void + { + self::$registeredTestCaseFiles[] = $filename; + } + + /** + * Register a file to delete in testmethod teardown + * + * @param string $filename + * @return void + */ + public function registerFileForTestMethodTeardown(string $filename): void + { + $this->registeredTestFiles[] = $filename; + } + + /** + * Expect notice on php version smaller than 8 + * Expect warning on php version greater or equal than 8 + * + * @return void + */ + public function expectNoticeOrWarning(): void + { + if (version_compare(phpversion(), '8', '>=')) { + $this->expectWarning(); + } else { + $this->expectNotice(); + } + } + + /** + * Access to private properties + * + * @param string $className + * @param string $propertyName + * @return ReflectionProperty + */ + public function getPrivatePropertyFromClassname(string $className, string $propertyName): ReflectionProperty + { + $reflector = new ReflectionClass($className); + $property = $reflector->getProperty($propertyName); + $property->setAccessible(true); + return $property; + } + + /** + * Access to private properties + * + * @param object $object + * @param string $propertyName + * @return ReflectionProperty + */ + public function getPrivatePropertyFromObject(object $object, string $propertyName): ReflectionProperty + { + $reflector = new ReflectionClass($object); + $property = $reflector->getProperty($propertyName); + $property->setAccessible(true); + return $property; + } + + /** + * Access to private method + * + * @param string $className + * @param string $methodName + * @return ReflectionMethod + */ + public function getPrivateMethodFromClassname(string $className, string $methodName): ReflectionMethod + { + $reflector = new ReflectionClass($className); + $method = $reflector->getMethod($methodName); + $method->setAccessible(true); + return $method; + } + + /** + * Access to private method + * + * @param object $object + * @param string $methodName + * @return ReflectionMethod + */ + public function getPrivateMethodFromObject(object $object, string $methodName): ReflectionMethod + { + $reflector = new ReflectionClass($object); + $method = $reflector->getMethod($methodName); + $method->setAccessible(true); + return $method; + } +} diff --git a/tests/testcases/BasicTest.php b/tests/testcases/BasicTest.php new file mode 100644 index 0000000..2e99fd0 --- /dev/null +++ b/tests/testcases/BasicTest.php @@ -0,0 +1,13 @@ +assertTrue(true); + } +}