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
Nicolas Grekas 1da85910c7 Merge branch '3.4' into 4.0
* 3.4:
  SCA with Php Inspections (EA Extended)
  Add test case for #25264
  Fixed the null value exception case.
  Remove rc/beta suffix from composer.json files
  Throw an exception is expression language is not installed
  Fail as early and noisily as possible
  [Console][DI] Fail gracefully
  [FrameworkBundle] Fix visibility of a test helper
  [link] clear the cache after linking
  [DI] Trigger deprecation when setting a to-be-private synthetic service
  [link] Prevent warnings when running link with 2.7
  [Validator] ExpressionValidator should use OBJECT_TO_STRING to allow value in message
  do not eagerly filter comment lines
  [WebProfilerBundle], [TwigBundle] Fix Profiler breaking XHTML pages (Content-Type: application/xhtml+xml)
2017-12-04 13:31:58 +01:00

115 lines
2.8 KiB
PHP

<?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;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Exception\LogicException;
/**
* @author Fabien Potencier <fabien@symfony.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class Definition
{
private $places = array();
private $transitions = array();
private $initialPlace;
/**
* @param string[] $places
* @param Transition[] $transitions
* @param string|null $initialPlace
*/
public function __construct(array $places, array $transitions, string $initialPlace = null)
{
foreach ($places as $place) {
$this->addPlace($place);
}
foreach ($transitions as $transition) {
$this->addTransition($transition);
}
$this->setInitialPlace($initialPlace);
}
/**
* @return string|null
*/
public function getInitialPlace()
{
return $this->initialPlace;
}
/**
* @return string[]
*/
public function getPlaces(): array
{
return $this->places;
}
/**
* @return Transition[]
*/
public function getTransitions(): array
{
return $this->transitions;
}
private function setInitialPlace(string $place = null)
{
if (null === $place) {
return;
}
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;
}
private function addPlace(string $place)
{
if (!preg_match('{^[\w_-]+$}', $place)) {
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $place));
}
if (!count($this->places)) {
$this->initialPlace = $place;
}
$this->places[$place] = $place;
}
private function addTransition(Transition $transition)
{
$name = $transition->getName();
foreach ($transition->getFroms() as $from) {
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) {
if (!isset($this->places[$to])) {
throw new LogicException(sprintf('Place "%s" referenced in transition "%s" does not exist.', $to, $name));
}
}
$this->transitions[] = $transition;
}
}