Skip to content

Commit

Permalink
Merge pull request #6 from php-soap/finishing-up-02
Browse files Browse the repository at this point in the history
Fancy meta table in inspection details
  • Loading branch information
veewee committed Apr 1, 2023
2 parents 6a02dbf + 75f9885 commit 9d834e8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
14 changes: 6 additions & 8 deletions src/Console/Command/InspectMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Soap\Engine\Metadata\Model\Method;
use Soap\Wsdl\Console\Helper\ConfiguredLoader;
use Soap\WsdlReader\Formatter\LongMethodFormatter;
use Soap\WsdlReader\Formatter\MetaTableFormatter;
use Soap\WsdlReader\Metadata\Wsdl1MetadataProvider;
use Soap\WsdlReader\Wsdl1Reader;
use Symfony\Component\Console\Command\Command;
Expand All @@ -17,7 +18,6 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use function Psl\Type\non_empty_string;
use function Psl\Vec\filter;
use function Psl\Vec\map;

final class InspectMethodCommand extends Command
{
Expand Down Expand Up @@ -56,13 +56,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::FAILURE;
}

$style->info('Methods:');
$style->writeln(
map(
$detectedMethods,
static fn (Method $method) => ' > '.(new LongMethodFormatter())($method)
)
);
foreach ($detectedMethods as $detectedMethod) {
$style->title($detectedMethod->getName());
$style->writeln(['> '.(new LongMethodFormatter())($detectedMethod), '']);
(new MetaTableFormatter($output))($detectedMethod->getMeta())->render();
}

return self::SUCCESS;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Console/Command/InspectTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Soap\Engine\Metadata\Model\Type;
use Soap\Wsdl\Console\Helper\ConfiguredLoader;
use Soap\WsdlReader\Formatter\LongTypeFormatter;
use Soap\WsdlReader\Formatter\MetaTableFormatter;
use Soap\WsdlReader\Metadata\Wsdl1MetadataProvider;
use Soap\WsdlReader\Wsdl1Reader;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -54,9 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::FAILURE;
}

$style->writeln(
$detectedTypes->map(static fn (Type $type) => ' > '.(new LongTypeFormatter())($type))
);
foreach ($detectedTypes as $detectedType) {
$style->title($detectedType->getName());
$style->writeln(['> '.(new LongTypeFormatter())($detectedType), '']);
(new MetaTableFormatter($output))($detectedType->getXsdType()->getMeta())->render();
}

return self::SUCCESS;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Formatter/LongMethodFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct()
public function __invoke(Method $method): string
{
return format(
'%s(%s): %s%s',
'%s(%s): %s',
$method->getName(),
implode(', ', $method->getParameters()->map(
fn (Parameter $parameter): string => format(
Expand All @@ -28,8 +28,7 @@ public function __invoke(Method $method): string
$parameter->getName()
)
)),
($this->xsdTypeFormatter)($method->getReturnType()),
PHP_EOL. ' '.print_r($method->getMeta(), true)
($this->xsdTypeFormatter)($method->getReturnType())
);
}
}
66 changes: 66 additions & 0 deletions src/Formatter/MetaTableFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Formatter;

use ReflectionClass;
use ReflectionProperty;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;
use function Psl\Vec\filter_nulls;
use function Psl\Vec\map;

final class MetaTableFormatter
{
public function __construct(
private OutputInterface $output
) {
}

public function __invoke(object $object): Table
{
$table = new Table($this->output);
$table
->setHeaders(['Meta', 'Value'])
->setRows($this->buildKeyPairs($object));

return $table;
}

/**
* @return list<array{0: non-empty-string, 1: string}>
*/
private function buildKeyPairs(object $object): array
{
$rc = new ReflectionClass($object);

return filter_nulls(
map(
$rc->getProperties(),
/** @return array{0: non-empty-string, 1: string}|null */
function (ReflectionProperty $prop) use ($object): ?array {
$value = $this->tryStringifyValue($prop->getValue($object));
if ($value === null) {
return null;
}

return [$prop->getName(), $value];
}
)
);
}

private function tryStringifyValue(mixed $value): ?string
{
try {
return match (true) {
is_array($value) => json_encode($value, JSON_PRETTY_PRINT),
is_bool($value) => $value ? 'true' : 'false',
is_scalar($value) => (string)$value,
default => null,
};
} catch (\Throwable) {
return null;
}
}
}

0 comments on commit 9d834e8

Please sign in to comment.