Completed
Push — master ( ed9362...6730cd )
by J.D.
03:52
created

WordPoints_Hook_Arg::get_title()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * Hook arg class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since   1.0.0
8
 */
9
10
/**
11
 * Represents a hook arg.
12
 *
13
 * When an action is fired, each event that is triggered by it needs to retrieve one
14
 * more values related to that event. These are called the event args. The values may
15
 * come from the action itself, or from elsewhere. This class provides a common
16
 * interface for retrieving those values and converting them into entities.
17
 *
18
 * @since 1.0.0
19
 */
20
class WordPoints_Hook_Arg {
21
22
	/**
23
	 * The slug of this arg.
24
	 *
25
	 * @since 1.0.0
26
	 *
27
	 * @var string
28
	 */
29
	protected $slug;
30
31
	/**
32
	 * The slug of the type of entity that this arg's value is.
33
	 *
34
	 * @since 1.0.0
35
	 *
36
	 * @var string
37
	 */
38
	protected $entity_slug;
39
40
	/**
41
	 * The action object.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @var WordPoints_Hook_ActionI
46
	 */
47
	protected $action;
48
49
	/**
50
	 * Whether this arg is stateful or not.
51
	 *
52
	 * @since 1.0.0
53
	 *
54
	 * @var bool
55
	 */
56
	protected $is_stateful = false;
57
58
	/**
59
	 * Construct the arg with a slug.
60
	 *
61
	 * Slugs are typically the slug of the entity itself, but this isn't always the
62
	 * case. Sometimes more than one value associated with an event will be of the
63
	 * same type of entity. To work around this, the arg slug can also be an entity
64
	 * alias. Entity aliases are just entity slugs that are prefixed with an
65
	 * arbitrary string ending in a semicolon. For example, 'current:user' is an
66
	 * alias of the User entity.
67
	 *
68
	 * @since 1.0.0
69
	 *
70
	 * @param string                  $slug   The arg slug.
71
	 * @param WordPoints_Hook_ActionI $action The calling action's object.
72
	 */
73
	public function __construct( $slug, WordPoints_Hook_ActionI $action = null ) {
74
75
		$this->slug = $slug;
76
		$this->action = $action;
77
78
		$parts = explode( ':', $slug, 2 );
79
80
		if ( isset( $parts[1] ) ) {
81
			$this->entity_slug = $parts[1];
82
		} else {
83
			$this->entity_slug = $slug;
84
		}
85
	}
86
87
	/**
88
	 * Get the slug of this arg.
89
	 *
90
	 * @since 1.0.0
91
	 *
92
	 * @return string The arg slug.
93
	 */
94
	public function get_slug() {
95
		return $this->slug;
96
	}
97
98
	/**
99
	 * Get the slug of the type of entity this arg is.
100
	 *
101
	 * @since 1.0.0
102
	 *
103
	 * @return string The entity slug.
104
	 */
105
	public function get_entity_slug() {
106
		return $this->entity_slug;
107
	}
108
109
	/**
110
	 * Get the entity object for this arg's value.
111
	 *
112
	 * @since 1.0.0
113
	 *
114
	 * @return WordPoints_Entity|false The entity, or false if not registered.
115
	 */
116
	public function get_entity() {
117
118
		$entity = wordpoints_entities()->get(
119
			$this->get_entity_slug()
120
		);
121
122
		if ( $entity instanceof WordPoints_Entity ) {
123
			$entity->set_the_value( $this->get_value() );
124
		}
125
126
		return $entity;
127
	}
128
129
	/**
130
	 * Retrieves the value for this arg.
131
	 *
132
	 * @since 1.0.0
133
	 *
134
	 * @return mixed The arg value.
135
	 */
136
	public function get_value() {
137
138
		if ( $this->action instanceof WordPoints_Hook_ActionI ) {
139
			return $this->action->get_arg_value( $this->slug );
140
		} else {
141
			return null;
142
		}
143
	}
144
145
	/**
146
	 * Retrieves the human-readable title of this arg.
147
	 *
148
	 * @since 1.0.0
149
	 *
150
	 * @return string The arg title.
151
	 */
152
	public function get_title() {
153
154
		$entity = $this->get_entity();
155
156
		if ( ! $entity ) {
157
			return $this->slug;
158
		}
159
160
		return $entity->get_title();
161
	}
162
163
	/**
164
	 * Check whether the arg is stateful.
165
	 *
166
	 * @since 1.0.0
167
	 *
168
	 * @return bool Whether this arg is stateful.
169
	 */
170
	public function is_stateful() {
171
		return $this->is_stateful;
172
	}
173
}
174
175
// EOF
176