Passed
Push — master ( 034e39...aabddd )
by Andrea Marco
04:28 queued 11s
created

ModelDtoFactory::getPropertyValueFromSource()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
1
<?php
2
3
namespace Cerbero\LaravelDto\Factories;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * The model DTO factory.
9
 *
10
 */
11
class ModelDtoFactory extends DtoFactory
12
{
13
    /**
14
     * Retrieve the value for the given property from the provided source
15
     *
16
     * @param string $property
17
     * @param mixed $source
18
     * @return mixed
19
     */
20 3
    protected function getPropertyValueFromSource(string $property, $source)
21
    {
22 3
        $snakeProperty = Str::snake($property);
23
24 3
        if (array_key_exists($snakeProperty, $attributes = $source->getAttributes())) {
25 3
            return $attributes[$snakeProperty];
26
        }
27
28 3
        if ($source->relationLoaded($snakeProperty)) {
29 3
            return $source->$snakeProperty->toArray();
30
        }
31
32 3
        $camelProperty = Str::camel($property);
33
34 3
        if ($source->relationLoaded($camelProperty)) {
35 3
            return $source->$camelProperty->toArray();
36
        }
37
38 3
        return static::MISSING_PROPERTY_TOKEN;
39
    }
40
}
41