generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / PHP 8.1 test on ubuntu-latest
Check warning on line 32 in src/Message.php GitHub Actions / PHP 8.2 test on ubuntu-latest
|
||
$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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |