Skip to content

Commit

Permalink
0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Nov 30, 2023
1 parent 39ca750 commit 0bd6379
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 27 deletions.
16 changes: 8 additions & 8 deletions .vscode/templates.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Message;",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -22,7 +22,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Message;",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -36,7 +36,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Message;",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -54,7 +54,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%;",
"namespace Chevere\\Message;",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand All @@ -68,7 +68,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Message\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"/**",
" * Describes the component in charge of ${1:doing}.",
Expand All @@ -85,7 +85,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Message\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"final class $TM_FILENAME_BASE",
"{",
Expand All @@ -99,7 +99,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Message\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"",
Expand All @@ -117,7 +117,7 @@
"body": [
"<?php",
"",
"namespace Chevere\\%namespace%\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"namespace Chevere\\Message\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};",
"",
"trait $TM_FILENAME_BASE",
"{",
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# %namespace%
# Message

> 🔔 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

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -12,7 +12,7 @@
}
],
"require": {
"chevere/chevere": "^3.0"
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^1.9",
Expand All @@ -24,7 +24,7 @@
"src/functions.php"
],
"psr-4": {
"Chevere\\%namespace%\\": "src/"
"Chevere\\Message\\": "src/"
}
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Empty file removed src/.git-keep
Empty file.
49 changes: 49 additions & 0 deletions src/Interfaces/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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<string, string>
*/
public function trTable(): array;

/**
* Returns a console message representation.
*/
public function toConsole(): string;

/**
* Returns a HTML message representation.
*/
public function toHtml(): string;
}
64 changes: 64 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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<string, string>
*/
private array $trTable = [];

public function __construct(
private string $template,
float|int|string|Stringable ...$translate
) {
$array = [];
foreach ($translate as $key => $value) {
$value = (string) $value;

Check warning on line 32 in src/Message.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ { $array = []; foreach ($translate as $key => $value) { - $value = (string) $value; + $value = $value; $array["%{$key}%"] = $value; $array["{{{$key}}}"] = $value; $array["{{ {$key} }}"] = $value;

Check warning on line 32 in src/Message.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ { $array = []; foreach ($translate as $key => $value) { - $value = (string) $value; + $value = $value; $array["%{$key}%"] = $value; $array["{{{$key}}}"] = $value; $array["{{ {$key} }}"] = $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();
}
}
24 changes: 24 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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);
}
Empty file removed tests/.git-keep
Empty file.
63 changes: 63 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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'));
}
}

0 comments on commit 0bd6379

Please sign in to comment.