diff --git a/.vscode/templates.code-snippets b/.vscode/templates.code-snippets index b9607d8..4b5328e 100644 --- a/.vscode/templates.code-snippets +++ b/.vscode/templates.code-snippets @@ -5,7 +5,7 @@ "body": [ " 🔔 Subscribe to the [newsletter](https://chv.to/chevere-newsletter) to don't miss any update regarding Chevere. ![Chevere](chevere.svg) -[![Build](https://img.shields.io/github/actions/workflow/status/chevere/reponame/test.yml?branch=%branch%&style=flat-square)](https://github.com/chevere/reponame/actions) -![Code size](https://img.shields.io/github/languages/code-size/chevere/reponame?style=flat-square) -[![Apache-2.0](https://img.shields.io/github/license/chevere/reponame?style=flat-square)](LICENSE) +[![Build](https://img.shields.io/github/actions/workflow/status/chevere/message/test.yml?branch=0.1&style=flat-square)](https://github.com/chevere/message/actions) +![Code size](https://img.shields.io/github/languages/code-size/chevere/message?style=flat-square) +[![Apache-2.0](https://img.shields.io/github/license/chevere/message?style=flat-square)](LICENSE) [![PHPStan](https://img.shields.io/badge/PHPStan-level%209-blueviolet?style=flat-square)](https://phpstan.org/) -[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Freponame%2F%branch%)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/reponame/%branch%) - -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_reponame&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_reponame) -[![CodeFactor](https://www.codefactor.io/repository/github/chevere/reponame/badge)](https://www.codefactor.io/repository/github/chevere/reponame) -[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b956754f8ff04aaa9ca24a6e4cc21661)](https://www.codacy.com/gh/chevere/reponame/dashboard) +[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fmessage%2F0.1)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/message/0.1) + +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_message) +[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_message) +[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_message) +[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_message) +[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_message) +[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_message&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_message) +[![CodeFactor](https://www.codefactor.io/repository/github/chevere/message/badge)](https://www.codefactor.io/repository/github/chevere/message) +[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b956754f8ff04aaa9ca24a6e4cc21661)](https://www.codacy.com/gh/chevere/message/dashboard) ## Documentation diff --git a/composer.json b/composer.json index 492f9a9..aca2ab1 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "chevere/reponame", - "description": "A chevere reponame package", + "name": "chevere/message", + "description": "A chevere message package", "homepage": "https://chevere.org", "type": "library", "license": "Apache-2.0", @@ -12,7 +12,7 @@ } ], "require": { - "chevere/chevere": "^3.0" + "php": "^8.1" }, "require-dev": { "phpstan/phpstan": "^1.9", @@ -24,7 +24,7 @@ "src/functions.php" ], "psr-4": { - "Chevere\\%namespace%\\": "src/" + "Chevere\\Message\\": "src/" } }, "autoload-dev": { diff --git a/sonar-project.properties b/sonar-project.properties index 4c6eae7..c919d44 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,4 +1,4 @@ -sonar.projectKey=chevere_reponame +sonar.projectKey=chevere_message sonar.organization=chevere sonar.host.url=https://sonarcloud.io sonar.sourceEncoding=UTF-8 diff --git a/src/.git-keep b/src/.git-keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Interfaces/MessageInterface.php b/src/Interfaces/MessageInterface.php new file mode 100644 index 0000000..5357518 --- /dev/null +++ b/src/Interfaces/MessageInterface.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Message\Interfaces; + +use Stringable; + +/** + * Describes the component in charge of handling rich system messages for CLI and HTML. + */ +interface MessageInterface extends Stringable +{ + /** + * Returns a text message representation. + */ + public function __toString(): string; + + /** + * Provides access to the message template. + */ + public function template(): string; + + /** + * Provides access to the message translation table. + * + * @return array + */ + public function trTable(): array; + + /** + * Returns a console message representation. + */ + public function toConsole(): string; + + /** + * Returns a HTML message representation. + */ + public function toHtml(): string; +} diff --git a/src/Message.php b/src/Message.php new file mode 100644 index 0000000..11c175e --- /dev/null +++ b/src/Message.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Message; + +use Chevere\Message\Interfaces\MessageInterface; +use Stringable; + +final class Message implements MessageInterface +{ + /** + * @var array + */ + private array $trTable = []; + + public function __construct( + private string $template, + float|int|string|Stringable ...$translate + ) { + $array = []; + foreach ($translate as $key => $value) { + $value = (string) $value; + $array["%{$key}%"] = $value; + $array["{{{$key}}}"] = $value; + $array["{{ {$key} }}"] = $value; + } + $this->trTable = $array; + } + + public function __toString(): string + { + return strtr($this->template, $this->trTable); + } + + public function template(): string + { + return $this->template; + } + + public function trTable(): array + { + return $this->trTable; + } + + public function toConsole(): string + { + return $this->__toString(); + } + + public function toHtml(): string + { + return $this->__toString(); + } +} diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..b0ec218 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Message; + +use Chevere\Message\Interfaces\MessageInterface; +use Stringable; + +function message( + string $template, + float|int|string|Stringable ...$translate +): MessageInterface { + return new Message($template, ...$translate); +} diff --git a/tests/.git-keep b/tests/.git-keep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/MessageTest.php b/tests/MessageTest.php new file mode 100644 index 0000000..126766c --- /dev/null +++ b/tests/MessageTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Tests; + +use Chevere\Message\Message; +use PHPUnit\Framework\TestCase; +use function Chevere\Message\message; + +final class MessageTest extends TestCase +{ + public function testConstruct(): void + { + $var = 'message'; + $message = new Message($var); + $this->assertSame($var, $message->template()); + $this->assertSame([], $message->trTable()); + $this->assertSame($var, $message->__toString()); + $this->assertSame($var, $message->toConsole()); + $this->assertSame($var, $message->toHtml()); + } + + public function testTranslate(): void + { + $search = '%translate%'; + $replace = '1'; + $var = 'string ' . $search; + $message = new Message($var, translate: $replace); + $varTr = strtr( + $var, + [ + $search => $replace, + ] + ); + $this->assertSame($var, $message->template()); + $this->assertSame( + [ + '%translate%' => $replace, + '{{translate}}' => $replace, + '{{ translate }}' => $replace, + ], + $message->trTable() + ); + $this->assertSame($varTr, $message->__toString()); + $this->assertSame($varTr, $message->toConsole()); + $this->assertSame($varTr, $message->toHtml()); + } + + public function testFunction(): void + { + $this->assertEquals(message('template'), new Message('template')); + } +}