Completed
Push — master ( fdb8ab...a48276 )
by Guillermo
14s
created

EntityStrictCollection::slice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Atrapalo\PHPTools\Collection;
4
5
/**
6
 * Class EntityStrictCollection
7
 * @package Atrapalo\PHPTools\Collection
8
 *
9
 * @author Guillermo González <[email protected]>
10
 */
11
abstract class EntityStrictCollection extends EntityCollection
12
{
13
    /**
14
     * Collection constructor.
15
     * @param array $entities
16
     * @param bool  $allowEntitiesChildren
17
     * @throws \Exception
18
     */
19 59
    public function __construct(array $entities, bool $allowEntitiesChildren = false)
20
    {
21 59
        if (empty($entities)) {
22 2
            throw static::customEmptyException();
23
        }
24
25 57
        $this->allowEntitiesChildren = $allowEntitiesChildren;
26
27 57
        foreach ($entities as $entity) {
28 57
            $this->add($entity);
29
        }
30 49
    }
31
32
    /**
33
     * @param int  $offset
34
     * @param null $length
35
     * @return static
36
     * @throws \Exception
37
     */
38 12
    public function slice(int $offset, $length = null)
39
    {
40 12
        $entities = array_slice($this->toArray(), $offset, $length, true);
41 12
        if (empty($entities)) {
42 6
            throw static::customEmptyException();
43
        }
44
45 6
        return new static($entities);
46
    }
47
48
    /**
49
     * @return \Exception
50
     */
51 7
    public static function customEmptyException(): \Exception
52
    {
53 7
        return new \LengthException('Collection can not be empty');
54
    }
55
}
56