tex/src/Document.php

148 lines
3.6 KiB
PHP
Raw Normal View History

2022-10-26 16:13:19 +02:00
<?php
namespace Zoomyboy\Tex;
use Closure;
use Illuminate\View\InvokableComponentVariable;
2022-10-31 13:52:41 +01:00
use PHPUnit\Framework\Assert;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
2022-10-31 13:52:41 +01:00
2022-10-26 16:13:19 +02:00
abstract class Document
{
2022-10-28 11:55:09 +02:00
abstract public function basename(): string;
2022-10-27 02:26:32 +02:00
abstract public function view(): string;
2022-10-27 16:54:24 +02:00
abstract public function template(): ?Template;
2022-10-27 02:26:32 +02:00
2022-10-27 16:54:24 +02:00
abstract public function getEngine(): Engine;
2022-10-27 02:26:32 +02:00
2022-10-27 16:54:24 +02:00
public function renderBody(): string
2022-10-26 16:13:19 +02:00
{
2022-10-27 16:54:24 +02:00
return view()
->make($this->view(), $this->data())
2022-10-27 16:54:24 +02:00
->render();
2022-10-26 16:13:19 +02:00
}
2022-10-28 11:55:09 +02:00
2022-10-31 13:52:41 +01:00
public function assertHasContent(string $content): void
{
Assert::assertStringContainsString(
$content,
$this->renderBody(),
"Failed asserting that Content contains {$content}"
);
}
2022-10-28 11:55:09 +02:00
public function hasContent(string $content): bool
{
return str_contains($this->renderBody(), $content);
}
/**
* @param array<int, string> $content
*/
public function hasAllContent(array $content): bool
{
foreach ($content as $check) {
if (!$this->hasContent($check)) {
return false;
}
}
return true;
}
/**
* @param array<int, string> $content
*/
public function missesAllContent(array $content): bool
{
foreach ($content as $check) {
if ($this->hasContent($check)) {
return false;
}
}
return true;
}
2022-10-28 11:55:09 +02:00
public function filename(): string
{
return $this->basename().'.tex';
}
public function compiledFilename(): string
{
return $this->basename().'.pdf';
}
/**
* @return array<string, mixed>
*/
public function data(): array
{
return array_merge($this->extractPublicProperties(), $this->extractPublicMethods());
}
/**
* @return array<string, mixed>
*/
protected function extractPublicProperties(): array
{
$class = get_class($this);
$reflection = new ReflectionClass($this);
$properties = collect($reflection->getProperties(ReflectionProperty::IS_PUBLIC))
->filter(fn (ReflectionProperty $property) => !$property->isStatic());
$values = [];
foreach ($properties as $property) {
$values[$property->getName()] = $this->{$property->getName()};
}
return $values;
}
/**
* Extract the public methods for the component.
*
* @return array<string, Closure|InvokableComponentVariable>
*/
protected function extractPublicMethods()
{
$class = get_class($this);
$reflection = new ReflectionClass($this);
/* @var Collection<string, string> */
$methods = collect($reflection->getMethods(ReflectionMethod::IS_PUBLIC))
->all();
$values = [];
foreach ($methods as $method) {
$values[$method->getName()] = $this->createVariableFromMethod(new ReflectionMethod($this, $method->getName()));
}
return $values;
}
protected function createVariableFromMethod(ReflectionMethod $method): Closure|InvokableComponentVariable
{
return 0 === $method->getNumberOfParameters()
? $this->createInvokableVariable($method->getName())
: Closure::fromCallable([$this, $method->getName()]);
}
protected function createInvokableVariable(string $method): InvokableComponentVariable
{
return new InvokableComponentVariable(function () use ($method) {
return $this->{$method}();
});
}
2022-10-26 16:13:19 +02:00
}