Name::get_json()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Name
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use InvalidArgumentException;
14
15
/**
16
 * Name
17
 *
18
 * @link https://docs.adyen.com/developers/api-reference/common-api/name
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.0
22
 * @since   1.0.0
23
 */
24
class Name {
25
	/**
26
	 * First name.
27
	 *
28
	 * @var string
29
	 */
30
	private $first_name;
31
32
	/**
33
	 * Gender.
34
	 *
35
	 * @var string
36
	 */
37
	private $gender;
38
39
	/**
40
	 * The name's infix, if applicable.
41
	 *
42
	 * @var string|null
43
	 */
44
	private $infix;
45
46
	/**
47
	 * Last name.
48
	 *
49
	 * @var string
50
	 */
51
	private $last_name;
52
53
	/**
54
	 * Construct shopper name.
55
	 *
56
	 * @param string $first_name First name.
57
	 * @param string $last_name  Last name.
58
	 * @param string $gender     Gender.
59
	 */
60 3
	public function __construct( $first_name, $last_name, $gender ) {
61 3
		$this->first_name = $first_name;
62 3
		$this->last_name  = $last_name;
63 3
		$this->gender     = $gender;
64 3
	}
65
66
	/**
67
	 * Get first name.
68
	 *
69
	 * @return string
70
	 */
71 2
	public function get_first_name() {
72 2
		return $this->first_name;
73
	}
74
75
	/**
76
	 * Get gender.
77
	 *
78
	 * @return string
79
	 */
80 2
	public function get_gender() {
81 2
		return $this->gender;
82
	}
83
84
	/**
85
	 * Get infix.
86
	 *
87
	 * @return string|null
88
	 */
89 2
	public function get_infix() {
90 2
		return $this->infix;
91
	}
92
93
	/**
94
	 * Set infix.
95
	 *
96
	 * @param string|null $infix Infix.
97
	 * @return void
98
	 * @throws InvalidArgumentException Throws invalid argument exception when infix is longer then 20 characters.
99
	 */
100 2
	public function set_infix( $infix ) {
101 2
		if ( null !== $infix && mb_strlen( $infix ) > 20 ) {
102 1
			throw new InvalidArgumentException(
103 1
				sprintf(
104 1
					'Given infix `%s` is longer then 20 characters.',
105
					$infix
106
				)
107
			);
108
		}
109
110 1
		$this->infix = $infix;
111 1
	}
112
113
	/**
114
	 * Get last name.
115
	 *
116
	 * @return string
117
	 */
118 2
	public function get_last_name() {
119 2
		return $this->last_name;
120
	}
121
122
	/**
123
	 * Get JSON.
124
	 *
125
	 * @return object
126
	 */
127 1
	public function get_json() {
128 1
		$properties = Util::filter_null(
129
			array(
130 1
				'firstName' => $this->get_first_name(),
131 1
				'gender'    => $this->get_gender(),
132 1
				'infix'     => $this->get_infix(),
133 1
				'lastName'  => $this->get_last_name(),
134
			)
135
		);
136
137 1
		$object = (object) $properties;
138
139 1
		return $object;
140
	}
141
}
142