本文整理汇总了PHP中Symfony\Component\Validator\Constraint类的典型用法代码示例。如果您正苦于以下问题:PHP Constraint类的具体用法?PHP Constraint怎么用?PHP Constraint使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Constraint类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* Checks if current workflow item allows transition
*
* @param WorkflowData $value
* @param TransitionIsAllowed $constraint
*/
public function validate($value, Constraint $constraint)
{
/** @var WorkflowItem $workflowItem */
$workflowItem = $constraint->getWorkflowItem();
$transitionName = $constraint->getTransitionName();
$workflow = $this->registry->getWorkflow($workflowItem->getWorkflowName());
$errors = new ArrayCollection();
$result = false;
try {
$result = $workflow->isTransitionAllowed($workflowItem, $transitionName, $errors, true);
} catch (InvalidTransitionException $e) {
switch ($e->getCode()) {
case InvalidTransitionException::UNKNOWN_TRANSITION:
$errors->add(array('message' => $constraint->unknownTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName)));
break;
case InvalidTransitionException::NOT_START_TRANSITION:
$errors->add(array('message' => $constraint->notStartTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName)));
break;
case InvalidTransitionException::STEP_HAS_NO_ALLOWED_TRANSITION:
$errors->add(array('message' => $constraint->stepHasNotAllowedTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName, '{{ step }}' => $workflowItem->getCurrentStep()->getName())));
break;
}
}
if (!$result) {
if ($errors->count()) {
foreach ($errors as $error) {
$this->context->addViolation($error['message'], $error['parameters']);
}
} else {
$this->context->addViolation($constraint->someConditionsNotMetMessage);
}
}
}
示例2: validate
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof TokenEntity) {
throw new \InvalidArgumentException('Given constraint must ne instance of TokenEntity class');
}
if ($value) {
$configValidation = false;
try {
/** @var StorageApi $storageApi */
$storageApi = $constraint->getStorageApi();
$token = $storageApi->getToken($value);
if (!$token) {
throw new \Exception('Token does not exists');
}
$userToken = new Token($storageApi);
$configValidation = true;
$components = new Components(new StorageApi(array('token' => $token['token'], 'url' => $storageApi->getApiUrl(), 'userAgent' => $storageApi->getUserAgent())));
$components->listComponents(new ListConfigurationsOptions());
} catch (\Exception $e) {
if ($e instanceof \Keboola\StorageApi\ClientException && $e->getCode() === 403) {
//@FIXME jak zmenit api exception code
if ($configValidation) {
$this->context->addViolation($constraint->configMessage, array('%string%' => $value), null, null, 'TOKEN_PERMISSION');
} else {
$this->context->addViolation($constraint->permissionsMessage, array(), null, null, 'TOKEN_PERMISSION');
}
} else {
$this->context->addViolation($constraint->message, array('%string%' => $value));
}
return;
}
//@FIXME doplnit validaci na master token!!!
}
}
示例3: validate
/**
* {@inheritdoc}
*
* @throws UnexpectedTypeException When the constraint isn't a subclass of {@see AbstractAssertionConstraint}
* @throws ConstraintDefinitionException When the assertion doesn't exist on the assertion class
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof AbstractAssertionConstraint) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\AbstractAssertionConstraint');
}
$methodName = $constraint->getAssertionMethodName();
$callable = [static::$assertClass, $methodName];
$override = null;
if (is_callable($callable) === false) {
throw new ConstraintDefinitionException('Must be a valid callable on the assertion class');
}
$parameters = array($value);
foreach ($constraint->getAssertionParameterNames() as $name) {
$parameters[] = $constraint->{$name};
}
try {
call_user_func_array($callable, $parameters);
} catch (\Exception $e) {
if ($e instanceof static::$assertExceptionClass) {
$this->context->buildViolation($e->getMessage())->setParameter('{{ value }}', $this->formatValue($value))->setCode($e->getCode())->setCause($e)->addViolation();
} else {
throw $e;
}
}
}
示例4: getInstance
/**
* Returns the validator for the supplied constraint.
*
* @param Constraint $constraint A constraint
*
* @return ConstraintValidatorInterface A validator for the supplied constraint
*
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();
if (!isset($this->validators[$name])) {
switch (get_class($constraint)) {
case self::BASE_NAMESPACE . '\\All':
$name = self::BASE_NAMESPACE . '\\LegacyAllValidator';
break;
case self::BASE_NAMESPACE . '\\Choice':
$name = self::BASE_NAMESPACE . '\\LegacyChoiceValidator';
break;
case self::BASE_NAMESPACE . '\\Collection':
$name = self::BASE_NAMESPACE . '\\LegacyCollectionValidator';
break;
case self::BASE_NAMESPACE . '\\Count':
$name = self::BASE_NAMESPACE . '\\LegacyCountValidator';
break;
case self::BASE_NAMESPACE . '\\Length':
$name = self::BASE_NAMESPACE . '\\LegacyLengthValidator';
break;
case self::FORM_BASE_NAMESPACE . '\\Form':
$name = self::FORM_BASE_NAMESPACE . '\\LegacyFormValidator';
break;
}
$this->validators[$name] = new $name();
} elseif (is_string($this->validators[$name])) {
$this->validators[$name] = $this->container->get($this->validators[$name]);
}
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
throw new UnexpectedTypeException($this->validators[$name], 'Symfony\\Component\\Validator\\ConstraintValidatorInterface');
}
return $this->validators[$name];
}
示例5: validate
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
* @return bool
*/
public function validate($value, Constraint $constraint)
{
/** @var EntityExists $constraint */
if ($constraint->validate($value)) {
$this->context->addViolation($constraint->message, ['{{ type }}' => $constraint->entityClass, '{{ value }}' => $value]);
}
}
示例6: validate
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof OrchestrationTableEntity) {
throw new \InvalidArgumentException('Given constraint must ne instance of OrchestrationTableEntity class');
}
if ($value) {
// table exists
try {
/** @var StorageApi $storageApi */
$storageApi = $constraint->getStorageApi();
if (!$storageApi->tableExists($value)) {
throw new \Exception('Configuration table does not exists');
}
} catch (\Exception $e) {
$this->context->addViolation($constraint->message, array('%string%' => $value));
return;
}
// table structure
try {
/** @var StorageApi $storageApi */
$storageApi = $constraint->getStorageApi();
$table = $storageApi->getTable($value);
$token = new Token($storageApi);
$requiredColumns = self::getRequiredColumns();
$missingColumns = array_diff($requiredColumns, $table['columns']);
if (!empty($missingColumns)) {
throw new \Exception('Invalid configuration table structure');
}
} catch (\Exception $e) {
$this->context->addViolation($constraint->structureMessage, array('%string%' => implode(', ', $requiredColumns)));
return;
}
}
}
示例7: getInstance
/**
* {@inheritDoc}
*/
public function getInstance(Constraint $constraint)
{
$className = $constraint->validatedBy();
if (!isset($this->validators[$className]) || $className === 'Symfony\\Component\\Validator\\Constraints\\CollectionValidator') {
$this->validators[$className] = new $className();
}
return $this->validators[$className];
}
示例8: getInstance
/**
* {@inheritdoc}
*/
public function getInstance(Constraint $constraint)
{
$class_name = $constraint->validatedBy();
if (!isset($this->validators[$class_name])) {
$this->validators[$class_name] = $this->classResolver->getInstanceFromDefinition($class_name);
}
return $this->validators[$class_name];
}
示例9: getInstance
/**
* {@inheritdoc}
*/
public function getInstance(Constraint $constraint)
{
$className = $constraint->validatedBy();
if (!isset($this->validators[$className])) {
$this->validators[$className] = 'validator.expression' === $className ? new ExpressionValidator($this->propertyAccessor) : new $className();
}
return $this->validators[$className];
}
示例10: getInstance
/**
* @param Constraint $constraint
* @return \Symfony\Component\Validator\ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
{
$class_name = $constraint->validatedBy();
if (!isset($this->validators[$class_name])) {
$this->validators[$class_name] = new $class_name();
}
return $this->validators[$class_name];
}
示例11: addConstraint
/**
* {@inheritdoc}
*/
public function addConstraint(Constraint $constraint)
{
if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', get_class($constraint)));
}
parent::addConstraint($constraint);
return $this;
}
示例12: getInstance
/**
* {@inheritdoc}
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();
if (isset($this->serviceNames[$name])) {
return $this->container[$this->serviceNames[$name]];
}
return parent::getInstance($constraint);
}
示例13:
function it_doesnt_support_multi_targets_constraint($guesser, $factory, ClassMetadata $metadata, ProductValueInterface $value, AttributeInterface $attribute, Constraint $multiTargets, Constraint $validNumber)
{
$factory->createMetadata(Argument::any())->willReturn($metadata);
$value->getAttribute()->willReturn($attribute);
$attribute->getBackendType()->willReturn('varchar');
$guesser->guessConstraints($attribute)->willReturn([$multiTargets]);
$multiTargets->getTargets()->willReturn([Constraint::PROPERTY_CONSTRAINT, Constraint::CLASS_CONSTRAINT]);
$this->shouldThrow(new \LogicException('No support provided for constraint on many targets'))->duringGetMetadataFor($value);
}
示例14: getInstance
/**
* Returns the validator for the supplied constraint.
*
* @param Constraint $constraint A constraint
* @return ConstraintValidator A validator for the supplied constraint
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();
if (isset($this->validators[$name])) {
return $this->validators[$name];
}
$this->validators[$name] = $this->createValidator($name);
return $this->validators[$name];
}
示例15: validate
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint)
{
if (!isset($entity)) {
return;
}
if (!in_array($entity->bundle(), $constraint->getBundleOption())) {
$this->context->addViolation($constraint->message, array('%bundle' => implode(', ', $constraint->getBundleOption())));
}
}