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

EntityStrictCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A slice() 0 9 2
A customEmptyException() 0 4 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