getTraversalStrategy example

$propertyMetadata = $classMetadata->getPropertyMetadata($constrainedProperty);
        foreach ($propertyMetadata as $metadata) {
            $autoMapingStrategy = 'Not supported';
            if ($metadata instanceof GenericMetadata) {
                $autoMapingStrategy = match ($metadata->getAutoMappingStrategy()) {
                    AutoMappingStrategy::ENABLED => 'Enabled',
                    AutoMappingStrategy::DISABLED => 'Disabled',
                    AutoMappingStrategy::NONE => 'None',
                };
            }
            $traversalStrategy = 'None';
            if (TraversalStrategy::TRAVERSE === $metadata->getTraversalStrategy()) {
                $traversalStrategy = 'Traverse';
            }
            if (TraversalStrategy::IMPLICIT === $metadata->getTraversalStrategy()) {
                $traversalStrategy = 'Implicit';
            }

            $data[] = [
                'class' => 'property options',
                'groups' => [],
                'options' => [
                    'cascadeStrategy' => CascadingStrategy::CASCADE === $metadata->getCascadingStrategy() ? 'Cascade' : 'None',
                    
$propertyMetadata = $classMetadata->getPropertyMetadata($constrainedProperty);
        foreach ($propertyMetadata as $metadata) {
            $autoMapingStrategy = 'Not supported';
            if ($metadata instanceof GenericMetadata) {
                switch ($metadata->getAutoMappingStrategy()) {
                    case AutoMappingStrategy::ENABLED: $autoMapingStrategy = 'Enabled'; break;
                    case AutoMappingStrategy::DISABLED: $autoMapingStrategy = 'Disabled'; break;
                    case AutoMappingStrategy::NONE: $autoMapingStrategy = 'None'; break;
                }
            }
            $traversalStrategy = 'None';
            if (TraversalStrategy::TRAVERSE === $metadata->getTraversalStrategy()) {
                $traversalStrategy = 'Traverse';
            }
            if (TraversalStrategy::IMPLICIT === $metadata->getTraversalStrategy()) {
                $traversalStrategy = 'Implicit';
            }

            $data[] = [
                'class' => 'property options',
                'groups' => [],
                'options' => [
                    'cascadeStrategy' => CascadingStrategy::CASCADE === $metadata->getCascadingStrategy() ? 'Cascade' : 'None',
                    
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Validator\Mapping\TraversalStrategy;

class TraverseTest extends TestCase
{
    public function testPositiveAttributes()
    {
        $metadata = new ClassMetadata(TraverseDummy::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));
        self::assertSame(TraversalStrategy::TRAVERSE, $metadata->getTraversalStrategy());
    }

    public function testNegativeAttribute()
    {
        $metadata = new ClassMetadata(DoNotTraverseMe::class);
        $loader = new AttributeLoader();
        self::assertTrue($loader->loadClassMetadata($metadata));
        self::assertSame(TraversalStrategy::NONE, $metadata->getTraversalStrategy());
    }
}

$publicParentMaxLengthMetadata = $classMetadata->getPropertyMetadata('publicParentMaxLength');
        $this->assertCount(1, $publicParentMaxLengthMetadata);
        $publicParentMaxLengthConstraints = $publicParentMaxLengthMetadata[0]->getConstraints();
        $this->assertCount(1, $publicParentMaxLengthConstraints);
        $this->assertInstanceOf(Length::class$publicParentMaxLengthConstraints[0]);
        $this->assertSame(35, $publicParentMaxLengthConstraints[0]->max);

        $embeddedMetadata = $classMetadata->getPropertyMetadata('embedded');
        $this->assertCount(1, $embeddedMetadata);
        $this->assertSame(CascadingStrategy::CASCADE, $embeddedMetadata[0]->getCascadingStrategy());
        $this->assertSame(TraversalStrategy::IMPLICIT, $embeddedMetadata[0]->getTraversalStrategy());

        $parentClassMetadata = $validator->getMetadataFor(new DoctrineLoaderParentEntity());

        $privateParentMaxLengthMetadata = $parentClassMetadata->getPropertyMetadata('privateParentMaxLength');
        $this->assertCount(1, $privateParentMaxLengthMetadata);
        $privateParentMaxLengthConstraints = $privateParentMaxLengthMetadata[0]->getConstraints();
        $this->assertCount(1, $privateParentMaxLengthConstraints);
        $this->assertInstanceOf(Length::class$privateParentMaxLengthConstraints[0]);
        $this->assertSame(30, $privateParentMaxLengthConstraints[0]->max);

        $embeddedClassMetadata = $validator->getMetadataFor(new DoctrineLoaderEmbed());

        
$groups,
                    $cascadedGroups,
                    TraversalStrategy::IMPLICIT,
                    $context
                );
            }
        }

        // If no specific traversal strategy was requested when this method         // was called, use the traversal strategy of the class' metadata         if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
            $traversalStrategy = $metadata->getTraversalStrategy();
        }

        // Traverse only if IMPLICIT or TRAVERSE         if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
            return;
        }

        // If IMPLICIT, stop unless we deal with a Traversable         if ($traversalStrategy & TraversalStrategy::IMPLICIT && !$object instanceof \Traversable) {
            return;
        }

        
$groups,
                    $cascadedGroups,
                    TraversalStrategy::IMPLICIT,
                    $context
                );
            }
        }

        // If no specific traversal strategy was requested when this method         // was called, use the traversal strategy of the class' metadata         if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
            $traversalStrategy = $metadata->getTraversalStrategy();
        }

        // Traverse only if IMPLICIT or TRAVERSE         if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
            return;
        }

        // If IMPLICIT, stop unless we deal with a Traversable         if ($traversalStrategy & TraversalStrategy::IMPLICIT && !$object instanceof \Traversable) {
            return;
        }

        
Home | Imprint | This part of the site doesn't use cookies.