MslsLanguageArray::get_val()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsLanguageArray
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Stores the language input from post
12
 * @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/c78a78b42cb4c9e97a118523f7497f02b838a2ee/MslsLanguageArray.php
13
 * @package Msls
14
 */
15
class MslsLanguageArray {
16
17
	/**
18
	 * Generic container
19
	 * @var array
20
	 */
21
	protected $arr;
22
23
	/**
24
	 * Constructor
25
	 * @param array $arr
26
	 */
27
	public function __construct( array $arr = [] ) {
28
		foreach ( $arr as $key => $value ) {
29
			$this->set( $key, $value );
30
		}
31
	}
32
33
	/**
34
	 * Set a key-value-pair
35
	 * - $key must be a string of length >= 2
36
	 * - $value must be an integer > 0
37
	 * @param string $key
38
	 * @param mixed $value
39
	 * @return MslsLanguageArray
40
	 */
41
	public function set( $key, $value ) {
42
		$value = (int) $value;
43
		if ( 2 <= strlen( $key ) && 0 < $value ) {
44
			$this->arr[ $key ] = $value;
45
		}
46
		return $this;
47
	}
48
49
50
	/**
51
	 * Get the value of the element with the specified key
52
	 * @param string $key
53
	 * @return int
54
	 */
55
	public function get_val( $key ) {
56
		return( isset( $this->arr[ $key ] ) ? $this->arr[ $key ] : 0 );
57
	}
58
59
	/**
60
	 * Get the filtered array without the specified element
61
	 * @param string $key
62
	 * @return array
63
	 */
64
	public function get_arr( $key = '' ) {
65
		$arr = $this->arr;
66
		if ( isset( $arr[ $key ] ) ) {
67
			unset( $arr[ $key ] );
68
		}
69
		return $arr;
70
	}
71
72
}
73