src/Graviton/DocumentBundle/Serializer/Handler/ExtReferenceHandler.php
public function validateExtRef(ConstraintEventFormat $event)
    {
        $schema = $event->getSchema();

        if (!isset($schema->format) || (isset($schema->format) && $schema->format != 'extref')) {
            return;
        }

        $value = $event->getElement();

        // 1st) can it be converted to extref?
        try {
            $this->converter->getExtReference(
                $value
            );
        } catch (\InvalidArgumentException $e) {
            $event->addError(sprintf('Value "%s" is not a valid extref.', $value));
            return;
        }

        // 2nd) if yes, correct collection(s)?
        if (!isset($schema->{'x-collection'})) {
            return;
        }

        $collections = $schema->{'x-collection'};

        if (in_array('*', $collections)) {
            return;
        }

        $allValues = implode('-', $collections);
        if (!isset($this->extRefPatternCache[$allValues])) {
            $paths = [];
            foreach ($collections as $url) {
                $urlParts = parse_url($url);
                $paths[] = str_replace('/', '\\/', $urlParts['path']);
            }
            $this->extRefPatternCache[$allValues] = '(' . implode('|', $paths) . ')(' . ActionUtils::ID_PATTERN . ')$';
        }

        $stringConstraint = $event->getFactory()->createInstanceFor('string');
        $schema->format = null;
        $schema->pattern = $this->extRefPatternCache[$allValues];
        $stringConstraint->check($value, $schema, $event->getPath());

        if (!empty($stringConstraint->getErrors())) {
            $event->addError(sprintf('Value "%s" does not refer to a correct collection for this extref.', $value));
        }
    }
public function validateExtRef(ConstraintEventFormat $event)
    {
        $schema = $event->getSchema();

        if (!isset($schema->format) || (isset($schema->format) && $schema->format != 'extref')) {
            return;
        }

        $value = $event->getElement();

        // 1st) can it be converted to extref?
        try {
            $this->converter->getExtReference(
                $value
            );
        } catch (\InvalidArgumentException $e) {
            $event->addError(sprintf('Value "%s" is not a valid extref.', $value));
            return;
        }

        // 2nd) if yes, correct collection(s)?
        if (!isset($schema->{'x-collection'})) {
            return;
        }

        $collections = $schema->{'x-collection'};

        if (in_array('*', $collections)) {
            return;
        }

        $allValues = implode('-', $collections);
        if (!isset($this->extRefPatternCache[$allValues])) {
            $paths = [];
            foreach ($collections as $url) {
                $urlParts = parse_url($url);
                $paths[] = str_replace('/', '\\/', $urlParts['path']);
            }
            $this->extRefPatternCache[$allValues] = '(' . implode('|', $paths) . ')(' . ActionUtils::ID_PATTERN . ')$';
        }

        $stringConstraint = $event->getFactory()->createInstanceFor('string');
        $schema->format = null;
        $schema->pattern = $this->extRefPatternCache[$allValues];
        $stringConstraint->check($value, $schema, $event->getPath());

        if (!empty($stringConstraint->getErrors())) {
            $event->addError(sprintf('Value "%s" does not refer to a correct collection for this extref.', $value));
        }
    }
public function validateExtRef ( ConstraintEventFormat $ event ) { $ schema = $ event -> getSchema ( ) ; if ( ! isset ( $ schema -> format ) || ( isset ( $ schema -> format ) && $ schema -> format != 'extref' ) ) { return ; } $ value = $ event -> getElement ( ) ; // 1st) can it be converted to extref? try { $ this -> converter -> getExtReference ( $ value ) ; } catch ( \ InvalidArgumentException $ e ) { $ event -> addError ( sprintf ( 'Value "%s" is not a valid extref.' , $ value ) ) ; return ; } // 2nd) if yes, correct collection(s)? if ( ! isset ( $ schema -> { 'x-collection' } ) ) { return ; } $ collections = $ schema -> { 'x-collection' } ; if ( in_array ( '*' , $ collections ) ) { return ; } $ allValues = implode ( '-' , $ collections ) ; if ( ! isset ( $ this -> extRefPatternCache [ $ allValues ] ) ) { $ paths = [ ] ; foreach ( $ collections as $ url ) { $ urlParts = parse_url ( $ url ) ; $ paths [ ] = str_replace ( '/' , '\\/' , $ urlParts [ 'path' ] ) ; } $ this -> extRefPatternCache [ $ allValues ] = '(' . implode ( '|' , $ paths ) . ')(' . ActionUtils :: ID_PATTERN . ')$' ; } $ stringConstraint = $ event -> getFactory ( ) -> createInstanceFor ( 'string' ) ; $ schema -> format = null ; $ schema -> pattern = $ this -> extRefPatternCache [ $ allValues ] ; $ stringConstraint -> check ( $ value , $ schema , $ event -> getPath ( ) ) ; if ( ! empty ( $ stringConstraint -> getErrors ( ) ) ) { $ event -> addError ( sprintf ( 'Value "%s" does not refer to a correct collection for this extref.' , $ value ) ) ; } }
Schema validation function for extref values. Will be executed when a user submits an extref

@param ConstraintEventFormat $event event

@return void
Schema validation function for extref values . Will be executed when a user submits an extref