AbstractModelCollection::getItemId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/skeleton/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/skeleton
7
 */
8
9
namespace Flipbox\Skeleton\Collections;
10
11
use Flipbox\Skeleton\Error\ErrorTrait;
12
use Flipbox\Skeleton\Model\ModelInterface;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 * @method ModelInterface[] getItems($indexBy = null)
18
 */
19
abstract class AbstractModelCollection extends AbstractObjectCollection implements ModelCollectionInterface
20
{
21
22
    use ErrorTrait {
23
        getErrors as _traitGetErrors;
24
        clearErrors as _traitClearErrors;
25
    }
26
27
    /**
28
     * The item instance class
29
     */
30
    const ITEM_CLASS_INSTANCE = ModelInterface::class;
31
32
33
    /*******************************************
34
     * AGGREGATE ERRORS
35
     *******************************************/
36
37
    /**
38
     * Merge errors from all
39
     * @inheritdoc
40
     */
41
    public function getErrors($attribute = null)
42
    {
43
        $itemErrors = [];
44
45
        foreach ($this->getItems() as $item) {
46
            if ($item->hasErrors($attribute)) {
47
                $itemErrors[$this->getItemId($item)] = $item->getErrors($attribute);
48
            }
49
        }
50
51
        return array_merge(
52
            $this->_traitGetErrors($attribute),
53
            $itemErrors
54
        );
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function clearErrors($attribute = null)
61
    {
62
        foreach ($this->getItems() as $item) {
63
            $item->clearErrors($attribute);
64
        }
65
66
        $this->_traitClearErrors($attribute);
67
    }
68
69
    /**
70
     * Get a unique Id for an object
71
     *
72
     * @param $item
73
     * @return string
74
     */
75
    protected function getItemId($item)
76
    {
77
        return spl_object_hash($item);
78
    }
79
}
80