Failed Conditions
Push — master ( e245a2...503ac3 )
by Arnold
8s
created

GetByReference::withSourceAndTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor\Helper;
4
5
use Jasny\DotKey;
6
7
/**
8
 * Get property from source or target by reference
9
 */
10
trait GetByReference
11
{
12
    /**
13
     * @var object
14
     */
15
    protected $source;
16
17
    /**
18
     * @var object|array
19
     */
20
    protected $target;
21
22
    
23
    /**
24
     * Get copy of processor that uses the given source and target
25
     * 
26
     * @param object       $source  Data source
27
     * @param array|object $target  Target or dot key path
28
     */
29
    public function withSourceAndTarget($source, $target)
30
    {
31
        $clone = clone $this;
32
        
33
        $clone->source = $source;
34
        $clone->target = $target;
35
        
36
        return $clone;
37
    }
38
    
39
    /**
40
     * Get item by reference
41
     * 
42
     * @param string       $ref
43
     * @param object       $source
44
     * @param object|array $target
45
     */
46
    protected function getByReference($ref, $source, $target)
47
    {
48
        $subject = $source;
49
        
50
        if ($ref === '$') {
51
            return $source;
52
        } elseif ($ref === '@') {
53
            return $target;
54
        }
55
        
56
        if (substr($ref, 0, 2) === '$.') {
57
            $ref = substr($ref, 2);
58
        } elseif (substr($ref, 0, 2) === '@.') {
59
            $ref = substr($ref, 2);
60
            $subject = $target;
61
        }
62
        
63
        return DotKey::on($subject)->get($ref);
64
    }
65
}
66