Completed
Push — master ( 6055b4...4680f5 )
by ARCANEDEV
06:39
created

Model::getDirty()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 0
dl 0
loc 20
ccs 0
cts 17
cp 0
crap 42
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Arcanedev\Support\Bases;
2
3
use Arcanedev\Support\Traits\PrefixedModel;
4
use Illuminate\Database\Eloquent\Model as Eloquent;
5
6
/**
7
 * Class     Model
8
 *
9
 * @package  Arcanedev\Support\Laravel
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class Model extends Eloquent
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Traits
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    use PrefixedModel;
19
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Other Functions
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * Get the attributes that have been changed since last sync.
26
     *
27
     * @return array
28
     */
29
    public function getDirty()
30
    {
31
        $dirty    = [];
32
        $original = $this->getCastedOriginal();
33
34
        foreach ($this->attributes as $key => $value) {
35
            if ($this->hasCast($key, $value)) {
36
                $value = $this->castAttribute($key, $value);
37
            }
38
39
            if (
40
                ! array_key_exists($key, $original) ||
41
                ($value !== $original[$key] && ! $this->originalIsNumericallyEquivalent($key))
42
            ) {
43
                $dirty[$key] = $value;
44
            }
45
        }
46
47
        return $dirty;
48
    }
49
50
    /**
51
     * Get the casted original attributes.
52
     *
53
     * @return array
54
     */
55
    protected function getCastedOriginal()
56
    {
57
        $original = [];
58
59
        foreach ($this->original as $key => $value) {
60
            $original[$key] = $this->hasCast($key) ? $this->castAttribute($key, $value) : $value;
61
        }
62
63
        return $original;
64
    }
65
}
66