Skip to content

Commit

Permalink
[Money] Add Twig Bridge (#173)
Browse files Browse the repository at this point in the history
## Description

Closes #19 

## Checklist
- [x] Updated CHANGELOG files
- [x] Updated Documentation
- [x] Unit Tests Created
- [x] php-cs-fixer
  • Loading branch information
JoshuaEstes authored Nov 16, 2023
1 parent d5778b2 commit 7be1fb4
Show file tree
Hide file tree
Showing 35 changed files with 476 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp
* [PR #133](https://github.com/SonsOfPHP/sonsofphp/pull/133) [Pager] New Contract
* [PR #134](https://github.com/SonsOfPHP/sonsofphp/pull/134) [Pager] New Component
* [PR #170](https://github.com/SonsOfPHP/sonsofphp/pull/170) [Link] New Component (PSR-13)
* [PR #173](https://github.com/SonsOfPHP/sonsofphp/pull/173) [Money] Twig Bridge

## [0.3.8]

Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ test-link: phpunit
test-logger: PHPUNIT_TESTSUITE=logger
test-logger: phpunit

test-money: PHPUNIT_TESTSUITE=money
test-money: phpunit

test-pager: PHPUNIT_TESTSUITE=pager
test-pager: phpunit

Expand Down Expand Up @@ -126,8 +129,8 @@ coverage-link: coverage
coverage-logger: PHPUNIT_TESTSUITE=logger
coverage-logger: coverage

coverage-money:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite money --coverage-html $(COVERAGE_DIR)
coverage-money: PHPUNIT_TESTSUITE=money
coverage-money: coverage

coverage-pager: PHPUNIT_TESTSUITE=pager
coverage-pager: coverage
Expand Down
4 changes: 4 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"path": "src/SonsOfPHP/Bridge/Symfony/EventSourcing",
"repository": "git@github.com:SonsOfPHP/event-sourcing-symfony.git"
},
{
"path": "src/SonsOfPHP/Bridge/Twig/Money",
"repository": "git@github.com:SonsOfPHP/money-twig.git"
},
{
"path": "src/SonsOfPHP/Component/EventDispatcher",
"repository": "git@github.com:SonsOfPHP/event-dispatcher.git"
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
"psr/cache": "^2.0 || ^3.0",
"psr/simple-cache": "^3.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/link": "^1.0 || ^2.0"
"psr/link": "^1.0 || ^2.0",
"twig/twig": "^3.0",
"ext-intl": "*"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -102,7 +104,8 @@
"sonsofphp/logger-contract": "self.version",
"sonsofphp/pager-contract": "self.version",
"sonsofphp/pager": "self.version",
"sonsofphp/link": "self.version"
"sonsofphp/link": "self.version",
"sonsofphp/money-twig": "self.version"
},
"autoload": {
"psr-4": {
Expand All @@ -113,6 +116,7 @@
"SonsOfPHP\\Bundle\\Cqrs\\": "src/SonsOfPHP/Bundle/Cqrs",
"SonsOfPHP\\Bridge\\Symfony\\Cqrs\\": "src/SonsOfPHP/Bridge/Symfony/Cqrs",
"SonsOfPHP\\Bridge\\Symfony\\EventSourcing\\": "src/SonsOfPHP/Bridge/Symfony/EventSourcing",
"SonsOfPHP\\Bridge\\Twig\\Money\\": "src/SonsOfPHP/Bridge/Twig/Money",
"SonsOfPHP\\Component\\EventDispatcher\\": "src/SonsOfPHP/Component/EventDispatcher",
"SonsOfPHP\\Component\\EventSourcing\\": "src/SonsOfPHP/Component/EventSourcing",
"SonsOfPHP\\Bridge\\Doctrine\\EventSourcing\\": "src/SonsOfPHP/Bridge/Doctrine/EventSourcing",
Expand Down Expand Up @@ -144,6 +148,7 @@
"src/SonsOfPHP/Bundle/Cqrs/Tests",
"src/SonsOfPHP/Bridge/Symfony/Cqrs/Tests",
"src/SonsOfPHP/Bridge/Symfony/EventSourcing/Tests",
"src/SonsOfPHP/Bridge/Twig/Money/Tests",
"src/SonsOfPHP/Component/EventDispatcher/Tests",
"src/SonsOfPHP/Component/EventSourcing/Tests",
"src/SonsOfPHP/Bridge/Doctrine/EventSourcing/Tests",
Expand Down
45 changes: 45 additions & 0 deletions docs/components/money/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ $currency = new Currency('USD');
$currency = Currency::USD();
```

### Formatters

```php
<?php

use SonsOfPHP\Component\Money\Formatter\IntlMoneyFormatter;
use SonsOfPHP\Component\Money\Money;

$formatter = new IntlMoneyFormatter(new \NumberFormatter('en_US', \NumberFormatter::CURRENCY));
$output = $formatter->format(Money::USD(4.20));
echo $output; // $4.20
```

## Twig Bridge

### Installation

```shell
composer require sonsofphp/money-twig
```

### Usage

#### Add Extension to Twig Environment

```php
<?php

use SonsOfPHP\Bridge\Twig\Money\MoneyExtension;
use SonsOfPHP\Component\Money\Formatter\IntlMoneyFormatter;

$formatter = new IntlMoneyFormatter(new \NumberFormatter('en_US', \NumberFormatter::CURRENCY));
$extension = new MoneyExtension($formatter);

$twig = new \Twig\Environment($loader);
$twig->addExtension($extension);
```

#### Usage in Twig Templates

```twig
Your total is {{ money|format_money }}.
```


## Need Help?

Check out [Sons of PHP's Organization Discussions][discussions].
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ nav:
- Link: components/link/index.md
- Logger: components/logger/index.md
- Money:
- components/money/index.md
- Overview: components/money/index.md
- Currency Providers: components/money/currency-providers.md
- Operators: components/money/operators.md
- Queries: components/money/queries.md
Expand Down
8 changes: 4 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<testsuite name="all">
<!--<directory>src/SonsOfPHP/Bard/src/Tests</directory>-->
<directory>src/SonsOfPHP/Bridge/*/*/Tests</directory>
<directory>src/SonsOfPHP/Bridge/*/*/*/Tests</directory>
<directory>src/SonsOfPHP/Bundle/*/Tests</directory>
<directory>src/SonsOfPHP/Component/*/Tests</directory>
</testsuite>
Expand Down Expand Up @@ -72,6 +73,7 @@
</testsuite>

<testsuite name="money">
<directory>src/SonsOfPHP/Bridge/*/Money/Tests</directory>
<directory>src/SonsOfPHP/Component/Money/Tests</directory>
</testsuite>

Expand All @@ -94,10 +96,8 @@
<exclude>
<!--<directory>src/SonsOfPHP/Bard/src/Tests</directory>-->
<directory>src/SonsOfPHP/Bard/vendor</directory>
<directory>src/SonsOfPHP/Bridge/Doctrine/*/Tests</directory>
<directory>src/SonsOfPHP/Bridge/Doctrine/*/vendor</directory>
<directory>src/SonsOfPHP/Bridge/Symfony/*/Tests</directory>
<directory>src/SonsOfPHP/Bridge/Symfony/*/vendor</directory>
<directory>src/SonsOfPHP/Bridge/*/*/Tests</directory>
<directory>src/SonsOfPHP/Bridge/*/*/vendor</directory>
<directory>src/SonsOfPHP/Bundle/*/Tests</directory>
<directory>src/SonsOfPHP/Bundle/*/vendor</directory>
<directory>src/SonsOfPHP/Component/*/Tests</directory>
Expand Down
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2022 to Present Joshua Estes

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.
40 changes: 40 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/MoneyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Bridge\Twig\Money;

use SonsOfPHP\Contract\Money\MoneyFormatterInterface;
use SonsOfPHP\Contract\Money\MoneyInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
*/
class MoneyExtension extends AbstractExtension
{
public function __construct(
private MoneyFormatterInterface $formatter,
) {}

/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new TwigFilter('format_money', [$this, 'formatMoney']),
];
}

/**
* Formats money
*
* Examples
* MoneyInterface|format_money
*/
public function formatMoney(MoneyInterface $money): string
{
return $this->formatter->format($money);
}
}
17 changes: 17 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Sons of PHP - Money - Twig Bridge
=================================

## Learn More

* [Documentation][docs]
* [Contributing][contributing]
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the
[Mother Repository][mother-repo]
* Get Help & Support using [Discussions][discussions]

[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/money/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3AMoney
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3AMoney
60 changes: 60 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/Tests/MoneyExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Bridge\Twig\Money\Tests;

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Bridge\Twig\Money\MoneyExtension;
use SonsOfPHP\Component\Money\Money;
use SonsOfPHP\Contract\Money\MoneyFormatterInterface;
use Twig\Extension\ExtensionInterface;

/**
* @coversDefaultClass \SonsOfPHP\Bridge\Twig\Money\MoneyExtension
*
* @uses \SonsOfPHP\Component\Money\Amount
* @uses \SonsOfPHP\Component\Money\Currency
* @uses \SonsOfPHP\Component\Money\Money
* @uses \SonsOfPHP\Bridge\Twig\Money\MoneyExtension
*/
final class MoneyExtensionTest extends TestCase
{
private $formatter;

public function setUp(): void
{
$this->formatter = $this->createMock(MoneyFormatterInterface::class);
}

/**
* @covers ::__construct
*/
public function testItHasTheRightInterface(): void
{
$extension = new MoneyExtension($this->formatter);

$this->assertInstanceOf(ExtensionInterface::class, $extension);
}

/**
* @covers ::getFilters
*/
public function testGetFilters(): void
{
$extension = new MoneyExtension($this->formatter);

$this->assertGreaterThan(0, $extension->getFilters());
}

/**
* @covers ::formatMoney
*/
public function testItCanFormatMoney(): void
{
$this->formatter->expects($this->once())->method('format')->willReturn('');

$extension = new MoneyExtension($this->formatter);
$extension->formatMoney(Money::USD(10));
}
}
54 changes: 54 additions & 0 deletions src/SonsOfPHP/Bridge/Twig/Money/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "sonsofphp/money-twig",
"type": "library",
"description": "Twig Extension for the Money Component",
"keywords": [
"money",
"currency",
"ISO4217"
],
"homepage": "https://github.com/SonsOfPHP/money",
"license": "MIT",
"authors": [
{
"name": "Joshua Estes",
"email": "joshua@sonsofphp.com"
}
],
"support": {
"issues": "https://github.com/SonsOfPHP/sonsofphp/issues",
"forum": "https://github.com/orgs/SonsOfPHP/discussions",
"docs": "https://docs.sonsofphp.com"
},
"autoload": {
"psr-4": {
"SonsOfPHP\\Bridge\\Twig\\Money\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"sonsofphp/money": "^0.3.x-dev",
"twig/twig": "^3.0"
},
"extra": {
"sort-packages": true,
"branch-alias": {
"dev-main": "0.3.x-dev"
}
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/JoshuaEstes"
},
{
"type": "tidelift",
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp"
}
]
}
1 change: 0 additions & 1 deletion src/SonsOfPHP/Component/Link/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SonsOfPHP\Component\Link;

use Psr\Link\LinkInterface;
use Psr\Link\EvolvableLinkInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
Expand Down
2 changes: 1 addition & 1 deletion src/SonsOfPHP/Component/Link/LinkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace SonsOfPHP\Component\Link;

use Psr\Link\LinkProviderInterface;
use Psr\Link\LinkInterface;
use Psr\Link\LinkProviderInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
Expand Down
Loading

0 comments on commit 7be1fb4

Please sign in to comment.