Completed
Push — master ( 52ef0c...b4ceaf )
by Stéphane
01:21
created

Collection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 72
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collect() 0 4 2
A make() 0 4 1
A map() 0 8 2
A mapInto() 0 10 2
1
<?php
2
3
namespace StephaneCoinon\SendGridActivity\Support;
4
5
use StephaneCoinon\SendGridActivity\Integrations\Framework;
6
7
/**
8
 * Cast an array into the framework native collection (or array for vanilla PHP).
9
 */
10
class Collection
11
{
12
    /**
13
     * Collection items.
14
     *
15
     * @var array|\Illuminate\Support\Collection
16
     */
17
    protected $items = [];
18
19
    /**
20
     * Get a new Collection instance.
21
     *
22
     * @param array $items
23
     */
24
    public function __construct(array $items)
25
    {
26
        $this->items = static::collect($items);
27
    }
28
29
    /**
30
     * Cast an array into the framework native collection.
31
     *
32
     * @param array $items
33
     * @return array|\Illuminate\Support\Collection
34
     */
35
    public static function collect(array $items)
36
    {
37
        return Framework::isLaravel() ? collect($items) : $items;
38
    }
39
40
    /**
41
     * Get a new Collection instance.
42
     *
43
     * @param array $items
44
     * @return self
45
     */
46
    public static function make(array $items): self
47
    {
48
        return new static($items);
49
    }
50
51
    /**
52
     * Run a map over each of the items.
53
     *
54
     * @param  callable $callback
55
     * @return array|\Illuminate\Support\Collection
56
     */
57
    public function map(callable $callback)
58
    {
59
        if (Framework::isLaravel()) {
60
            return $this->items->map($callback);
61
        }
62
63
        return array_map($callback, $this->items);
64
    }
65
66
    /**
67
     * Undocumented function
68
     *
69
     * @return array|\Illuminate\Support\Collection
70
     */
71
    public function mapInto(string $model)
72
    {
73
        if (Framework::isLaravel()) {
74
            return $this->items->mapInto($model);
75
        }
76
77
        return array_map(function ($item) use ($model) {
78
            return new $model($item);
79
        }, $this->items);
80
    }
81
}
82