From 32309b381fae4bdb9ed27648c5c15441fbd1ec0c Mon Sep 17 00:00:00 2001 From: shimomo Date: Thu, 2 Jan 2025 19:31:08 +0900 Subject: [PATCH] Initial commit --- .editorconfig | 12 +++++++ .github/workflows/tests.yml | 35 +++++++++++++++++++ .gitignore | 3 ++ LICENSE | 21 +++++++++++ README.md | 33 +++++++++++++++++ composer.json | 29 +++++++++++++++ config/definitions.php | 12 +++++++ phpunit.xml | 26 ++++++++++++++ src/MainTrimmer.php | 41 ++++++++++++++++++++++ src/Trimmer.php | 70 +++++++++++++++++++++++++++++++++++++ tests/MainTrimmerTest.php | 54 ++++++++++++++++++++++++++++ tests/TrimmerTest.php | 41 ++++++++++++++++++++++ 12 files changed, 377 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 config/definitions.php create mode 100644 phpunit.xml create mode 100644 src/MainTrimmer.php create mode 100644 src/Trimmer.php create mode 100644 tests/MainTrimmerTest.php create mode 100644 tests/TrimmerTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..37cfad7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..9e38619 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,35 @@ +name: tests +on: + push: + branches: + - main + schedule: + - cron: '0 0 * * *' +jobs: + tests: + name: tests + runs-on: ubuntu-latest + strategy: + matrix: + php-version: ['8.1', '8.2', '8.3', '8.4'] + steps: + - name: Setup php ${{ matrix.php-version }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + - name: Checkout + uses: actions/checkout@v4 + - name: Validate composer.json + run: composer validate + - name: Install dependencies + run: composer install --prefer-dist --no-progress + - name: Run test suites + run: | + mkdir --parents build/logs + vendor/bin/phpunit --coverage-clover=build/logs/clover.xml + - name: Upload coverage results + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + composer global require php-coveralls/php-coveralls + php-coveralls --coverage_clover=build/logs/clover.xml -v diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..83713b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor +.phpunit.result.cache +composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6a4bdf8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020 shimomo + +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..170574b --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Boatrace Sakura Trimmer + +[![Build Status](https://github.com/boatrace-sakura/trimmer/workflows/tests/badge.svg)](https://github.com/boatrace-sakura/trimmer/actions?query=workflow%3Atests) +[![Coverage Status](https://coveralls.io/repos/github/boatrace-sakura/trimmer/badge.svg?branch=main)](https://coveralls.io/github/boatrace-sakura/trimmer?branch=main) +[![Latest Stable Version](https://poser.pugx.org/boatrace-sakura/trimmer/v/stable)](https://packagist.org/packages/boatrace-sakura/trimmer) +[![Latest Unstable Version](https://poser.pugx.org/boatrace-sakura/trimmer/v/unstable)](https://packagist.org/packages/boatrace-sakura/trimmer) +[![License](https://poser.pugx.org/boatrace-sakura/trimmer/license)](https://packagist.org/packages/boatrace-sakura/trimmer) + +## Installation +```bash +composer require boatrace-sakura/trimmer +``` + +## Usage +```php + \DI\create('\Boatrace\Sakura\Trimmer')->constructor( + \DI\get('MainTrimmer') + ), + 'MainTrimmer' => function ($container) { + return $container->get('\Boatrace\Sakura\MainTrimmer'); + }, +]; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..53f1cdb --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,26 @@ + + + + + + ./tests/ + + + + + + ./src + + + + + + + diff --git a/src/MainTrimmer.php b/src/MainTrimmer.php new file mode 100644 index 0000000..c65a531 --- /dev/null +++ b/src/MainTrimmer.php @@ -0,0 +1,41 @@ +trimmer->$name(...$arguments); + } + + /** + * @param string $name + * @param array $arguments + * @return string|null + */ + public static function __callStatic(string $name, array $arguments): ?string + { + return self::getInstance()->$name(...$arguments); + } + + /** + * @return \Boatrace\Sakura\Trimmer + */ + public static function getInstance(): Trimmer + { + return self::$instance ?? self::$instance = ( + self::$container ?? self::$container = self::getContainer() + )->get('Trimmer'); + } + + /** + * @return \DI\Container + */ + public static function getContainer(): Container + { + $builder = new ContainerBuilder; + $builder->addDefinitions(__DIR__ . '/../config/definitions.php'); + return $builder->build(); + } +} diff --git a/tests/MainTrimmerTest.php b/tests/MainTrimmerTest.php new file mode 100644 index 0000000..85c98f2 --- /dev/null +++ b/tests/MainTrimmerTest.php @@ -0,0 +1,54 @@ +trimmer = new MainTrimmer; + } + + /** + * @return void + */ + public function testTrim(): void + { + $this->assertNull($this->trimmer->trim(null)); + $this->assertSame('競艇', $this->trimmer->trim(' 競艇 ')); + } + + /** + * @return void + */ + public function testLtrim(): void + { + $this->assertNull($this->trimmer->ltrim(null)); + $this->assertSame('競艇 ', $this->trimmer->ltrim(' 競艇 ')); + } + + /** + * @return void + */ + public function testRtrim(): void + { + $this->assertNull($this->trimmer->rtrim(null)); + $this->assertSame(' 競艇', $this->trimmer->rtrim(' 競艇 ')); + } +} diff --git a/tests/TrimmerTest.php b/tests/TrimmerTest.php new file mode 100644 index 0000000..6aee139 --- /dev/null +++ b/tests/TrimmerTest.php @@ -0,0 +1,41 @@ +assertNull(Trimmer::trim(null)); + $this->assertSame('競艇', Trimmer::trim(' 競艇 ')); + } + + /** + * @return void + */ + public function testLtrim(): void + { + $this->assertNull(Trimmer::ltrim(null)); + $this->assertSame('競艇 ', Trimmer::ltrim(' 競艇 ')); + } + + /** + * @return void + */ + public function testRtrim(): void + { + $this->assertNull(Trimmer::rtrim(null)); + $this->assertSame(' 競艇', Trimmer::rtrim(' 競艇 ')); + } +}