Taxonomy.php
801 Bytes
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
<?php
namespace ACP\Helper\Select\Value;
use AC;
use LogicException;
use WP_Term;
final class Taxonomy
implements AC\Helper\Select\Value {
const ID = 'term_id';
const SLUG = 'slug';
/**
* @var string
*/
private $property;
/**
* @param null|string $property
*/
public function __construct( $property = null ) {
if ( null == $property ) {
$property = self::ID;
}
$this->property = $property;
$this->validate();
}
private function validate() {
$properties = [ self::ID, self::SLUG ];
if ( ! in_array( $this->property, $properties ) ) {
throw new LogicException( 'Invalid property found.' );
}
}
/**
* @param WP_Term $term
*
* @return string
*/
public function get_value( $term ) {
$property = $this->property;
return $term->$property;
}
}