HasMany::matchSimple()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 4
rs 9.9
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Relations\HasMany as BaseHasMany;
6
use Illuminate\Support\Collection;
7
use stdClass;
8
use Volosyuk\SimpleEloquent\ModelAccessor;
9
10
/**
11
 * Class HasManyWithSimple
12
 * @package Volosyuk\SimpleEloquent
13
 */
14
class HasMany extends BaseHasMany
15
{
16
    use Relation;
17
18
    /**
19
     * @param array $models
20
     * @param Collection $results
21
     * @param $relation
22
     * @return array|stdClass[]
23
     */
24 1
    protected function matchSimple(array &$models, Collection $results, $relation)
25
    {
26 1
        $dictionary = [];
27
28 1
        $foreign = $this->getForeignKeyName();
29
30 1
        foreach ($results as $result) {
31 1
            $dictionary[ModelAccessor::get($result, $foreign)][] = $result;
32
        }
33
34 1
        foreach ($models as &$model) {
35 1
            $value = [];
36
37 1
            if (isset($dictionary[$key = ModelAccessor::get($model, $this->localKey)])) {
38 1
                $value = $dictionary[$key];
39
            }
40
41 1
            ModelAccessor::set($model, $relation, Collection::make($value));
42
        }
43 1
        unset($model);
44
45 1
        return $models;
46
    }
47
}
48