Skip to content

Commit 4814896

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

File tree

2 files changed

+105
-17
lines changed

2 files changed

+105
-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 $transitions;
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->transitions = new TransitionsCollectionBuilder();
39+
$this->transitions->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->transitions->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->transitions = $this->transitions->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->transitions->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->transitions->addTransition($transition, $froms, $tos);
113103
}
114104
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
private $transitions = array();
25+
26+
/**
27+
* @param Transition|string $transition
28+
* @param string[]string $froms
29+
* @param string[]string $tos
30+
*/
31+
public function addTransition($transition, $froms, $tos)
32+
{
33+
if ($transition instanceof Transition) {
34+
$this->add($transition);
35+
36+
return;
37+
}
38+
39+
if (empty($froms)) {
40+
throw new LogicException(sprintf('The transition "%" is missing "froms" place(s).', $transition));
41+
}
42+
if (empty($to)) {
43+
throw new LogicException(sprintf('The transition "%" is missing "tos" place(s).', $transition));
44+
}
45+
46+
$this->createTransition($transition, $froms, $tos);
47+
}
48+
49+
/**
50+
* @param array[] $transitions An array of three arguments to pass to "addTransition"
51+
*/
52+
public function addTransitions(array $transitions)
53+
{
54+
foreach ($transitions as $transition) {
55+
if ($transition instanceof Transition) {
56+
$this->add($transition);
57+
58+
continue;
59+
}
60+
61+
if (!is_array($transition) || 3 === count($transitions)) {
62+
throw new InvalidArgumentException(sprintf(
63+
'Calling "%" 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.',
64+
__METHOD__,
65+
Transition::class,
66+
isset($transition[0]) ? ' named "'.$transition[0].'"' : '',
67+
is_array($transition) ? ' an array with '.count($transition).' entries' : gettype($transition)));
68+
}
69+
list($name, $froms, $tos) = $transition;
70+
$this->addTransition($name, $froms, $tos);
71+
}
72+
}
73+
74+
public function getTransitions()
75+
{
76+
return $this->transitions;
77+
}
78+
79+
public function reset()
80+
{
81+
return $this->transitions = array();
82+
}
83+
84+
private function createTransition($name, $froms, $tos)
85+
{
86+
$this->add(new Transition($name, $froms, $tos));
87+
88+
}
89+
90+
private function add(Transition $newTransition) {
91+
foreach ($this->transitions as $transition) {
92+
if ($newTransition == $transition) {
93+
throw new InvalidArgumentException(sprintf('The transition named "%s" has already been added.', $name));
94+
}
95+
}
96+
$this->transitions[] = $newTransition;
97+
}
98+
}

0 commit comments

Comments
 (0)