Skip to content

Commit

Permalink
add stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 7, 2024
1 parent be7ae2d commit 6660c93
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 38 deletions.
24 changes: 24 additions & 0 deletions .github/banner/message-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/banner/message-social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,72 @@
[![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/37a9acc4f6bc49b89a898302fc9330f4)](https://app.codacy.com/gh/chevere/message/dashboard)

![Message](.github/banner/message-logo.svg)

## Summary

Message enables to create strings with template tags support.

## Installing

Message is available through [Packagist](https://packagist.org/packages/chevere/message) and the repository source is at [chevere/message](https://github.com/chevere/message).

```sh
composer require chevere/message
```

## Creating a Message

Use function `message` to create a Message by passing the message template. Use named named arguments to define replacement pairs.

```php
use function Chevere\Message\message;

$message = message(
'Hello, **%tag%**!',
tag: 'World'
);
```

🪄 Message supports `%tag%`, `{{tag}}` and `{{ tag }}` replacement template tags.

## To string

The `__toString` method return the message with translated placeholders.

```php
$message->__toString();
// Hello, **World**!
```

## Utility methods

### Template

Use `template` method to return the message template.

```php
$message->replacements();
// Hello, **%tag%**!
```

### Replacements

Use `replacements` method to read message replacement pairs.

```php
$message->replacements();
// ['tag' => 'World']
```

## Documentation

Documentation is available at [chevere.org](https://chevere.org/).
Documentation is available at [chevere.org](https://chevere.org/packages/message).

## License

Copyright 2023 [Rodolfo Berrios A.](https://rodolfoberrios.com/)
Copyright 2024 [Rodolfo Berrios A.](https://rodolfoberrios.com/)

Chevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.

Expand Down
15 changes: 1 addition & 14 deletions src/Interfaces/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

use Stringable;

/**
* Describes the component in charge of handling rich system messages for CLI and HTML.
*/
interface MessageInterface extends Stringable
{
/**
Expand All @@ -35,15 +32,5 @@ public function template(): string;
*
* @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;
public function replacements(): array;
}
20 changes: 5 additions & 15 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Message implements MessageInterface
/**
* @var array<string, string>
*/
private array $trTable = [];
private array $replacements = [];

public function __construct(
private string $template,
Expand All @@ -34,31 +34,21 @@ public function __construct(
$array["{{{$key}}}"] = $value;
$array["{{ {$key} }}"] = $value;
}
$this->trTable = $array;
$this->replacements = $array;
}

public function __toString(): string
{
return strtr($this->template, $this->trTable);
return strtr($this->template, $this->replacements);
}

public function template(): string
{
return $this->template;
}

public function trTable(): array
public function replacements(): array
{
return $this->trTable;
}

public function toConsole(): string
{
return $this->__toString();
}

public function toHtml(): string
{
return $this->__toString();
return $this->replacements;
}
}
8 changes: 2 additions & 6 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public function testConstruct(): void
$var = 'message';
$message = new Message($var);
$this->assertSame($var, $message->template());
$this->assertSame([], $message->trTable());
$this->assertSame([], $message->replacements());
$this->assertSame($var, $message->__toString());
$this->assertSame($var, $message->toConsole());
$this->assertSame($var, $message->toHtml());
}

public function testTranslate(): void
Expand All @@ -49,11 +47,9 @@ public function testTranslate(): void
'{{translate}}' => $replace,
'{{ translate }}' => $replace,
],
$message->trTable()
$message->replacements()
);
$this->assertSame($varTr, $message->__toString());
$this->assertSame($varTr, $message->toConsole());
$this->assertSame($varTr, $message->toHtml());
}

public function testFunction(): void
Expand Down

0 comments on commit 6660c93

Please sign in to comment.