Skip to content

Commit

Permalink
Merge pull request #5 from legalthings/nodes
Browse files Browse the repository at this point in the history
Added nodes
  • Loading branch information
jasny committed May 27, 2016
2 parents 035c594 + 4822c8d commit bbe4caa
Show file tree
Hide file tree
Showing 16 changed files with 535 additions and 380 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/composer.lock
/build
/nbproject/*
83 changes: 74 additions & 9 deletions src/DataEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace LegalThings;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;

/**
* Enrich objects by processing special properties.
*/
Expand All @@ -12,12 +15,12 @@ class DataEnricher
* @var array
*/
public static $defaultProcessors = [
'_ref' => 'Reference',
'_switch' => 'SwitchChoose',
'_src' => 'Http',
'_merge' => 'Merge',
'_jmespath' => 'JmesPath',
'_tpl' => 'Mustache'
'_ref' => Processor\Reference::class,
'_switch' => Processor\SwitchChoose::class,
'_src' => Processor\Http::class,
'_merge' => Processor\Merge::class,
'_jmespath' => Processor\JmesPath::class,
'_tpl' => Processor\Mustache::class
];


Expand Down Expand Up @@ -49,7 +52,7 @@ public function __construct($source)

foreach (static::$defaultProcessors as $property => $processor) {
if (is_string($processor)) {
$class = $processor[0] === '\\' ? substr($processor, 1) : __CLASS__ . '\\' . $processor;
$class = $processor;
$processor = new $class($this, $property);
}

Expand All @@ -75,13 +78,75 @@ public function getSource()
*/
public function applyTo($target)
{
if (is_string($target)) $target = \DotKey::on($this->source)->get($target);
if (is_string($target)) {
$target = \DotKey::on($this->source)->get($target);
}

$nodes = $this->findNodes($target);

foreach ($this->processors as $processor) {
$processor->applyTo($target);
$processor->applyTo($nodes);
}

$this->applyNodeResults($target);
}

/**
* Find nodes that have processing instructions
*
* @param array|object $target
* @return array
*/
public function findNodes(&$target)
{
$nodes = [];

foreach ($target as $key => &$value) {
if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
$nodes = array_merge($nodes, $this->findNodes($value));
}

if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
$value = new Node($value);
$nodes[] = $value;
}
}

return $nodes;
}

/**
* Check if object has at leas one process property
*
* @param \stdClass $value
* @return boolean
*/
protected function hasProcessorProperty($value)
{
$processorProps = array_map(function ($processor) {
return $processor->getProperty();
}, $this->processors);

$valueProps = array_keys(get_object_vars($value));

return count(array_intersect($valueProps, $processorProps)) > 0;
}

/**
* Replace nodes with their results
*
* @param array|object $target
*/
public function applyNodeResults(&$target)
{
foreach ($target as &$value) {
if ($value instanceof Node) {
$value = $value->getResult();
} elseif (is_array($value) || is_object($value)) {
$this->applyNodeResults($value);
}
}
}

/**
* Enrich object
Expand Down
143 changes: 0 additions & 143 deletions src/DataEnricher/Http.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/DataEnricher/JmesPath.php

This file was deleted.

64 changes: 0 additions & 64 deletions src/DataEnricher/Merge.php

This file was deleted.

Loading

0 comments on commit bbe4caa

Please sign in to comment.