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

src/DataEnricher.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace LegalThings;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use Jasny\DotKey;
8
9
/**
10
 * Enrich objects by processing special properties.
11
 */
12
class DataEnricher
13
{
14
    /**
15
     * Default processors
16
     * @var array
17
     */
18
    public static $defaultProcessors = [
19
        '<ifset>' => Processor\IfSet::class,
20
        '<ref>' => Processor\Reference::class,
21
        '<switch>' => Processor\SwitchChoose::class,
22
        '<merge>' => Processor\Merge::class,
23
        '<tpl>' => Processor\Mustache::class,
24
        '<src>' => Processor\Http::class,
25
        '<jmespath>' => Processor\JmesPath::class,
26
        '<transformation>' => Processor\Transform::class,
27
        '<math>' => Processor\Math::class,
28
        '<enrich>' => Processor\Enrich::class,
29
        '<dateformat>' => Processor\DateFormat::class,
30
        
31
        // Deprecated
32
        '_ref' => Processor\Reference::class,
33
        '_switch' => Processor\SwitchChoose::class,
34
        '_src' => Processor\Http::class,
35
        '_merge' => Processor\Merge::class,
36
        '_jmespath' => Processor\JmesPath::class,
37
        '_tpl' => Processor\Mustache::class,
38
        '_transformation' => Processor\Transform::class
39
    ];
40
    
41
    
42
    /**
43
     * Processors, applied in specified order.
44
     * 
45
     * @var DataEnricher\Processor[]
46
     */
47
    public $processors;
48
    
49
    
50
    /**
51
     * Class constructor
52
     */
53 3
    public function __construct()
54
    {
55 3
        foreach (static::$defaultProcessors as $property => $processor) {
56 3
            if (is_string($processor)) {
57 3
                $processor = new $processor($property);
58 3
            }
59
            
60 3
            $this->processors[] = $processor;
61 3
        }
62 3
    }
63
    
64
    /**
65
     * Create processors
66
     * 
67
     * @param object       $source  Data source
68
     * @param array|object $target  Target or dot key path
69
     * @return Processor[]
70
     */
71
    protected function getProcessorsFor($source, $target)
72
    {
73
        $processors = [];
74
        
75
        foreach ($this->processors as $processor) {
76
            $processors[] = $processor->withSourceAndTarget($source, $target);
77
        }
78
        
79
        return $processors;
80
    }
81
    
82
    
83
    /**
84
     * Apply processing instructions
85
     * 
86
     * @param array|object|string $target  Target or dot key path
87
     * @param object              $source  Data source
88
     */
89 2
    public function applyTo($target, $source = null)
90
    {
91 2
        if (!isset($source)) {
92 2
            $source = $target;
93 2
        }
94
        
95 2
        if (!is_object($source)) {
96 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
97
        }
98
        
99
        if (is_string($target)) {
100
            $target = DotKey::on($source)->get($target);
101
        }
102
        
103
        $nodes = $this->findNodes($target);
104
        $processors = $this->getProcessorsFor($source, $target);
105
        
106
        foreach ($nodes as $node) {
107
            foreach ($processors as $processor) {
108
                $node->apply($processor);
109
            }
110
        }
111
        
112
        $this->applyNodeResults($target);
113
    }
114
115
    /**
116
     * Find nodes that have processing instructions
117
     * 
118
     * @param array|object $target
119
     * @return array
120
     */
121
    public function findNodes(&$target)
122
    {
123
        $nodes = [];
124
        
125
        foreach ($target as $key => &$value) {
126
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
127
                $nodes = array_merge($nodes, $this->findNodes($value));
128
            }
129
            
130
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
131
                $value = new Node($value);
132
                $nodes[] = $value;
133
            }
134
        }
135
        
136
        return $nodes;
137
    }
138
    
139
    /**
140
     * Check if object has at leas one process property
141
     * 
142
     * @param \stdClass    $value
143
     * @param Processor[]  $processors
144
     * @return boolean
145
     */
146
    protected function hasProcessorProperty($value)
147
    {
148
        $processorProps = array_map(function ($processor) {
149
            return $processor->getProperty();
150
        }, $this->processors);
151
        
152
        $valueProps = array_keys(get_object_vars($value));
153
        return count(array_intersect($valueProps, $processorProps)) > 0;
154
    }
155
    
156
    /**
157
     * Replace nodes with their results
158
     * 
159
     * @param array|object $target
160
     */
161 View Code Duplication
    protected function applyNodeResults(&$target)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        foreach ($target as &$value) {
164
            if ($value instanceof Node) {
165
                $value = $value->getResult();
166
            } elseif (is_array($value) || is_object($value)) {
167
                $this->applyNodeResults($value);
168
            }
169
        }
170
    }
171
}
172