You are here

class GeoConstraintValidator in Geofield 8

Validates the GeoType constraint.

Hierarchy

Expanded class hierarchy of GeoConstraintValidator

1 file declares its use of GeoConstraintValidator
ConstraintsTest.php in tests/src/Kernel/ConstraintsTest.php

File

src/Plugin/Validation/Constraint/GeoConstraintValidator.php, line 14

Namespace

Drupal\geofield\Plugin\Validation\Constraint
View source
class GeoConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The geoPhpWrapper service.
   *
   * @var \Drupal\geofield\GeoPHP\GeoPHPInterface
   */
  protected $geoPhpWrapper;

  /**
   * Constructs a new GeoConstraintValidator object.
   *
   * @param \Drupal\geofield\GeoPHP\GeoPHPInterface $geophp_wrapper
   *   The geoPhpWrapper.
   */
  public function __construct(GeoPHPInterface $geophp_wrapper) {
    $this->geoPhpWrapper = $geophp_wrapper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('geofield.geophp'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (isset($value)) {
      $valid_geometry = TRUE;
      try {
        if (!$this->geoPhpWrapper
          ->load($value)) {
          $valid_geometry = FALSE;
        }
      } catch (\Exception $e) {
        $valid_geometry = FALSE;
      }
      if (!$valid_geometry) {
        $this->context
          ->addViolation($constraint->message, [
          '@value' => $value,
        ]);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeoConstraintValidator::$geoPhpWrapper protected property The geoPhpWrapper service.
GeoConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
GeoConstraintValidator::validate public function Checks if the passed value is valid.
GeoConstraintValidator::__construct public function Constructs a new GeoConstraintValidator object.