-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from moufmouf/hide_parameter_annotation
Adding a new @HideParameter annotation
- Loading branch information
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Annotations; | ||
|
||
use BadMethodCallException; | ||
use function ltrim; | ||
|
||
/** | ||
* Use this annotation to tell GraphQLite to ignore a parameter and not expose it as an input parameter. | ||
* This is only possible if the parameter has a default value. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
* @Attributes({ | ||
* @Attribute("for", type = "string") | ||
* }) | ||
*/ | ||
class HideParameter implements ParameterAnnotationInterface | ||
{ | ||
/** @var string */ | ||
private $for; | ||
|
||
/** | ||
* @param array<string, mixed> $values | ||
*/ | ||
public function __construct(array $values) | ||
{ | ||
if (! isset($values['for'])) { | ||
throw new BadMethodCallException('The @HideParameter annotation must be passed a target. For instance: "@HideParameter(for="$myParameterToHide")"'); | ||
} | ||
$this->for = ltrim($values['for'], '$'); | ||
} | ||
|
||
public function getTarget(): string | ||
{ | ||
return $this->for; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Parameters; | ||
|
||
use ReflectionMethod; | ||
use ReflectionParameter; | ||
use TheCodingMachine\GraphQLite\GraphQLException; | ||
use Webmozart\Assert\Assert; | ||
|
||
class CannotHideParameterException extends GraphQLException | ||
{ | ||
public static function needDefaultValue(ReflectionParameter $parameter): self | ||
{ | ||
$method = $parameter->getDeclaringFunction(); | ||
Assert::isInstanceOf($method, ReflectionMethod::class); | ||
|
||
return new self('For parameter $' . $parameter->getName() . ' of method ' . $method->getDeclaringClass()->getName() . '::' . $method->getName() . '(), cannot use the @HideParameter annotation. The parameter needs to provide a default value.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Parameters; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
|
||
/** | ||
* Fills a parameter with a default value. Always. | ||
*/ | ||
class DefaultValueParameter implements ParameterInterface | ||
{ | ||
/** @var mixed */ | ||
private $defaultValue; | ||
|
||
/** | ||
* @param mixed $defaultValue | ||
*/ | ||
public function __construct($defaultValue) | ||
{ | ||
$this->defaultValue = $defaultValue; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $args | ||
* @param mixed $context | ||
* | ||
* @return mixed | ||
*/ | ||
public function resolve(?object $source, array $args, $context, ResolveInfo $info) | ||
{ | ||
return $this->defaultValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters