-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added json, xml and txt invoice renderer (#1576)
- Loading branch information
1 parent
c89750f
commit 1ca1b00
Showing
19 changed files
with
560 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai time-tracking app. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Invoice\Renderer; | ||
|
||
use App\Entity\InvoiceDocument; | ||
use App\Invoice\InvoiceFilename; | ||
use App\Invoice\InvoiceModel; | ||
use App\Invoice\RendererInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Twig\Environment; | ||
|
||
final class JsonRenderer implements RendererInterface | ||
{ | ||
/** | ||
* @var Environment | ||
*/ | ||
private $twig; | ||
|
||
public function __construct(Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
public function supports(InvoiceDocument $document): bool | ||
{ | ||
return stripos($document->getFilename(), '.json.twig') !== false; | ||
} | ||
|
||
public function render(InvoiceDocument $document, InvoiceModel $model): Response | ||
{ | ||
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [ | ||
'model' => $model | ||
]); | ||
$filename = (string) new InvoiceFilename($model); | ||
|
||
$response = new Response($content); | ||
|
||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.json'); | ||
|
||
$response->headers->set('Content-Type', 'application/json'); | ||
$response->headers->set('Content-Disposition', $disposition); | ||
|
||
return $response; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai time-tracking app. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Invoice\Renderer; | ||
|
||
use App\Entity\InvoiceDocument; | ||
use App\Invoice\InvoiceFilename; | ||
use App\Invoice\InvoiceModel; | ||
use App\Invoice\RendererInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Twig\Environment; | ||
|
||
final class TextRenderer implements RendererInterface | ||
{ | ||
/** | ||
* @var Environment | ||
*/ | ||
private $twig; | ||
|
||
public function __construct(Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
public function supports(InvoiceDocument $document): bool | ||
{ | ||
return stripos($document->getFilename(), '.txt.twig') !== false; | ||
} | ||
|
||
public function render(InvoiceDocument $document, InvoiceModel $model): Response | ||
{ | ||
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [ | ||
'model' => $model | ||
]); | ||
$filename = (string) new InvoiceFilename($model); | ||
|
||
$response = new Response($content); | ||
|
||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.txt'); | ||
|
||
$response->headers->set('Content-Type', 'text/plain'); | ||
$response->headers->set('Content-Disposition', $disposition); | ||
|
||
return $response; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai time-tracking app. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Invoice\Renderer; | ||
|
||
use App\Entity\InvoiceDocument; | ||
use App\Invoice\InvoiceFilename; | ||
use App\Invoice\InvoiceModel; | ||
use App\Invoice\RendererInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Twig\Environment; | ||
|
||
final class XmlRenderer implements RendererInterface | ||
{ | ||
/** | ||
* @var Environment | ||
*/ | ||
private $twig; | ||
|
||
public function __construct(Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
public function supports(InvoiceDocument $document): bool | ||
{ | ||
return stripos($document->getFilename(), '.xml.twig') !== false; | ||
} | ||
|
||
public function render(InvoiceDocument $document, InvoiceModel $model): Response | ||
{ | ||
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [ | ||
'model' => $model | ||
]); | ||
$filename = (string) new InvoiceFilename($model); | ||
|
||
$response = new Response($content); | ||
|
||
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename . '.xml'); | ||
|
||
$response->headers->set('Content-Type', 'application/xml'); | ||
$response->headers->set('Content-Disposition', $disposition); | ||
|
||
return $response; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% set json = model.toArray %} | ||
{% set items = [] %} | ||
{% for entry in model.calculator.entries %} | ||
{% set items = items|merge([model.itemToArray(entry)]) %} | ||
{% endfor %} | ||
{% set json = json|merge({'items': items}) %} | ||
{{ json|json_encode|raw}} |
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,19 @@ | ||
{% for key, value in model.toArray %} | ||
{{ key }}: | ||
{% if value is not empty %} | ||
{{ value|multiline_indent(' ') }} | ||
{% endif %} | ||
|
||
{% endfor %} | ||
{% for entry in model.calculator.entries %} | ||
--- | ||
|
||
{% set items = model.itemToArray(entry) %} | ||
{% for key, value in items %} | ||
{{ key }}: | ||
{% if value is not empty %} | ||
{{ value|multiline_indent(' ') }} | ||
{% endif %} | ||
|
||
{% endfor %} | ||
{% endfor %} |
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<kimai version="{{ constant('App\\Constants::VERSION') }}"> | ||
{% for key, value in model.toArray %} | ||
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %} | ||
|
||
{% endfor %} | ||
<items> | ||
{%- for entry in model.calculator.entries %} | ||
|
||
<item> | ||
{% for key, value in model.itemToArray(entry) %} | ||
<{{ key }}{% if value is empty %}/>{% else %}>{{ value|escape }}</{{ key }}>{% endif %} | ||
|
||
{% endfor %} | ||
</item> | ||
{%- endfor %} | ||
|
||
</items> | ||
</kimai> |
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
Oops, something went wrong.