Completed
Push — master ( c4606f...2cda3d )
by
unknown
15:54 queued 06:31
created

Term_Condition::is_fulfilled()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 23
nc 5
nop 1
dl 0
loc 28
rs 8.439
c 1
b 1
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\Toolset\WP_Toolset;
6
7
/**
8
 * Check for a specific term
9
 *
10
 * Accepts the following values:
11
 *     Operators "=" and "!=":
12
 *         array(
13
 *             'value'=>...,
14
 *             'taxonomy'=>...,
15
 *             ['field'=>...] // "slug", "term_id" etc. - see get_term_by()
16
 *         )
17
 *
18
 *     Operators "IN" and "NOT IN":
19
 *         array(
20
 *             array(
21
 *                 'value'=>...,
22
 *                 'taxonomy'=>...,
23
 *                 ['field'=>...]
24
 *             ),
25
 *             ...
26
 *         )
27
 *
28
 *     Operator "CUSTOM" is passed the term_id
29
 */
30
class Term_Condition extends Condition {
31
32
	/**
33
	 * WP_Toolset to fetch term data with
34
	 *
35
	 * @var WP_Toolset
36
	 */
37
	protected $wp_toolset;
38
39
	/**
40
	 * Constructor
41
	 */
42
	public function __construct( $wp_toolset ) {
43
		$this->wp_toolset = $wp_toolset;
44
	}
45
46
	/**
47
	 * Get the condition value
48
	 *
49
	 * @return mixed
50
	 */
51
	public function get_value() {
52
		$value = parent::get_value();
53
		if ( is_array( $value ) ) {
54
			if ( isset( $value['value'] ) ) {
55
				$value = $this->wp_toolset->wildcard_term_descriptor_to_full_term_descriptor( $value );
56
			} else {
57
				$value = array_map( array( $this->wp_toolset, 'wildcard_term_descriptor_to_full_term_descriptor' ), $value );
58
			}
59
		}
60
		return $value;
61
	}
62
63
	/**
64
	 * Pluck term id from a full term descriptor
65
	 *
66
	 * @param  array   $full_term_descriptor
67
	 * @return integer
68
	 */
69
	protected function get_term_id_from_full_term_descriptor( $full_term_descriptor ) {
70
		return intval( $full_term_descriptor['term_object']->term_id );
71
	}
72
73
	/**
74
	 * Pluck term ids from array of full term descriptors
75
	 *
76
	 * @param  array          $full_term_descriptors
77
	 * @return array<integer>
78
	 */
79
	protected function get_term_ids_from_full_term_descriptors( $full_term_descriptors ) {
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $full_term_descriptors exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
80
		return array_map( array( $this, 'get_term_id_from_full_term_descriptor'), $full_term_descriptors );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
81
	}
82
83
	/**
84
	 * Check if the condition is fulfilled
85
	 *
86
	 * @param  array $environment
87
	 * @return bool
88
	 */
89
	public function is_fulfilled( $environment ) {
90
		$term_id = $environment['term_id'];
91
92
		switch ( $this->get_comparison_operator() ) {
93
			case '=':
94
				$value_term_id = $this->get_term_id_from_full_term_descriptor( $this->get_value() );
95
				return $term_id == $value_term_id;
96
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
97
			case '!=':
98
				$value_term_id = $this->get_term_id_from_full_term_descriptor( $this->get_value() );
99
				return $term_id != $value_term_id;
100
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
101
			case 'IN':
102
				$value_term_ids = $this->get_term_ids_from_full_term_descriptors( $this->get_value() );
103
				return in_array( $term_id, $value_term_ids );
104
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
105
			case 'NOT IN':
106
				$value_term_ids = $this->get_term_ids_from_full_term_descriptors( $this->get_value() );
107
				return ! in_array( $term_id, $value_term_ids );
108
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
109
		}
110
111
		return $this->compare(
112
			$term_id,
113
			$this->get_comparison_operator(),
114
			$this->get_value()
115
		);
116
	}
117
}