|
| 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