Completed
Pull Request — develop (#1425)
by Gennady
07:12
created

Entry_Sort::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * Entry sorting.
11
 */
12
class Entry_Sort {
13
14
	/**
15
	 * @var string An enum of sorts, sort direction identifier - ascending.
16
	 */
17
	const ASC = 'ASC';
18
19
	/**
20
	 * @var string An enum of sorts, sort direction identifier - descending.
21
	 */
22
	const DESC = 'DESC';
23
24
	/**
25
	 * @var string An enum of sorts, sort direction identifier - random.
26
	 */
27
	const RAND = 'RAND';
28
29
	/**
30
	 * @var string Numeric sort mode.
31
	 */
32
	const NUMERIC = 'NUMERIC';
33
34
	/**
35
	 * @var string Alpabetic sort mode.
36
	 */
37
	const ALPHA = 'ALPHA';
38
39
	/**
40
	 * @var \GV\Field The field that this sort is for.
41
	 */
42
	public $field;
43
44
	/**
45
	 * @var string The direction (see self::ASC, self::DESC).
46
	 */
47
	public $direction;
48
49
	/**
50
	 * @var string The sort mode (see self::NUMERIC, self::ALPHA).
51
	 */
52
	public $mode;
53
54
	/**
55
	 * Instantiate a sort for a field.
56
	 *
57
	 * @param \GV\Field $field The field we're sorting by.
58
	 * @param string $direction The direction of this sort (\GV\Entry_Sort::ASC, \GV\Entry_Sort::DESC, etc.). Default: self::ASC.
59
	 * @param string $mode The sort mode (self::NUMERIC). Default: self::ALPHA.
60
	 *
61
	 * @api
62
	 * @since 2.0
63
	 *
64
	 * @return \GV\Entry_Sort An instance of this class, pass to \GV\Entry_Collection::sort()
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
65
	 */
66 9
	public function __construct( \GV\Field $field, $direction = self::ASC, $mode = self::ALPHA ) {
67 9
		$this->field = $field;
68 9
		$this->direction = $direction;
69 9
		$this->mode = $mode;
70 9
	}
71
72
	/**
73
	 * Return search_criteria-compatible array.
74
	 *
75
	 * @return array [`key`, `direction`, `is_numeric`]
76
	 */
77
	public function to_sorting() {
78
		if ( $this->field ) {
79
			return array(
80
				'key' => $this->field->ID,
81
				'direction' => $this->direction ? : self::ASC,
82
				'is_numeric' => self::ALPHA ? true : false,
83
			);
84
		}
85
		return array();
86
	}
87
}
88