Version 1
[yaffs-website] / web / core / modules / workflows / src / State.php
1 <?php
2
3 namespace Drupal\workflows;
4
5 /**
6  * A value object representing a workflow state.
7  *
8  * @internal
9  *   The workflow system is currently experimental and should only be leveraged
10  *   by experimental modules and development releases of contributed modules.
11  */
12 class State implements StateInterface {
13
14   /**
15    * The workflow the state is attached to.
16    *
17    * @var \Drupal\workflows\WorkflowInterface
18    */
19   protected $workflow;
20
21   /**
22    * The state's ID.
23    *
24    * @var string
25    */
26   protected $id;
27
28   /**
29    * The state's label.
30    *
31    * @var string
32    */
33   protected $label;
34
35   /**
36    * The state's weight.
37    *
38    * @var int
39    */
40   protected $weight;
41
42   /**
43    * State constructor.
44    *
45    * @param \Drupal\workflows\WorkflowInterface $workflow
46    *   The workflow the state is attached to.
47    * @param string $id
48    *   The state's ID.
49    * @param string $label
50    *   The state's label.
51    * @param int $weight
52    *   The state's weight.
53    */
54   public function __construct(WorkflowInterface $workflow, $id, $label, $weight = 0) {
55     $this->workflow = $workflow;
56     $this->id = $id;
57     $this->label = $label;
58     $this->weight = $weight;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function id() {
65     return $this->id;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function label() {
72     return $this->label;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function weight() {
79     return $this->weight;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function canTransitionTo($to_state_id) {
86     return $this->workflow->hasTransitionFromStateToState($this->id, $to_state_id);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getTransitionTo($to_state_id) {
93     if (!$this->canTransitionTo($to_state_id)) {
94       throw new \InvalidArgumentException("Can not transition to '$to_state_id' state");
95     }
96     return $this->workflow->getTransitionFromStateToState($this->id(), $to_state_id);
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getTransitions() {
103     return $this->workflow->getTransitionsForState($this->id);
104   }
105
106   /**
107    * Helper method to convert a list of states to labels
108    *
109    * @param \Drupal\workflows\StateInterface $state
110    *
111    * @return string
112    *   The label of the state.
113    */
114   public static function labelCallback(StateInterface $state) {
115     return $state->label();
116   }
117
118 }