Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shimomo committed Jan 2, 2025
0 parents commit 32309b3
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
.phpunit.result.cache
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php

require __DIR__ . '/vendor/autoload.php';

use Boatrace\Sakura\Trimmer;

var_dump(Trimmer::trim(null)); // NULL
var_dump(Trimmer::trim(' 競艇 ')); // string(6) "競艇"

var_dump(Trimmer::ltrim(null)); // NULL
var_dump(Trimmer::ltrim(' 競艇 ')); // string(7) "競艇 "

var_dump(Trimmer::rtrim(null)); // NULL
var_dump(Trimmer::rtrim(' 競艇 ')); // string(7) " 競艇"
```

## License
The Boatrace Sakura Trimmer is open source software licensed under the [MIT license](LICENSE).
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "boatrace-sakura/trimmer",
"description": "The Boatrace Sakura Trimmer.",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "shimomo",
"email": "developer@shimomo.net"
}
],
"require": {
"php": "^8.1",
"php-di/php-di": "^6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"Boatrace\\Sakura\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Boatrace\\Sakura\\Tests\\": "tests/"
}
}
}
12 changes: 12 additions & 0 deletions config/definitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

return [
'Trimmer' => \DI\create('\Boatrace\Sakura\Trimmer')->constructor(
\DI\get('MainTrimmer')
),
'MainTrimmer' => function ($container) {
return $container->get('\Boatrace\Sakura\MainTrimmer');
},
];
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuites>
<testsuite name="boatrace-sakura">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
41 changes: 41 additions & 0 deletions src/MainTrimmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Boatrace\Sakura;

/**
* @author shimomo
*/
class MainTrimmer
{
/**
* @param string|null $value
* @param string $characters
* @return string|null
*/
public function trim(?string $value, string $characters = "\x00\x09\x0A\x0B\x0D\x20"): ?string
{
return is_null($value) ? null : trim($value, $characters);
}

/**
* @param string|null $value
* @param string $characters
* @return string|null
*/
public function ltrim(?string $value, string $characters = "\x00\x09\x0A\x0B\x0D\x20"): ?string
{
return is_null($value) ? null : ltrim($value, $characters);
}

/**
* @param string|null $value
* @param string $characters
* @return string|null
*/
public function rtrim(?string $value, string $characters = "\x00\x09\x0A\x0B\x0D\x20"): ?string
{
return is_null($value) ? null : rtrim($value, $characters);
}
}
70 changes: 70 additions & 0 deletions src/Trimmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Boatrace\Sakura;

use DI\Container;
use DI\ContainerBuilder;

/**
* @author shimomo
*/
class Trimmer
{
/**
* @var \Boatrace\Sakura\Trimmer
*/
protected static Trimmer $instance;

/**
* @var \DI\Container
*/
protected static Container $container;

/**
* @param \Boatrace\Sakura\MainTrimmer $trimmer
* @return void
*/
public function __construct(protected MainTrimmer $trimmer){}

/**
* @param string $name
* @param array $arguments
* @return string|null
*/
public function __call(string $name, array $arguments): ?string
{
return $this->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();
}
}
54 changes: 54 additions & 0 deletions tests/MainTrimmerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Boatrace\Sakura\Tests;

use Boatrace\Sakura\MainTrimmer;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

/**
* @author shimomo
*/
class MainTrimmerTest extends PHPUnitTestCase
{
/**
* @var \Boatrace\Sakura\MainTrimmer
*/
protected MainTrimmer $trimmer;

/**
* @return void
*/
public function setUp(): void
{
$this->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(' 競艇 '));
}
}
41 changes: 41 additions & 0 deletions tests/TrimmerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Boatrace\Sakura\Tests;

use Boatrace\Sakura\Trimmer;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

/**
* @author shimomo
*/
class TrimmerTest extends PHPUnitTestCase
{
/**
* @return void
*/
public function testTrim(): void
{
$this->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(' 競艇 '));
}
}

0 comments on commit 32309b3

Please sign in to comment.