Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
Improve inputable and renderable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
knesmeyanov committed Aug 21, 2018
1 parent fdf8d81 commit 97fe688
Show file tree
Hide file tree
Showing 33 changed files with 198 additions and 363 deletions.
45 changes: 3 additions & 42 deletions src/AbstractTypeDefinition.php
Expand Up @@ -12,22 +12,19 @@
use Railt\Reflection\Contracts\Definition\TypeDefinition;
use Railt\Reflection\Definition\Behaviour\HasDeprecation;
use Railt\Reflection\Definition\Behaviour\HasInheritance;
use Railt\Reflection\Definition\Behaviour\HasName;
use Railt\Reflection\Invocation\Behaviour\HasDirectives;

/**
* Class AbstractTypeDefinition
*/
abstract class AbstractTypeDefinition extends AbstractDefinition implements TypeDefinition
{
use HasName;
use HasDirectives;
use HasDeprecation;
use HasInheritance;

/**
* @var string
*/
protected $name;

/**
* @var string|null
*/
Expand All @@ -40,8 +37,7 @@ abstract class AbstractTypeDefinition extends AbstractDefinition implements Type
*/
public function __construct(Document $document, string $name)
{
$this->name = $name;

$this->renameTo($name);
parent::__construct($document);
}

Expand All @@ -64,25 +60,6 @@ public function withDescription(?string $description): TypeDefinition
return $this;
}

/**
* @param string $name
* @return TypeDefinition|$this
*/
public function renameTo(string $name): TypeDefinition
{
$this->name = $name;

return $this;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return bool
*/
Expand All @@ -98,20 +75,4 @@ public function __toString(): string
{
return \sprintf('%s<%s>', $this->name ?? '?', static::getType());
}

/**
* @return bool
*/
public function isRenderable(): bool
{
return false;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return false;
}
}
Expand Up @@ -7,33 +7,28 @@
*/
declare(strict_types=1);

namespace Railt\Reflection\Invocation;
namespace Railt\Reflection;

use Railt\Reflection\AbstractDefinition;
use Railt\Reflection\Contracts\Definition\TypeDefinition;
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
use Railt\Reflection\Document;
use Railt\Reflection\Contracts\Document as DocumentInterface;
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
use Railt\Reflection\Definition\Behaviour\HasName;

/**
* Class AbstractTypeInvocation
*/
abstract class AbstractTypeInvocation extends AbstractDefinition implements TypeInvocation
{
/**
* @var string
*/
protected $name;
use HasName;

/**
* AbstractTypeInvocation constructor.
* @param Document|DocumentInterface $document
* @param string $name
* @param string|null $name
*/
public function __construct(Document $document, string $name)
public function __construct(Document $document, ?string $name)
{
$this->name = $name;

$this->renameTo($name);
parent::__construct($document);
}

Expand All @@ -43,15 +38,7 @@ public function __construct(Document $document, string $name)
*/
public function getDefinition(): TypeDefinition
{
return $this->fetch($this->name);
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
return $this->fetch($this->getName());
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Contracts/Definition/Behaviour/Nameable.php
@@ -0,0 +1,23 @@
<?php
/**
* This file is part of Railt package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace Railt\Reflection\Contracts\Definition\Behaviour;

/**
* Interface Nameable
*/
interface Nameable
{
/**
* Returns the name of definition instance.
*
* @return string
*/
public function getName(): string;
}
2 changes: 1 addition & 1 deletion src/Contracts/Definition/SchemaDefinition.php
Expand Up @@ -17,7 +17,7 @@ interface SchemaDefinition extends TypeDefinition
/**
* @var string
*/
public const DEFAULT_SCHEMA_NAME = 'Schema';
public const DEFAULT_SCHEMA_NAME = ':schema';

/**
* @return ObjectDefinition
Expand Down
27 changes: 2 additions & 25 deletions src/Contracts/Definition/TypeDefinition.php
Expand Up @@ -13,43 +13,20 @@
use Railt\Reflection\Contracts\Definition\Behaviour\Deprecatable;
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesInheritance;
use Railt\Reflection\Contracts\Invocation\Behaviour\ProvidesDirectives;
use Railt\Reflection\Contracts\Definition\Behaviour\Nameable;

/**
* Interface TypeDefinition
*/
interface TypeDefinition extends Deprecatable, ProvidesDirectives, ProvidesInheritance, Definition
interface TypeDefinition extends Deprecatable, Nameable, ProvidesDirectives, ProvidesInheritance, Definition
{
/**
* Returns the name of definition instance.
*
* @return string
*/
public function getName(): string;

/**
* Returns a short description of definition.
*
* @return string
*/
public function getDescription(): string;

/**
* Returns true when a type can be rendered as a JSON
* value or a structure, and they can also be operated
* on in the GraphQL query.
*
* @return bool
*/
public function isRenderable(): bool;

/**
* Returns true in the event that the type can be represented
* as a value and passed as an argument to the GraphQL request.
*
* @return bool
*/
public function isInputable(): bool;

/**
* Returns true if the type definition is built-in.
*
Expand Down
8 changes: 2 additions & 6 deletions src/Contracts/Invocation/TypeInvocation.php
Expand Up @@ -11,19 +11,15 @@

use Railt\Reflection\Contracts\Definition;
use Railt\Reflection\Contracts\Definition\TypeDefinition;
use Railt\Reflection\Contracts\Definition\Behaviour\Nameable;

/**
* Interface TypeInvocation
*/
interface TypeInvocation extends Definition
interface TypeInvocation extends Definition, Nameable
{
/**
* @return TypeDefinition
*/
public function getDefinition(): TypeDefinition;

/**
* @return string
*/
public function getName(): string;
}
46 changes: 46 additions & 0 deletions src/Definition/Behaviour/HasName.php
@@ -0,0 +1,46 @@
<?php
/**
* This file is part of Railt package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace Railt\Reflection\Definition\Behaviour;

use Railt\Reflection\Contracts\Definition\Behaviour\Nameable;

/**
* Trait HasName
*/
trait HasName
{
/**
* @var string|null
*/
protected $name;

/**
* @return string
*/
public function getName(): string
{
if ($this->name === null) {
return \spl_object_hash($this) . '@anonymous';
}

return $this->name;
}

/**
* @param string|null $name
* @return Nameable|$this
*/
public function renameTo(?string $name): Nameable
{
$this->name = $name;

return $this;
}
}
16 changes: 0 additions & 16 deletions src/Definition/DirectiveDefinition.php
Expand Up @@ -47,20 +47,4 @@ public function isAllowedFor(TypeDefinition $definition): bool

return false;
}

/**
* @return bool
*/
public function isRenderable(): bool
{
return false;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return false;
}
}
17 changes: 0 additions & 17 deletions src/Definition/EnumDefinition.php
Expand Up @@ -29,21 +29,4 @@ public static function getType(): TypeInterface
{
return Type::of(Type::ENUM);
}


/**
* @return bool
*/
public function isRenderable(): bool
{
return true;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return true;
}
}
16 changes: 0 additions & 16 deletions src/Definition/InputDefinition.php
Expand Up @@ -29,20 +29,4 @@ public static function getType(): TypeInterface
{
return Type::of(Type::INPUT_OBJECT);
}

/**
* @return bool
*/
public function isRenderable(): bool
{
return false;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return true;
}
}
16 changes: 0 additions & 16 deletions src/Definition/InputUnionDefinition.php
Expand Up @@ -29,20 +29,4 @@ public static function getType(): TypeInterface
{
return Type::of(Type::INPUT_UNION);
}

/**
* @return bool
*/
public function isRenderable(): bool
{
return false;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return true;
}
}
16 changes: 0 additions & 16 deletions src/Definition/InterfaceDefinition.php
Expand Up @@ -44,20 +44,4 @@ public function instanceOf($definition): bool
return $this->isImplements($definition) ||
parent::instanceOf($definition);
}

/**
* @return bool
*/
public function isRenderable(): bool
{
return true;
}

/**
* @return bool
*/
public function isInputable(): bool
{
return false;
}
}

0 comments on commit 97fe688

Please sign in to comment.