validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\crop\Plugin\Validation\Constraint;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * Checks if the crop type is valid.
10
 */
11
class CropTypeMachineNameValidationConstraintValidator extends ConstraintValidator {
12
13
  /**
14
   * Validator 2.5 and upwards compatible execution context.
15
   *
16
   * @var \Symfony\Component\Validator\Context\ExecutionContextInterface
17
   */
18
  protected $context;
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function validate($value, Constraint $constraint) {
24
    // '0' is invalid, since elsewhere we check it using empty().
25
    /** @var \Drupal\crop\Entity\CropType $value */
26
    if (trim($value->id()) == '0') {
27
      $this->context->buildViolation($constraint->message)
28
        ->atPath('id')
29
        ->addViolation();
30
    }
31
  }
32
33
}
34