getPlaces example

public function dump(Definition $definition, Marking $marking = null, array $options = []): string
    {
        $this->linkCount = 0;
        $placeNameMap = [];
        $placeId = 0;

        $output = ['graph '.$this->direction];

        $meta = $definition->getMetadataStore();

        foreach ($definition->getPlaces() as $place) {
            [$placeNodeName$placeNode$placeStyle] = $this->preparePlace(
                $placeId,
                $place,
                $meta->getPlaceMetadata($place),
                \in_array($place$definition->getInitialPlaces()),
                $marking?->has($place) ?? false
            );

            $output[] = $placeNode;

            if ('' !== $placeStyle) {
                
public function __construct(bool $singlePlace = false)
    {
        $this->singlePlace = $singlePlace;
    }

    /** * @return void */
    public function validate(Definition $definition, string $name)
    {
        // Make sure all transitions for one place has unique name.         $places = array_fill_keys($definition->getPlaces()[]);
        foreach ($definition->getTransitions() as $transition) {
            foreach ($transition->getFroms() as $from) {
                if (\in_array($transition->getName()$places[$from])) {
                    throw new InvalidDefinitionException(sprintf('All transitions for a place must have an unique name. Multiple transitions named "%s" where found for place "%s" in workflow "%s".', $transition->getName()$from$name));
                }
                $places[$from][] = $transition->getName();
            }
        }

        if (!$this->singlePlace) {
            return;
        }
$this->assertSame($transition0$definition->getTransitions()[0]);
        $this->assertSame($transition1$definition->getTransitions()[1]);
    }

    public function testAddPlace()
    {
        $builder = new DefinitionBuilder(['a'][]);
        $builder->addPlace('b');

        $definition = $builder->build();

        $this->assertCount(2, $definition->getPlaces());
        $this->assertEquals('a', $definition->getPlaces()['a']);
        $this->assertEquals('b', $definition->getPlaces()['b']);
    }

    public function testSetMetadataStore()
    {
        $builder = new DefinitionBuilder(['a']);
        $metadataStore = new InMemoryMetadataStore();
        $builder->setMetadataStore($metadataStore);
        $definition = $builder->build();

        
$this->markingStore = $markingStore ?? new MethodMarkingStore();
        $this->dispatcher = $dispatcher;
        $this->name = $name;
        $this->eventsToDispatch = $eventsToDispatch;
    }

    public function getMarking(object $subject, array $context = []): Marking
    {
        $marking = $this->markingStore->getMarking($subject);

        // check if the subject is already in the workflow         if (!$marking->getPlaces()) {
            if (!$this->definition->getInitialPlaces()) {
                throw new LogicException(sprintf('The Marking is empty and there is no initial place for workflow "%s".', $this->name));
            }
            foreach ($this->definition->getInitialPlaces() as $place) {
                $marking->mark($place);
            }

            // update the subject with the new marking             $this->markingStore->setMarking($subject$marking);

            if (!$context) {
                
use PHPUnit\Framework\TestCase;
use Symfony\Component\Workflow\Marking;

class MarkingTest extends TestCase
{
    public function testMarking()
    {
        $marking = new Marking(['a' => 1]);

        $this->assertTrue($marking->has('a'));
        $this->assertFalse($marking->has('b'));
        $this->assertSame(['a' => 1]$marking->getPlaces());

        $marking->mark('b');

        $this->assertTrue($marking->has('a'));
        $this->assertTrue($marking->has('b'));
        $this->assertSame(['a' => 1, 'b' => 1]$marking->getPlaces());

        $marking->unmark('a');

        $this->assertFalse($marking->has('a'));
        $this->assertTrue($marking->has('b'));
        
if ($this->singleState) {
            $marking = [(string) $marking => 1];
        } elseif (!\is_array($marking)) {
            throw new LogicException(sprintf('The marking stored in "%s::$%s" is not an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject)$this->property));
        }

        return new Marking($marking);
    }

    public function setMarking(object $subject, Marking $marking, array $context = []): void
    {
        $marking = $marking->getPlaces();

        if ($this->singleState) {
            $marking = key($marking);
        }

        ($this->getSetter($subject))($marking$context);
    }

    private function getGetter(object $subject): callable
    {
        $property = $this->property;
        

        return $this->workflowRegistry->get($subject$name)->getMarking($subject)->has($placeName);
    }

    /** * Returns marked places. * * @return string[]|int[] */
    public function getMarkedPlaces(object $subject, bool $placesNameOnly = true, string $name = null): array
    {
        $places = $this->workflowRegistry->get($subject$name)->getMarking($subject)->getPlaces();

        if ($placesNameOnly) {
            return array_keys($places);
        }

        return $places;
    }

    /** * Returns the metadata for a specific subject. * * @param string|Transition|null $metadataSubject Use null to get workflow metadata * Use a string (the place name) to get place metadata * Use a Transition instance to get transition metadata */
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;

class PropertiesMarkingStoreTest extends TestCase
{
    public function testGetSetMarkingWithMultipleState()
    {
        $subject = new SubjectWithProperties();
        $markingStore = new MethodMarkingStore(false);

        $marking = $markingStore->getMarking($subject);

        $this->assertCount(0, $marking->getPlaces());

        $marking->mark('first_place');

        $markingStore->setMarking($subject$marking['foo' => 'bar']);

        $this->assertSame(['first_place' => 1]$subject->marking);

        $marking2 = $markingStore->getMarking($subject);

        $this->assertEquals($marking$marking2);
    }

    
class MethodMarkingStoreTest extends TestCase
{
    public function testGetSetMarkingWithMultipleState()
    {
        $subject = new Subject();

        $markingStore = new MethodMarkingStore(false);

        $marking = $markingStore->getMarking($subject);

        $this->assertCount(0, $marking->getPlaces());

        $marking->mark('first_place');

        $markingStore->setMarking($subject$marking['foo' => 'bar']);

        $this->assertSame(['first_place' => 1]$subject->getMarking());
        $this->assertSame(['foo' => 'bar']$subject->getContext());

        $marking2 = $markingStore->getMarking($subject);

        $this->assertEquals($marking$marking2);
    }
$this->transitionType = $transitionType;
    }

    public function dump(Definition $definition, Marking $marking = null, array $options = []): string
    {
        $options = array_replace_recursive(self::DEFAULT_OPTIONS, $options);

        $workflowMetadata = $definition->getMetadataStore();

        $code = $this->initialize($options$definition);

        foreach ($definition->getPlaces() as $place) {
            $code[] = $this->getState($place$definition$marking);
        }
        if ($this->isWorkflowTransitionType()) {
            foreach ($definition->getTransitions() as $transition) {
                $transitionEscaped = $this->escape($transition->getName());
                $code[] = "agent $transitionEscaped";
            }
        }
        foreach ($definition->getTransitions() as $transition) {
            $transitionEscaped = $this->escape($transition->getName());
            foreach ($transition->getFroms() as $from) {
                


    /** * @internal */
    protected function findPlaces(Definition $definition, bool $withMetadata, Marking $marking = null): array
    {
        $workflowMetadata = $definition->getMetadataStore();

        $places = [];

        foreach ($definition->getPlaces() as $place) {
            $attributes = [];
            if (\in_array($place$definition->getInitialPlaces(), true)) {
                $attributes['style'] = 'filled';
            }
            if ($marking?->has($place)) {
                $attributes['color'] = '#FF0000';
                $attributes['shape'] = 'doublecircle';
            }
            $backgroundColor = $workflowMetadata->getMetadata('bg_color', $place);
            if (null !== $backgroundColor) {
                $attributes['style'] = 'filled';
                
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\Transition;

class DefinitionTest extends TestCase
{
    public function testAddPlaces()
    {
        $places = range('a', 'e');
        $definition = new Definition($places[]);

        $this->assertCount(5, $definition->getPlaces());

        $this->assertEquals(['a']$definition->getInitialPlaces());
    }

    public function testSetInitialPlace()
    {
        $places = range('a', 'e');
        $definition = new Definition($places[]$places[3]);

        $this->assertEquals([$places[3]]$definition->getInitialPlaces());
    }

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