This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Workflow/Definition.php

120 lines
2.9 KiB
PHP
Raw Normal View History

<?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;
/**
* @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>
*/
2016-11-08 19:01:05 +00:00
final class Definition
{
2016-03-25 15:43:30 +00:00
private $places = array();
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)
{
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);
}
}
2016-11-08 19:01:05 +00:00
/**
* @return string|null
*/
2016-03-25 15:43:30 +00:00
public function getInitialPlace()
{
2016-03-25 15:43:30 +00:00
return $this->initialPlace;
}
2016-11-08 19:01:05 +00:00
/**
* @return string[]
*/
2016-03-25 15:43:30 +00:00
public function getPlaces()
{
2016-03-25 15:43:30 +00:00
return $this->places;
}
/**
* @return Transition[]
*/
public function getTransitions()
{
return $this->transitions;
}
2016-11-08 19:01:05 +00:00
private function setInitialPlace($place)
{
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;
}
2016-11-08 19:01:05 +00:00
private function addPlace($place)
{
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));
}
2016-03-25 15:43:30 +00:00
if (!count($this->places)) {
$this->initialPlace = $place;
}
$this->places[$place] = $place;
}
2016-11-08 19:01:05 +00:00
private function addPlaces(array $places)
{
2016-03-25 15:43:30 +00:00
foreach ($places as $place) {
$this->addPlace($place);
}
2016-03-25 15:43:30 +00:00
}
2016-11-08 19:01:05 +00:00
private function addTransition(Transition $transition)
{
2016-03-25 15:43:30 +00:00
$name = $transition->getName();
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));
}
}
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));
}
}
$this->transitions[] = $transition;
}
}