Skip to content

Commit

Permalink
Rename MACROS to LISTENERS (#8)
Browse files Browse the repository at this point in the history
- MacrosRenderer moved to ListenersRenderer
- All MACROS renamed to LISTENERS
  • Loading branch information
roxblnfk authored Dec 16, 2021
1 parent 2d14ab2 commit 0b0ba21
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 130 deletions.
85 changes: 85 additions & 0 deletions src/ConsoleRenderer/Renderer/ListenersRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

class ListenersRenderer implements Renderer
{
private int $property;
private string $title;

public function __construct(int $property, string $title)
{
$this->property = $property;
$this->title = $title;
}

public function render(Formatter $formatter, array $schema, string $role): ?string
{
if (! isset($schema[$this->property])) {
return null;
}

$listeners = (array)($schema[$this->property] ?? []);

if ($listeners === []) {
return null;
}

$rows = [
\sprintf('%s:', $formatter->title($this->title)),
];

foreach ($listeners as $definition) {
$data = (array)$definition;

// Listener class
$class = \is_string($data[0] ?? null)
? $formatter->typecast($data[0])
: $formatter->error('undefined');
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class);

// Listener arguments
$args = $data[1] ?? null;
if ($args === null) {
continue;
}
if (\is_array($args)) {
foreach ($args as $key => $value) {
$row = $formatter->title(' ') . ' - ';
if (\is_string($key)) {
$row .= $formatter->property($key) . ' : ';
}
$row .= $formatter->info($this->printValue($value, $formatter));
$rows[] = $row;
}
} elseif (is_string($args)) {
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($args, $formatter));
} else {
$rows[] = $formatter->typecast($this->printValue($data, $formatter));
}
}

return \implode($formatter::LINE_SEPARATOR, $rows);
}

/**
* @param mixed $value
*/
private function printValue($value, Formatter $formatter): string
{
$data = \trim(\var_export($value, true), '\'');
$data = \array_map(
static fn (string $row): string => $formatter->title(' ') . $row,
\explode("\n", $data)
);

return \ltrim(
\implode("\n", $data)
);
}
}
81 changes: 4 additions & 77 deletions src/ConsoleRenderer/Renderer/MacrosRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,9 @@

namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

class MacrosRenderer implements Renderer
/**
* @deprecated use {@see ListenersRenderer} instead
*/
class MacrosRenderer extends ListenersRenderer
{
private int $property;
private string $title;

public function __construct(int $property, string $title)
{
$this->property = $property;
$this->title = $title;
}

public function render(Formatter $formatter, array $schema, string $role): ?string
{
if (! isset($schema[$this->property])) {
return null;
}

$macrosList = (array)($schema[$this->property] ?? []);

if ($macrosList === []) {
return null;
}

$rows = [
\sprintf('%s:', $formatter->title($this->title)),
];

foreach ($macrosList as $definition) {
$data = (array)$definition;

// macros class
$class = \is_string($data[0] ?? null)
? $formatter->typecast($data[0])
: $formatter->error('undefined');
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class);

// macros params
$params = $data[1] ?? null;
if ($params === null) {
continue;
}
if (\is_array($params)) {
foreach ($params as $key => $value) {
$row = $formatter->title(' ') . ' - ';
if (\is_string($key)) {
$row .= $formatter->property($key) . ' : ';
}
$row .= $formatter->info($this->printValue($value, $formatter));
$rows[] = $row;
}
} elseif (is_string($params)) {
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($params, $formatter));
} else {
$rows[] = $formatter->typecast($this->printValue($data, $formatter));
}
}

return \implode($formatter::LINE_SEPARATOR, $rows);
}

/**
* @param mixed $value
*/
private function printValue($value, Formatter $formatter): string
{
$data = \trim(\var_export($value, true), '\'');
$data = \array_map(
static fn (string $row): string => $formatter->title(' ') . $row,
\explode("\n", $data)
);

return \ltrim(
\implode("\n", $data)
);
}
}
8 changes: 4 additions & 4 deletions src/OutputSchemaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ColumnsRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\CustomPropertiesRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\KeysRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\PropertyRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\RelationsRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\TitleRenderer;
Expand Down Expand Up @@ -75,13 +75,13 @@ public function __construct(int $format = self::FORMAT_CONSOLE_COLOR, ?Constants
$this->addRenderer(new PropertyRenderer($constants['TYPECAST_HANDLER'], 'Typecast'));
}

if (isset($constants['MACROS'])) {
$this->addRenderer(new MacrosRenderer($constants['MACROS'], 'Macros'));
if (isset($constants['LISTENERS'])) {
$this->addRenderer(new ListenersRenderer($constants['LISTENERS'], 'Listeners'));
}

$this->addRenderer(
new RelationsRenderer(),
new CustomPropertiesRenderer(array_values($constants)),
new CustomPropertiesRenderer(\array_values($constants)),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer;
use Cycle\Schema\Renderer\Tests\BaseTest;

final class MacrosRendererTest extends BaseTest
final class ListenersRendererTest extends BaseTest
{
private Formatter $formatter;

Expand All @@ -22,7 +22,7 @@ protected function setUp(): void

public function testRenderNotExistsProperty()
{
$renderer = new MacrosRenderer(1, 'Foo');
$renderer = new ListenersRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [], 'baz');

Expand All @@ -31,7 +31,7 @@ public function testRenderNotExistsProperty()

public function testRenderStringProperty()
{
$renderer = new MacrosRenderer(1, 'Foo');
$renderer = new ListenersRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [
1 => 'bar',
Expand All @@ -49,46 +49,46 @@ public function testRenderStringProperty()

public function testRenderArrayProperty()
{
$renderer = new MacrosRenderer(1, 'Foo');
$renderer = new ListenersRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [
1 => ['foo-macros', 'bar-macros'],
1 => ['foo-listener', 'bar-listener'],
], 'baz');

$this->assertSame(
<<<'OUTPUT'
Foo:
foo-macros
bar-macros
OUTPUT
Foo:
foo-listener
bar-listener
OUTPUT
,
$result
);
}

public function testRenderAssocArrayProperty()
{
$renderer = new MacrosRenderer(1, 'Foo');
$renderer = new ListenersRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [
1 => [
['foo-macros', []],
['bar-macros', ['baz' => 'bar', 'baz1', 'baz2']],
['baz-macros', 'baz'],
['foo-listener', []],
['bar-listener', ['baz' => 'bar', 'baz1', 'baz2']],
['baz-listener', 'baz'],
],
], 'baz');

$this->assertSame(
<<<'OUTPUT'
Foo:
foo-macros
bar-macros
- baz : bar
- baz1
- baz2
baz-macros
- baz
OUTPUT
Foo:
foo-listener
bar-listener
- baz : bar
- baz1
- baz2
baz-listener
- baz
OUTPUT
,
$result
);
Expand Down
Loading

0 comments on commit 0b0ba21

Please sign in to comment.