summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/workflows/src/State.php
blob: e4e65579bf2391693ca8d45d72888b467575456c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Drupal\workflows;

/**
 * A value object representing a workflow state.
 */
class State implements StateInterface {

  /**
   * The workflow the state is attached to.
   *
   * @var \Drupal\workflows\WorkflowTypeInterface
   */
  protected $workflow;

  /**
   * The state's ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The state's label.
   *
   * @var string
   */
  protected $label;

  /**
   * The state's weight.
   *
   * @var int
   */
  protected $weight;

  /**
   * State constructor.
   *
   * @param \Drupal\workflows\WorkflowTypeInterface $workflow
   *   The workflow the state is attached to.
   * @param string $id
   *   The state's ID.
   * @param string $label
   *   The state's label.
   * @param int $weight
   *   The state's weight.
   */
  public function __construct(WorkflowTypeInterface $workflow, $id, $label, $weight = 0) {
    $this->workflow = $workflow;
    $this->id = $id;
    $this->label = $label;
    $this->weight = $weight;
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->label;
  }

  /**
   * {@inheritdoc}
   */
  public function weight() {
    return $this->weight;
  }

  /**
   * {@inheritdoc}
   */
  public function canTransitionTo($to_state_id) {
    return $this->workflow->hasTransitionFromStateToState($this->id, $to_state_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitionTo($to_state_id) {
    if (!$this->canTransitionTo($to_state_id)) {
      throw new \InvalidArgumentException("Can not transition to '$to_state_id' state");
    }
    return $this->workflow->getTransitionFromStateToState($this->id(), $to_state_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitions() {
    return $this->workflow->getTransitionsForState($this->id);
  }

  /**
   * Helper method to convert a State value object to a label.
   *
   * @param \Drupal\workflows\StateInterface $state
   *   The state.
   *
   * @return string
   *   The label of the state.
   */
  public static function labelCallback(StateInterface $state) {
    return $state->label();
  }

}