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

EntityStrictCollection::customEmptyException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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