Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StringConstraint.php

class StringConstraint

The StringConstraint Constraints, validates an string against a given schema

@author Robert Schönthal <seroscho@googlemail.com> @author Bruno Prieto Reis <bruno.p.reis@gmail.com>

Hierarchy

  • class \JsonSchema\Constraints\BaseConstraint
    • class \JsonSchema\Constraints\Constraint extends \JsonSchema\Constraints\BaseConstraint implements \JsonSchema\Constraints\ConstraintInterface
      • class \JsonSchema\Constraints\StringConstraint extends \JsonSchema\Constraints\Constraint

Expanded class hierarchy of StringConstraint

File

vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php, line 20

Namespace

JsonSchema\Constraints
View source
class StringConstraint extends Constraint {
    
    /**
     * {@inheritdoc}
     */
    public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) {
        // Verify maxLength
        if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) {
            $this->addError($path, 'Must be at most ' . $schema->maxLength . ' characters long', 'maxLength', array(
                'maxLength' => $schema->maxLength,
            ));
        }
        
        //verify minLength
        if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) {
            $this->addError($path, 'Must be at least ' . $schema->minLength . ' characters long', 'minLength', array(
                'minLength' => $schema->minLength,
            ));
        }
        // Verify a regex pattern
        if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#u', $element)) {
            $this->addError($path, 'Does not match the regex pattern ' . $schema->pattern, 'pattern', array(
                'pattern' => $schema->pattern,
            ));
        }
        $this->checkFormat($element, $schema, $path, $i);
    }
    private function strlen($string) {
        if (extension_loaded('mbstring')) {
            return mb_strlen($string, mb_detect_encoding($string));
        }
        // mbstring is present on all test platforms, so strlen() can be ignored for coverage
        return strlen($string);
        // @codeCoverageIgnore
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaseConstraint::$errorMask protected property
BaseConstraint::$errors protected property
BaseConstraint::$factory protected property
BaseConstraint::addError public function
BaseConstraint::addErrors public function
BaseConstraint::arrayToObjectRecursive public static function Recursively cast an associative array to an object
BaseConstraint::getErrorMask public function Get the error mask
BaseConstraint::getErrors public function
BaseConstraint::isValid public function
BaseConstraint::numErrors public function
BaseConstraint::reset public function Clears any reported errors. Should be used between
multiple validation checks.
BaseConstraint::__construct public function
Constraint::$inlineSchemaProperty protected property
Constraint::checkArray protected function Validates an array
Constraint::checkEnum protected function Checks a enum element
Constraint::checkFormat protected function Checks format of an element
Constraint::checkNumber protected function Checks a number element
Constraint::checkObject protected function Validates an object
Constraint::checkString protected function Checks a string element
Constraint::checkType protected function Validates the type of a property
Constraint::checkUndefined protected function Checks a undefined element
Constraint::CHECK_MODE_APPLY_DEFAULTS constant
Constraint::CHECK_MODE_COERCE_TYPES constant
Constraint::CHECK_MODE_DISABLE_FORMAT constant
Constraint::CHECK_MODE_EXCEPTIONS constant
Constraint::CHECK_MODE_NONE constant
Constraint::CHECK_MODE_NORMAL constant
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS constant
Constraint::CHECK_MODE_TYPE_CAST constant
Constraint::CHECK_MODE_VALIDATE_SCHEMA constant
Constraint::convertJsonPointerIntoPropertyPath protected function
Constraint::getTypeCheck protected function Get the type check based on the set check mode.
Constraint::incrementPath protected function Bubble down the path
StringConstraint::check public function invokes the validation of an element Overrides ConstraintInterface::check
StringConstraint::strlen private function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal