Skip to content

Commit 6f448db

Browse files
committed
Initial commit
0 parents  commit 6f448db

21 files changed

+950
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
/composer.lock
3+
/build
4+
.php_cs_cache
5+
.phpunit.result.cache

.travis.yml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
language: php
2+
sudo: false
3+
cache:
4+
directories:
5+
- $HOME/.composer/cache/files
6+
#- $HOME/symfony-bridge/.phpunit
7+
8+
env:
9+
global:
10+
- PHPUNIT_FLAGS="-v"
11+
#- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"
12+
13+
matrix:
14+
fast_finish: true
15+
include:
16+
# Test the latest stable release
17+
- php: 7.2
18+
- php: 7.3
19+
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"
20+
21+
# Test LTS versions.
22+
- php: 7.3
23+
env: DEPENDENCIES="symfony/lts:^4"
24+
25+
# Latest commit to master
26+
- php: 7.3
27+
env: STABILITY="dev"
28+
29+
allow_failures:
30+
# Minimum supported dependencies with the latest and oldest PHP version
31+
- php: 7.3
32+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
33+
- php: 7.2
34+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
35+
# Dev-master is allowed to fail.
36+
- env: STABILITY="dev"
37+
38+
before_install:
39+
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
40+
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
41+
- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;
42+
43+
install:
44+
# To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355
45+
- if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi
46+
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
47+
#- ./vendor/bin/simple-phpunit install
48+
49+
script:
50+
- composer validate --strict --no-check-lock
51+
# simple-phpunit is the PHPUnit wrapper provided by the PHPUnit Bridge component and
52+
# it helps with testing legacy code and deprecations (composer require symfony/phpunit-bridge)
53+
#- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
54+
- composer phpstan
55+
- ./vendor/bin/phpunit
56+
57+
after_script:
58+
- ./vendor/bin/php-coveralls -v

GraphqliteBundle.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Graphqlite\Bundle;
5+
6+
use TheCodingMachine\Graphqlite\Bundle\DependencyInjection\OverblogGraphiQLEndpointWiringPass;
7+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\HttpKernel\Bundle\Bundle;
10+
use TheCodingMachine\Graphqlite\Bundle\DependencyInjection\GraphqliteCompilerPass;
11+
12+
class GraphqliteBundle extends Bundle
13+
{
14+
public function build(ContainerBuilder $container)
15+
{
16+
parent::build($container);
17+
18+
$container->addCompilerPass(new GraphqliteCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
19+
$container->addCompilerPass(new OverblogGraphiQLEndpointWiringPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1);
20+
}
21+
}

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[![Latest Stable Version](https://poser.pugx.org/thecodingmachine/graphqlite-symfony-validator-bridge/v/stable)](https://packagist.org/packages/thecodingmachine/graphqlite-symfony-validator-bridge)
2+
[![Latest Unstable Version](https://poser.pugx.org/thecodingmachine/graphqlite-symfony-validator-bridge/v/unstable)](https://packagist.org/packages/thecodingmachine/graphqlite-symfony-validator-bridge)
3+
[![License](https://poser.pugx.org/thecodingmachine/graphqlite-symfony-validator-bridge/license)](https://packagist.org/packages/thecodingmachine/graphqlite-symfony-validator-bridge)
4+
[![Build Status](https://travis-ci.org/thecodingmachine/graphqlite-symfony-validator-bridge.svg?branch=master)](https://travis-ci.org/thecodingmachine/graphqlite-symfony-validator-bridge)
5+
[![Coverage Status](https://coveralls.io/repos/thecodingmachine/graphqlite-symfony-validator-bridge/badge.svg?branch=master&service=github)](https://coveralls.io/github/thecodingmachine/graphqlite-symfony-validator-bridge?branch=master)
6+
7+
8+
# Graphqlite - Symfony validator bridge
9+
10+
A bridge between GraphQLite and the Symfony Validator component
11+
12+
See [thecodingmachine/graphqlite](https://github.com/thecodingmachine/graphqlite)

composer.json

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name" : "thecodingmachine/graphqlite-symfony-validator-bridge",
3+
"description" : "A bridge to use Symfony validator with GraphQLite.",
4+
"keywords" : [
5+
"graphql",
6+
"graphqlite",
7+
"validator",
8+
"symfony"
9+
],
10+
"homepage" : "https://graphqlite.thecodingmachine.io/",
11+
"type" : "library",
12+
"license" : "MIT",
13+
"authors" : [{
14+
"name" : "David Négrier",
15+
"email" : "[email protected]",
16+
"homepage" : "http://mouf-php.com"
17+
}
18+
],
19+
"require" : {
20+
"php" : ">=7.2",
21+
"thecodingmachine/graphqlite" : "~4.0.0",
22+
"symfony/validator": "^4.0",
23+
"doctrine/annotations": "^1.6"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^8.4.1",
27+
"mouf/picotainer": "^1.1",
28+
"phpstan/phpstan": "^0.11.16",
29+
"php-coveralls/php-coveralls": "^2.1.0",
30+
"symfony/translation": "^4",
31+
"doctrine/coding-standard": "^6.0"
32+
},
33+
"scripts": {
34+
"phpstan": "phpstan analyse src/ -c phpstan.neon --level=7 --no-progress",
35+
"cs-check": "phpcs",
36+
"cs-fix": "phpcbf",
37+
"test": ["@cs-check", "@phpstan", "phpunit"]
38+
},
39+
"autoload" : {
40+
"psr-4" : {
41+
"TheCodingMachine\\Graphqlite\\Validator\\" : "src/"
42+
}
43+
},
44+
"autoload-dev" : {
45+
"psr-4" : {
46+
"TheCodingMachine\\Graphqlite\\Validator\\" : "tests/"
47+
}
48+
},
49+
"extra": {
50+
"branch-alias": {
51+
"dev-master": "4.0.x-dev"
52+
}
53+
},
54+
"minimum-stability": "dev",
55+
"prefer-stable": true
56+
}

phpcs.xml.dist

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="."/>
4+
<arg name="extensions" value="php"/>
5+
<arg name="parallel" value="16"/>
6+
<arg name="cache" value=".php_cs_cache"/>
7+
<arg name="colors"/>
8+
9+
<!-- Ignore warnings, show progress of the run and show sniff names -->
10+
<arg value="nps"/>
11+
12+
<!-- Directories to be checked -->
13+
<file>src</file>
14+
<!-- <file>tests</file> -->
15+
16+
<!-- Include full Doctrine Coding Standard -->
17+
<rule ref="Doctrine">
18+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
19+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix"/>
20+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming.SuperfluousSuffix"/>
21+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
22+
<exclude name="Squiz.Commenting.FunctionComment.InvalidNoReturn" />
23+
<exclude name="Generic.Formatting.MultipleStatementAlignment" />
24+
</rule>
25+
26+
<!-- Do not align assignments -->
27+
<rule ref="Generic.Formatting.MultipleStatementAlignment">
28+
<severity>0</severity>
29+
</rule>
30+
31+
<!-- Do not align comments -->
32+
<rule ref="Squiz.Commenting.FunctionComment.SpacingAfterParamName">
33+
<severity>0</severity>
34+
</rule>
35+
<rule ref="Squiz.Commenting.FunctionComment.SpacingAfterParamType">
36+
<severity>0</severity>
37+
</rule>
38+
39+
<!-- Require no space before colon in return types -->
40+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
41+
<properties>
42+
<property name="spacesCountBeforeColon" value="0"/>
43+
</properties>
44+
</rule>
45+
</ruleset>

phpstan.neon

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
ignoreErrors:
3+
4+
#includes:
5+
# - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

phpunit.xml.dist

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
bootstrap="vendor/autoload.php"
12+
>
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./src</directory>
22+
</whitelist>
23+
</filter>
24+
<logging>
25+
<log type="coverage-html" target="build/coverage"/>
26+
<log type="coverage-clover" target="build/logs/clover.xml"/>
27+
</logging>
28+
</phpunit>

src/Annotations/Assert.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\Graphqlite\Validator\Annotations;
6+
7+
use BadMethodCallException;
8+
use Symfony\Component\Validator\Constraint;
9+
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotationInterface;
10+
use function is_array;
11+
use function ltrim;
12+
13+
/**
14+
* Use this annotation to validate a parameter for a query or mutation.
15+
*
16+
* @Annotation
17+
* @Target({"METHOD"})
18+
* @Attributes({
19+
* @Attribute("for", type = "string"),
20+
* @Attribute("constraint", type = "Symfony\Component\Validator\Constraint[]|Symfony\Component\Validator\Constraint")
21+
* })
22+
*/
23+
class Assert implements ParameterAnnotationInterface
24+
{
25+
/** @var string */
26+
private $for;
27+
/** @var Constraint[] */
28+
private $constraint;
29+
30+
/**
31+
* @param array<string, mixed> $values
32+
*/
33+
public function __construct(array $values)
34+
{
35+
if (! isset($values['for'])) {
36+
throw new BadMethodCallException('The @Assert annotation must be passed a target. For instance: "@Assert(for="$email", constraint=@Email)"');
37+
}
38+
if (! isset($values['constraint'])) {
39+
throw new BadMethodCallException('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"');
40+
}
41+
$this->for = ltrim($values['for'], '$');
42+
$this->constraint = is_array($values['constraint']) ? $values['constraint'] : [ $values['constraint'] ];
43+
}
44+
45+
public function getTarget(): string
46+
{
47+
return $this->for;
48+
}
49+
50+
/**
51+
* @return Constraint[]
52+
*/
53+
public function getConstraint(): array
54+
{
55+
return $this->constraint;
56+
}
57+
}

src/ConstraintViolationException.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\Graphqlite\Validator;
6+
7+
use Exception;
8+
use Symfony\Component\Validator\ConstraintViolationInterface;
9+
use TheCodingMachine\GraphQLite\Exceptions\GraphQLExceptionInterface;
10+
11+
class ConstraintViolationException extends Exception implements GraphQLExceptionInterface
12+
{
13+
/** @var ConstraintViolationInterface */
14+
private $violation;
15+
16+
public function __construct(ConstraintViolationInterface $violation)
17+
{
18+
parent::__construct($violation->getMessage(), 400);
19+
$this->violation = $violation;
20+
}
21+
22+
/**
23+
* Returns true when exception message is safe to be displayed to a client.
24+
*/
25+
public function isClientSafe(): bool
26+
{
27+
return true;
28+
}
29+
30+
/**
31+
* Returns string describing a category of the error.
32+
*
33+
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
34+
*/
35+
public function getCategory(): string
36+
{
37+
return 'Validate';
38+
}
39+
40+
/**
41+
* Returns the "extensions" object attached to the GraphQL error.
42+
*
43+
* @return array<string, mixed>
44+
*/
45+
public function getExtensions(): array
46+
{
47+
$extensions = [];
48+
$code = $this->violation->getCode();
49+
if ($code !== null) {
50+
$extensions['code'] = $code;
51+
}
52+
$propertyPath = $this->violation->getPropertyPath();
53+
if ($propertyPath !== null) {
54+
$extensions['field'] = $propertyPath;
55+
}
56+
57+
return $extensions;
58+
}
59+
}

0 commit comments

Comments
 (0)