Completed
Push — master ( 213dd9...c614be )
by Wojciech
9s
created

GroupEntity::setProducts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
/*
3
 * This file is part of the PayBreak/basket package.
4
 *
5
 * (c) PayBreak <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PayBreak\Sdk\Entities;
12
13
use WNowicki\Generic\AbstractEntity;
14
use WNowicki\Generic\Exception;
15
16
/**
17
 * Group Entity
18
 *
19
 * @author EB
20
 * @method $this setId(int $id)
21
 * @method int|null getId()
22
 * @method $this setName(string $name)
23
 * @method string|null getName()
24
 * @package PayBreak\Sdk\Entities
25
 */
26
class GroupEntity extends AbstractEntity
27
{
28
    private $products = [];
29
30
    protected $properties = [
31
        'id',
32
        'name',
33
    ];
34
35
    /**
36
     * @return ProductEntity[]
37
     */
38 6
    public function getProducts()
39
    {
40 6
        return $this->products;
41
    }
42
43
    /**
44
     * @param ProductEntity $product
45
     */
46 2
    public function addProduct(ProductEntity $product)
47
    {
48 2
        $this->products[] = $product;
49 2
    }
50
51
    /**
52
     * @author EB, WN
53
     * @param array $products
54
     * @return $this
55
     */
56 4
    public function setProducts(array $products)
57
    {
58 4
        $this->products = [];
59
60 4
        foreach ($products as $product) {
61 4
            if (is_array($product)) {
62 2
                $this->addProduct(ProductEntity::make($product));
63 2
                continue;
64
            }
65
66 4
            if ($product instanceof ProductEntity) {
67 2
                $this->addProduct($product);
68 2
            }
69 4
        }
70
71 4
        return $this;
72
    }
73
74
    /**
75
     * Make Entity
76
     *
77
     * @author WN
78
     * @param array $components
79
     * @return static
80
     */
81 8
    public static function make(array $components)
82
    {
83 8
        $entity = parent::make($components);
84
85 8
        if (array_key_exists('products', $components) && is_array($components['products'])) {
86
87 4
            $entity->setProducts($components['products']);
88 4
        }
89
90 8
        return $entity;
91
    }
92
93
    /**
94
     * To Array
95
     *
96
     * @author WN
97
     * @param bool $recursively If set to `true` then toArray(true) will be called on each `Arrayable` property
98
     * @return array
99
     */
100 2
    public function toArray($recursively = false)
101
    {
102 2
        $ar =  parent::toArray($recursively);
103
104 2
        if ($recursively) {
105
            foreach ($this->getProducts() as $product) {
106
107
                $ar['products'][] = $product->toArray();
108
            }
109
        }
110
111 2
        return $ar;
112
    }
113
}
114