2013-12-01 09:16:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Workflow;
|
|
|
|
|
2016-03-25 15:43:30 +00:00
|
|
|
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
|
|
|
|
use Symfony\Component\Workflow\Exception\LogicException;
|
|
|
|
|
2013-12-01 09:16:07 +00:00
|
|
|
/**
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2016-03-25 15:43:30 +00:00
|
|
|
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
2016-11-08 19:01:05 +00:00
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
2013-12-01 09:16:07 +00:00
|
|
|
*/
|
2016-11-08 19:01:05 +00:00
|
|
|
final class Definition
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
private $places = array();
|
2013-12-01 09:16:07 +00:00
|
|
|
private $transitions = array();
|
2016-03-25 15:43:30 +00:00
|
|
|
private $initialPlace;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition constructor.
|
|
|
|
*
|
|
|
|
* @param string[] $places
|
|
|
|
* @param Transition[] $transitions
|
2016-11-08 19:01:05 +00:00
|
|
|
* @param string|null $initialPlace
|
2016-03-25 15:43:30 +00:00
|
|
|
*/
|
2016-11-08 19:01:05 +00:00
|
|
|
public function __construct(array $places, array $transitions, $initialPlace = null)
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
$this->addPlaces($places);
|
2016-11-08 19:01:05 +00:00
|
|
|
$this->setInitialPlace($initialPlace);
|
|
|
|
foreach ($transitions as $transition) {
|
|
|
|
$this->addTransition($transition);
|
|
|
|
}
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-03-25 15:43:30 +00:00
|
|
|
public function getInitialPlace()
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
return $this->initialPlace;
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2016-03-25 15:43:30 +00:00
|
|
|
public function getPlaces()
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
return $this->places;
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:13:16 +01:00
|
|
|
/**
|
|
|
|
* @return Transition[]
|
|
|
|
*/
|
2013-12-01 09:16:07 +00:00
|
|
|
public function getTransitions()
|
|
|
|
{
|
|
|
|
return $this->transitions;
|
|
|
|
}
|
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
private function setInitialPlace($place)
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-11-08 19:01:05 +00:00
|
|
|
if (null === $place) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-25 15:43:30 +00:00
|
|
|
if (!isset($this->places[$place])) {
|
|
|
|
throw new LogicException(sprintf('Place "%s" cannot be the initial place as it does not exist.', $place));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->initialPlace = $place;
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
private function addPlace($place)
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
if (!preg_match('{^[\w\d_-]+$}', $place)) {
|
2016-06-24 09:16:28 +01:00
|
|
|
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $place));
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 15:43:30 +00:00
|
|
|
if (!count($this->places)) {
|
|
|
|
$this->initialPlace = $place;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->places[$place] = $place;
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
private function addPlaces(array $places)
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
foreach ($places as $place) {
|
|
|
|
$this->addPlace($place);
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
2016-03-25 15:43:30 +00:00
|
|
|
}
|
2013-12-01 09:16:07 +00:00
|
|
|
|
2016-11-08 19:01:05 +00:00
|
|
|
private function addTransition(Transition $transition)
|
2013-12-01 09:16:07 +00:00
|
|
|
{
|
2016-03-25 15:43:30 +00:00
|
|
|
$name = $transition->getName();
|
2013-12-01 09:16:07 +00:00
|
|
|
|
|
|
|
foreach ($transition->getFroms() as $from) {
|
2016-03-25 15:43:30 +00:00
|
|
|
if (!isset($this->places[$from])) {
|
|
|
|
throw new LogicException(sprintf('Place "%s" referenced in transition "%s" does not exist.', $from, $name));
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($transition->getTos() as $to) {
|
2016-03-25 15:43:30 +00:00
|
|
|
if (!isset($this->places[$to])) {
|
|
|
|
throw new LogicException(sprintf('Place "%s" referenced in transition "%s" does not exist.', $to, $name));
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:13:16 +01:00
|
|
|
$this->transitions[] = $transition;
|
2013-12-01 09:16:07 +00:00
|
|
|
}
|
|
|
|
}
|