8000 [Workflow] Added 'workflow_marked_places' twig function by lyrixx · Pull Request #22180 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added a `workflow_has_marked_place` function
* added a `workflow_marked_places` function

3.2.0
-----
Expand Down
34 changes: 32 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getFunctions()
new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
new \Twig_SimpleFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')),
new \Twig_SimpleFunction('workflow_marked_places', array($this, 'getMarkedPlaces')),
);
}

Expand Down Expand Up @@ -63,9 +64,38 @@ public function getEnabledTransitions($subject, $name = null)
return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
}

public function hasMarkedPlace($object, $place, $name = null)
/**
* Returns true if the place is marked.
*
* @param object $subject A subject
* @param string $placeName A place name
* @param string $name A workflow name
*
* @return bool true if the transition is enabled
*/
public function hasMarkedPlace($subject, $placeName, $name = null)
{
return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($placeName);
}

/**
* Returns marked places.
*
* @param object $subject A subject
* @param string $placesNameOnly If true, returns only places name. If false returns the raw representation
* @param string $name A workflow name
*
* @return string[]|int[]
*/
public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
{
return $this->workflowRegistry->get($object, $name)->getMarking($object)->has($place);
$places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();

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

return $places;
}

public function getName()
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ public function testHasMarkedPlace()
$this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment'));
$this->assertFalse($this->extension->hasMarkedPlace($subject, 'processed'));
}

public function testGetMarkedPlaces()
{
$subject = new \stdClass();
$subject->marking = array();
$subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1);

$this->assertSame(array('ordered', 'waiting_for_payment'), $this->extension->getMarkedPlaces($subject));
$this->assertSame($subject->marking, $this->extension->getMarkedPlaces($subject, false));
}
}
0