5149e265e97b7714efcf61c7e7897b622a1f32b8
[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 class State implements StateInterface {
9
10   /**
11    * The workflow the state is attached to.
12    *
13    * @var \Drupal\workflows\WorkflowInterface
14    */
15   protected $workflow;
16
17   /**
18    * The state's ID.
19    *
20    * @var string
21    */
22   protected $id;
23
24   /**
25    * The state's label.
26    *
27    * @var string
28    */
29   protected $label;
30
31   /**
32    * The state's weight.
33    *
34    * @var int
35    */
36   protected $weight;
37
38   /**
39    * State constructor.
40    *
41    * @param \Drupal\workflows\WorkflowInterface $workflow
42    *   The workflow the state is attached to.
43    * @param string $id
44    *   The state's ID.
45    * @param string $label
46    *   The state's label.
47    * @param int $weight
48    *   The state's weight.
49    */
50   public function __construct(WorkflowTypeInterface $workflow, $id, $label, $weight = 0) {
51     $this->workflow = $workflow;
52     $this->id = $id;
53     $this->label = $label;
54     $this->weight = $weight;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function id() {
61     return $this->id;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function label() {
68     return $this->label;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function weight() {
75     return $this->weight;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function canTransitionTo($to_state_id) {
82     return $this->workflow->hasTransitionFromStateToState($this->id, $to_state_id);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getTransitionTo($to_state_id) {
89     if (!$this->canTransitionTo($to_state_id)) {
90       throw new \InvalidArgumentException("Can not transition to '$to_state_id' state");
91     }
92     return $this->workflow->getTransitionFromStateToState($this->id(), $to_state_id);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getTransitions() {
99     return $this->workflow->getTransitionsForState($this->id);
100   }
101
102   /**
103    * Helper method to convert a State value object to a label.
104    *
105    * @param \Drupal\workflows\StateInterface $state
106    *
107    * @return string
108    *   The label of the state.
109    */
110   public static function labelCallback(StateInterface $state) {
111     return $state->label();
112   }
113
114 }