Failed Conditions
Push — master ( 04820f...7dec56 )
by Moesjarraf
03:10
created

GetByReference::withSourceAndTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
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 1
    public function withSourceAndTarget($source, $target)
30
    {
31 1
        $clone = clone $this;
32
        
33 1
        $clone->source = $source;
34 1
        $clone->target = $target;
35
        
36 1
        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 1
    protected function getByReference($ref, $source, $target)
47
    {
48 1
        $subject = $source;
49
        
50 1
        if ($ref === '$') {
51
            return $source;
52 1
        } elseif ($ref === '@') {
53
            return $target;
54
        }
55
        
56 1
        if (substr($ref, 0, 2) === '$.') {
57 1
            $ref = substr($ref, 2);
58 1
        } elseif (substr($ref, 0, 2) === '@.') {
59
            $ref = substr($ref, 2);
60
            $subject = $target;
61
        }
62
        
63 1
        return DotKey::on($subject)->get($ref);
64
    }
65
}
66