Passed
Push — 1.x ( 2f6c1b...f64955 )
by Ulises Jeremias
02:44
created

Builder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Mbh\Collection\Traits\Sequenceable\Arrayed;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use SplFixedArray;
12
use Traversable;
13
use Countable as CountableInterface;
14
15
trait Builder
16
{
17
    protected $sfa = null;
18
19
    /**
20
     * Create an fixed array
21
     *
22
     * @param Traversable $array data
23
     */
24
    protected function __construct(Traversable $array)
25
    {
26
        $this->sfa = $array;
27
        $this->checkCapacity();
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public static function empty()
34
    {
35
        return new static(new SplFixedArray(0));
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public static function fromArray(array $array)
42
    {
43
        return new static(SplFixedArray::fromArray($array));
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public static function fromItems(Traversable $array)
50
    {
51
        if (!$array instanceof CountableInterface) {
52
            return static::fromArray(iterator_to_array($array));
53
        }
54
55
        $sfa = new SplFixedArray(count($array));
56
57
        foreach ($array as $i => $elem) {
58
            $sfa[$i] = $elem;
59
        }
60
61
        return new static($sfa);
62
    }
63
64
    abstract protected function checkCapacity();
65
}
66