Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/relay'
Browse files Browse the repository at this point in the history
  • Loading branch information
viniychuk committed Jun 2, 2016
2 parents 45ed2ba + d3ecfe7 commit c8332f6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Tests/Library/Field/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function testInlineFieldCreation()
}
]);

$this->assertEquals('true', $fieldWithResolve->resolve(true, [], $resolveInfo), 'Resolve bool to string');
$this->assertEquals('CTO', $fieldWithResolve->resolve('CTO', [], $resolveInfo));
$this->assertEquals(null, $fieldWithResolve->resolve(true, [], $resolveInfo), 'Resolve bool to string');

$fieldWithResolve->setType(new IntType());
$this->assertEquals(new IntType(), $fieldWithResolve->getType());
Expand Down
2 changes: 1 addition & 1 deletion Tests/Library/Validator/TypeValidationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TypeValidationRuleTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->rule = new TypeValidationRule(new ConfigValidator());
$this->rule = new TypeValidationRule(ConfigValidator::getInstance());
}


Expand Down
11 changes: 4 additions & 7 deletions src/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidatorInterface;
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
use Youshido\GraphQL\Validator\Exception\ValidationException;

Expand All @@ -32,9 +31,6 @@ abstract class AbstractConfig

protected $extraFieldsAllowed = null;

/** @var ConfigValidatorInterface */
protected $validator;

/**
* TypeConfig constructor.
* @param array $configData
Expand All @@ -53,10 +49,11 @@ public function __construct(array $configData, $contextObject = null, $finalClas
$this->contextObject = $contextObject;
$this->data = $configData;
$this->finalClass = $finalClass;
$this->validator = new ConfigValidator($contextObject);

if (!$this->validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $this->validator->getErrorsArray(false)));
$validator = ConfigValidator::getInstance();

if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false)));
}

$this->build();
Expand Down
13 changes: 10 additions & 3 deletions src/Execution/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ protected function resolveFieldValue(AbstractField $field, $contextValue, Query
{
$resolveInfo = new ResolveInfo($field, $query->getFields(), $field->getType(), $this->executionContext);

return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
if ($resolveFunc = $field->getConfig()->getResolveFunction()) {
return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
} elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) {
return $propertyValue;
} else {
return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
}
}

/**
Expand Down Expand Up @@ -251,9 +257,10 @@ protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, Abstra
$resolved = true;
}

if ($field->getConfig()->getResolveFunction()) {
if ($resolveFunction = $field->getConfig()->getResolveFunction()) {
$resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext);
$resolverValue = $field->resolve($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo);

$resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo);
}

if (!$resolverValue && !$resolved) {
Expand Down
8 changes: 0 additions & 8 deletions src/Field/AbstractField.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ public function setType($type)
*/
public function resolve($value, array $args, ResolveInfo $info)
{
if ($resolveFunc = $this->getConfig()->getResolveFunction()) {
$info->setReturnType($this->getType());

return $resolveFunc($value, $args, $info);
} elseif ($propertyValue = TypeService::getPropertyValue($value, $this->getName())) {
return $propertyValue;
}

return null;
}

Expand Down
48 changes: 24 additions & 24 deletions src/Validator/ConfigValidator/ConfigValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,33 @@ class ConfigValidator implements ConfigValidatorInterface

protected $rules = [];

protected $contextObject;

protected $extraFieldsAllowed = false;

/** @var ValidationRuleInterface[] */
protected $validationRules = [];

public function __construct($contextObject = null)
/** @var ConfigValidator */
protected static $instance;

private function __construct()
{
$this->contextObject = $contextObject;
$this->initializeRules();
}

/**
* @return ConfigValidator
*/
public static function getInstance()
{
if (empty(self::$instance)) {
self::$instance = new self();
}

self::$instance->clearErrors();

return self::$instance;
}

public function validate($data, $rules = [], $extraFieldsAllowed = null)
{
if ($extraFieldsAllowed !== null) $this->setExtraFieldsAllowed($extraFieldsAllowed);
Expand All @@ -47,7 +61,7 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
unset($fieldRules['required']);

if (!array_key_exists($fieldName, $data)) {
$this->addError(new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName() . ' is required'));
$this->addError(new ValidationException(sprintf('Field "%s" is required', $fieldName)));

continue;
}
Expand All @@ -59,25 +73,21 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
/** Validation of all other rules*/
foreach ($fieldRules as $ruleName => $ruleInfo) {
if (!array_key_exists($ruleName, $this->validationRules)) {
$this->addError(new ValidationException('Field \'' . $fieldName . '\' has invalid rule \'' . $ruleInfo . '\''));
$this->addError(new ValidationException(sprintf('Field "%s" has invalid rule "%s"', $fieldName, $ruleInfo)));

continue;
}

if (!$this->validationRules[$ruleName]->validate($data[$fieldName], $ruleInfo)) {
$this->addError(
new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName()
. ' expected to be ' . $ruleName . ': \'' . (string)$ruleInfo . '\', but got: ' . gettype($data[$fieldName])));
$this->addError(new ValidationException(sprintf('Field "%s" expected to be "%s" but got "%s"', $fieldName, $ruleName, gettype($data[$fieldName]))));
}
}
}

if (!$this->isExtraFieldsAllowed()) {
foreach (array_keys($data) as $fieldName) {
if (!in_array($fieldName, $processedFields)) {
$this->addError(
new ValidationException('Field \'' . $fieldName . '\' is not expected in ' . $this->getContextName()));

$this->addError(new ValidationException(sprintf('Field "%s" is not expected', $fieldName)));
}
}
}
Expand All @@ -90,19 +100,9 @@ protected function initializeRules()
$this->validationRules['type'] = new TypeValidationRule($this);
}

/**
* @return string
*/
protected function getContextName()
public function addRule($name, ValidationRuleInterface $rule)
{
if (is_object($this->contextObject)) {
$class = get_class($this->contextObject);
$class = substr($class, strrpos($class, '\\') + 1);

return $class;
} else {
return $this->contextObject ? $this->contextObject : '(context)';
}
$this->validationRules[$name] = $rule;
}

public function isValid()
Expand Down

0 comments on commit c8332f6

Please sign in to comment.