Skip to content

Commit

Permalink
add option to provide type namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 11, 2024
1 parent 43249ab commit b39a3a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Parser/Documentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace PSX\Schema\Parser;

use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\ContextFactory;
use PSX\Schema\Definitions;
use PSX\Schema\Exception\ParserException;
use PSX\Schema\Exception\TypeNotFoundException;
Expand All @@ -41,13 +43,15 @@
*/
class Documentor extends Popo
{
private ContextFactory $contextFactory;
private TypeResolver $typeResolver;
private Popo\Resolver\Documentor $documentor;

public function __construct()
{
parent::__construct();

$this->contextFactory = new ContextFactory();
$this->typeResolver = new TypeResolver();
$this->documentor = new Popo\Resolver\Documentor();
}
Expand All @@ -62,7 +66,12 @@ public function parse(string $schema, ?ContextInterface $context = null): Schema
try {
$typeName = 'InlineType' . substr(md5($schema), 0, 8);

$result = $this->documentor->buildPropertyType($this->typeResolver->resolve($schema));
$parts = explode('@', $schema, 2);
$type = $parts[0] ?? null;
$namespace = $parts[1] ?? null;

$typeContext = !empty($namespace) ? new Context($namespace) : null;
$result = $this->documentor->buildPropertyType($this->typeResolver->resolve($type, $typeContext));

if ($result instanceof MapPropertyType) {
$schema = $result->getSchema();
Expand Down
4 changes: 2 additions & 2 deletions src/SchemaSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public static function fromClass(string $class): self
/**
* A PHP doc type i.e. array<string>
*/
public static function fromType(string $type): self
public static function fromType(string $type, ?string $namespace = null): self
{
return new self('php+doc', $type);
return new self('php+doc', $type . (!empty($namespace) ? '@' . $namespace : ''));
}

public static function fromTypeHub(string $user, string $document, string $version): self
Expand Down

0 comments on commit b39a3a1

Please sign in to comment.