Failed Conditions
Push — master ( e6b844...0f20c1 )
by Reüel
09:09 queued 12s
created

OrderItems::get_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Order items.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
/**
14
 * Order items.
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 2.1.8
18
 * @since   2.0.3
19
 */
20
class OrderItems implements \JsonSerializable {
21
	/**
22
	 * Order items.
23
	 *
24
	 * @var array<OrderItem>
25
	 */
26
	private $order_items;
27
28
	/**
29
	 * Construct order results message.
30
	 *
31
	 * @param array<OrderItem> $items Order items.
32
	 */
33 1
	public function __construct( $items = null ) {
34 1
		if ( \is_array( $items ) ) {
35
			foreach ( $items as $item ) {
36
				$this->add_item( $item );
37
			}
38
		}
39 1
	}
40
41
	/**
42
	 * Create and add new order item.
43
	 *
44
	 * @param string $name     Name.
45
	 * @param int    $quantity Quantity.
46
	 * @param Money  $amount   Amount.
47
	 * @param string $category Category.
48
	 * @return OrderItem
49
	 * @throws \InvalidArgumentException Throws invalid argument exception when arguments are invalid.
50
	 */
51 1
	public function new_item( $name, $quantity, Money $amount, $category ) {
52 1
		$item = new OrderItem( $name, $quantity, $amount, $category );
53
54 1
		$this->add_item( $item );
55
56 1
		return $item;
57
	}
58
59
	/**
60
	 * Add order item.
61
	 *
62
	 * @param OrderItem $item Order item.
63
	 * @return void
64
	 */
65 1
	public function add_item( OrderItem $item ) {
66 1
		$this->order_items[] = $item;
67 1
	}
68
69
	/**
70
	 * Get order items.
71
	 *
72
	 * @return array<OrderItem>
73
	 */
74 1
	public function get_order_items() {
75 1
		return $this->order_items;
76
	}
77
78
	/**
79
	 * Get JSON.
80
	 *
81
	 * @return array<object>|null
82
	 */
83
	public function jsonSerialize() {
84
		$data = \array_map(
85
			static function( OrderItem $item ) {
86
				return $item;
87
			},
88
			$this->get_order_items()
89
		);
90
91
		return $data;
92
	}
93
94
	/**
95
	 * Get signature fields.
96
	 *
97
	 * @param array<string> $fields Fields.
98
	 * @return array<string>
99
	 */
100 1
	public function get_signature_fields( $fields = array() ) {
101 1
		foreach ( $this->get_order_items() as $item ) {
102 1
			$fields = $item->get_signature_fields( $fields );
103
		}
104
105 1
		return $fields;
106
	}
107
}
108