Passed
Push — master ( c087b5...d4ad7c )
by Remco
12:51 queued 05:29
created

LineItems::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Line items.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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
/**
14
 * Line items.
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 1.0.0
18
 * @since   1.0.0
19
 */
20
class LineItems implements \JsonSerializable {
21
	/**
22
	 * Line items.
23
	 *
24
	 * @var array
25
	 */
26
	private $line_items;
27
28
	/**
29
	 * Construct line items.
30
	 *
31
	 * @param LineItem[] $items Line items.
32
	 */
33 8
	public function __construct( $items = null ) {
34 8
		$this->line_items = array();
35
36 8
		if ( is_array( $items ) ) {
37 3
			foreach ( $items as $item ) {
38 2
				$this->add_item( $item );
39
			}
40
		}
41 8
	}
42
43
	/**
44
	 * Create and add new line item.
45
	 *
46
	 * @param string $description          Name.
47
	 * @param int    $quantity             Quantity.
48
	 * @param int    $amount_including_tax Amount (including tax).
49
	 *
50
	 * @return LineItem
51
	 *
52
	 * @throws \InvalidArgumentException Throws invalid argument exception when arguments are invalid.
53
	 */
54 1
	public function new_item( $description, $quantity, $amount_including_tax ) {
55 1
		$item = new LineItem( $description, $quantity, $amount_including_tax );
56
57 1
		$this->add_item( $item );
58
59 1
		return $item;
60
	}
61
62
	/**
63
	 * Add line item.
64
	 *
65
	 * @param LineItem $item Line item.
66
	 * @return void
67
	 */
68 3
	public function add_item( LineItem $item ) {
69 3
		$this->line_items[] = $item;
70 3
	}
71
72
	/**
73
	 * Get line items.
74
	 *
75
	 * @return LineItem[]
76
	 */
77 7
	public function get_line_items() {
78 7
		return $this->line_items;
79
	}
80
81
	/**
82
	 * Get JSON.
83
	 *
84
	 * @return array<object>
85
	 */
86 1
	public function get_json() {
87 1
		$data = array_map(
88
			/**
89
			 * Get line item JSON.
90
			 *
91
			 * @param LineItem $item Line item.
92
			 * @return object
93
			 */
94 1
			function( LineItem $item ) {
95 1
				return $item->get_json();
96 1
			},
97 1
			$this->get_line_items()
98
		);
99
100 1
		return $data;
101
	}
102
103
	/**
104
	 * JSON serialize.
105
	 *
106
	 * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
107
	 * @return array<object>
108
	 */
109 1
	public function jsonSerialize() {
110 1
		return $this->get_json();
111
	}
112
}
113