Skip to content

Commit 1530498

Browse files
committed
Added a TransitionsCollectionBuilder
1 parent 69aff45 commit 1530498

File tree

2 files changed

+101
-17
lines changed

2 files changed

+101
-17
lines changed

src/Symfony/Component/Workflow/DefinitionBuilder.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class DefinitionBuilder
2424
{
2525
private $places = array();
26-
private $transitions = array();
26+
private $transitionsCollection;
2727
private $initialPlace;
2828

2929
/**
@@ -35,15 +35,16 @@ class DefinitionBuilder
3535
public function __construct(array $places = array(), array $transitions = array())
3636
{
3737
$this->addPlaces($places);
38-
$this->addTransitions($transitions);
38+
$this->transitionsCollection = new TransitionsCollectionBuilder();
39+
$this->transitionsCollection->addTransitions($transitions);
3940
}
4041

4142
/**
4243
* @return Definition
4344
*/
4445
public function build()
4546
{
46-
return new Definition($this->places, $this->transitions, $this->initialPlace);
47+
return new Definition($this->places, $this->transitionsCollection->getTransitions(), $this->initialPlace);
4748
}
4849

4950
/**
@@ -52,7 +53,7 @@ public function build()
5253
public function reset()
5354
{
5455
$this->places = array();
55-
$this->transitions = array();
56+
$this->transitionsCollection->reset();
5657
$this->initialPlace = null;
5758
}
5859

@@ -88,14 +89,7 @@ public function addPlaces(array $places)
8889
*/
8990
public function addTransitions(array $transitions)
9091
{
91-
foreach ($transitions as $transition) {
92-
if ($transition instanceof Transition) {
93-
$this->addTransition($transition);
94-
} else {
95-
list($name, $froms, $tos) = $transition;
96-
$this->addTransition($name, $froms, $tos);
97-
}
98-
}
92+
$this->transitionsCollection->addTransitions($transitions);
9993
}
10094

10195
/**
@@ -105,10 +99,6 @@ public function addTransitions(array $transitions)
10599
*/
106100
public function addTransition($transition, $froms = null, $tos = null)
107101
{
108-
if ($transition instanceof Transition) {
109-
$this->transitions[] = $transition;
110-
} else {
111-
$this->transitions[] = new Transition($transition, $froms, $tos);
112-
}
102+
$this->transitionsCollection->addTransition($transition, $froms, $tos);
113103
}
114104
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow;
13+
14+
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
15+
use Symfony\Component\Workflow\Exception\LogicException;
16+
17+
/**
18+
* TransitionsCollectionBuilder.
19+
*
20+
* @author Jules Pietri <jules@heahprod.com>
21+
*/
22+
class TransitionsCollectionBuilder
23+
{
24+
/**
25+
* @var Transition[]
26+
*/
27+
private $transitions = array();
28+
29+
/**
30+
* @param Transition|string $transition
31+
* @param string[]string|null $froms
32+
* @param string[]string|null $tos
33+
*/
34+
public function addTransition($transition, $froms = null, $tos = null)
35+
{
36+
if ($transition instanceof Transition) {
37+
$this->add($transition);
38+
39+
return;
40+
}
41+
42+
$this->createTransition($transition, $froms, $tos);
43+
}
44+
45+
/**
46+
* @param array[] $transitions An array of arrays of three arguments to pass to "addTransition"
47+
*/
48+
public function addTransitions(array $transitions)
49+
{
50+
foreach ($transitions as $transition) {
51+
if ($transition instanceof Transition) {
52+
$this->add($transition);
53+
54+
continue;
55+
}
56+
57+
if (!is_array($transition) || 3 !== count($transition)) {
58+
throw new InvalidArgumentException(sprintf(
59+
'Calling "%s" expected each entry to be an instance of "%s" or an array of three arguments to create a transition instance%s: "name", "froms", and "tos", but got %s.',
60+
__METHOD__,
61+
Transition::class,
62+
isset($transition[0]) ? ' named "'.$transition[0].'"' : '',
63+
is_array($transition) ? ' an array with '.count($transition).' entries' : gettype($transition)));
64+
}
65+
list($name, $froms, $tos) = $transition;
66+
$this->addTransition($name, $froms, $tos);
67+
}
68+
}
69+
70+
public function getTransitions()
71+
{
72+
return $this->transitions;
73+
}
74+
75+
public function reset()
76+
{
77+
return $this->transitions = array();
78+
}
79+
80+
private function createTransition($name, $froms, $tos)
81+
{
82+
$this->add(new Transition($name, $froms, $tos));
83+
84+
}
85+
86+
private function add(Transition $newTransition) {
87+
foreach ($this->transitions as $transition) {
88+
if ($newTransition == $transition) {
89+
throw new InvalidArgumentException(sprintf('The transition named "%s" has already been added.', $transition->getName()));
90+
}
91+
}
92+
$this->transitions[] = $newTransition;
93+
}
94+
}

0 commit comments

Comments
 (0)