CartSubItem::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait;
6
7
/**
8
 * Class CartItemOption.
9
 *
10
 * @property float price
11
 * @property array options
12
 * @property array items
13
 */
14
class CartSubItem
15
{
16
    use CartOptionsMagicMethodsTrait;
17
18
    const ITEMS = 'items';
19
20
    public $locale;
21
    public $internationalFormat;
22
23
    private $itemHash;
24
25
    /**
26
     * @param $options
27
     */
28
    public function __construct($options)
29
    {
30
        $this->options['items'] = [];
31
32
        foreach ($options as $option => $value) {
33
            array_set($this->options, $option, $value);
34
        }
35
36
        $this->itemHash = app(LaraCart::HASH, $this->options);
37
    }
38
39
    /**
40
     * Gets the hash for the item.
41
     *
42
     * @return mixed
43
     */
44
    public function getHash()
45
    {
46
        return $this->itemHash;
47
    }
48
49
    /**
50
     * Gets the formatted price.
51
     *
52
     * @param bool|true $format
53
     * @param bool      $taxedItemsOnly
54
     *
55
     * @return string
56
     */
57
    public function price($format = true, $taxedItemsOnly = true)
58
    {
59
        $price = $this->price;
60
61
        if (isset($this->items)) {
62
            foreach ($this->items as $item) {
63
                if ($taxedItemsOnly && !$item->taxable) {
64
                    continue;
65
                }
66
                $price += $item->price(false, $taxedItemsOnly);
67
            }
68
        }
69
70
        return LaraCart::formatMoney($price, $this->locale, $this->internationalFormat, $format);
71
    }
72
73
    /**
74
     * Search for matching options on the item.
75
     *
76
     * @return mixed
77
     */
78
    public function find($data)
79
    {
80
        foreach ($data as $key => $value) {
81
            if ($this->$key === $value) {
82
                return $this;
83
            }
84
        }
85
    }
86
}
87