Skip to content

Commit

Permalink
Enricher schema (#32)
Browse files Browse the repository at this point in the history
* jmespath is now called apply and accepts different properties

* Ref processor uses jmespath

* Add equal processor

* Add match enricher

* Add if enricher

* Add join enricher

* Added replace enricher

* Renamed date property of dateformat to input

* Switch enricher uses on and options properties

* Enrich processor uses an input property

* Add encode enricher

* Add decode enricher

* Add serialize and unserialize enrichers

* Add default for switch

* Add numberformat enricher

* Add hash enricher with hmac support

* Use legalthings/jmespath.php

* Add sum processor

* Fix ref with jmespath

* Fix crc32 hash
  • Loading branch information
moesjarraf authored Apr 3, 2018
1 parent 0bbe18e commit 211e209
Show file tree
Hide file tree
Showing 38 changed files with 1,750 additions and 38 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
"issues": "https://github.com/legalthings/data-enricher/issues",
"source": "https://github.com/legalthings/data-enricher"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/legalthings/jmespath.php"
}
],
"require": {
"php": ">=5.5.0",
"mtdowling/jmespath.php": "^2.2",
"mtdowling/jmespath.php": "dev-master#8ec2d692c59e45184d16b4c9160e902397cfc659",
"guzzlehttp/guzzle": "^6.0",
"jasny/dotkey": "^1.0",
"mustache/mustache": "^2.10",
"hoa/Math": "1.16.08.29"
"hoa/Math": "1.16.08.29",
"stephenhill/base58": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
Expand Down
12 changes: 12 additions & 0 deletions src/DataEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ class DataEnricher
'<tpl>' => Processor\Mustache::class,
'<src>' => Processor\Http::class,
'<jmespath>' => Processor\JmesPath::class,
'<apply>' => Processor\JmesPath::class,
'<transformation>' => Processor\Transform::class,
'<math>' => Processor\Math::class,
'<enrich>' => Processor\Enrich::class,
'<dateformat>' => Processor\DateFormat::class,
'<numberformat>' => Processor\NumberFormat::class,
'<equal>' => Processor\Equal::class,
'<match>' => Processor\Match::class,
'<replace>' => Processor\Replace::class,
'<if>' => Processor\IfElse::class,
'<join>' => Processor\Join::class,
'<encode>' => Processor\Encode::class,
'<decode>' => Processor\Decode::class,
'<serialize>' => Processor\Serialize::class,
'<unserialize>' => Processor\Unserialize::class,
'<hash>' => Processor\Hash::class,

// Deprecated
'_ref' => Processor\Reference::class,
Expand Down
7 changes: 4 additions & 3 deletions src/DataEnricher/Processor/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

if(!isset($instruction->date)) {
if(!isset($instruction->date) && !isset($instruction->input)) {
return;
}

Expand All @@ -30,9 +30,10 @@ public function applyToNode(Node $node)
$format = $instruction->format;
}

$date = new \DateTime($instruction->date);
$input = isset($instruction->input) ? $instruction->input : $instruction->date;
$date = new \DateTime($input);
if (isset($instruction->timezone)) {
$date = new \DateTime($instruction->date, new \DateTimeZone($instruction->timezone));
$date = new \DateTime($input, new \DateTimeZone($instruction->timezone));
}

$result = $date->format($format);
Expand Down
73 changes: 73 additions & 0 deletions src/DataEnricher/Processor/Decode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace LegalThings\DataEnricher\Processor;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;
use StephenHill\Base58;

/**
* Decode processor
*/
class Decode implements Processor
{
use Processor\Implementation;

/**
* Apply processing to a single node
*
* @param Node $node
*/
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

if (is_array($instruction) || is_object($instruction)) {
$instruction = (object)$instruction;
}

if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->algo)) {
return;
}

if (!method_exists($this, $instruction->algo)) {
return;
}

$result = call_user_func_array([$this, $instruction->algo], [$instruction->input]);

$node->setResult($result);
}


/**
* Base64 decode
*
* @param string $input
*/
public function base64($input)
{
return base64_decode($input);
}

/**
* Base58 decode
*
* @param string $input
*/
public function base58($input)
{
$base58 = new Base58();
return $base58->decode($input);
}

/**
* Url decode
*
* @param string $input
*/
public function url($input)
{
return urldecode($input);
}
}
73 changes: 73 additions & 0 deletions src/DataEnricher/Processor/Encode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace LegalThings\DataEnricher\Processor;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;
use StephenHill\Base58;

/**
* Encode processor
*/
class Encode implements Processor
{
use Processor\Implementation;

/**
* Apply processing to a single node
*
* @param Node $node
*/
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

if (is_array($instruction) || is_object($instruction)) {
$instruction = (object)$instruction;
}

if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->algo)) {
return;
}

if (!method_exists($this, $instruction->algo)) {
return;
}

$result = call_user_func_array([$this, $instruction->algo], [$instruction->input]);

$node->setResult($result);
}


/**
* Base64 encode
*
* @param string $input
*/
public function base64($input)
{
return base64_encode($input);
}

/**
* Base58 encode
*
* @param string $input
*/
public function base58($input)
{
$base58 = new Base58();
return $base58->encode($input);
}

/**
* Url encode
*
* @param string $input
*/
public function url($input)
{
return urlencode($input);
}
}
7 changes: 6 additions & 1 deletion src/DataEnricher/Processor/Enrich.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class Enrich implements Processor
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);
$source = $node->getResult() ?: [];

if (is_array($instruction) || is_object($instruction)) {
$instruction = (object)$instruction;
}

$source = isset($instruction->input) ? $instruction->input : ($node->getResult() ?: []);

if (is_string($instruction->match)) {
$match = ['extra' => $instruction->match, 'source' => $instruction->match];
Expand Down
31 changes: 31 additions & 0 deletions src/DataEnricher/Processor/Equal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LegalThings\DataEnricher\Processor;

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

/**
* Equal processor
*/
class Equal implements Processor
{
use Processor\Implementation;

/**
* Apply processing to a single node
*
* @param Node $node
*/
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

if (!is_array($instruction) || count($instruction) !== 2) {
$node->setResult(false);
}

// might want to improve this check for different types using a library
$node->setResult($instruction[0] == $instruction[1]);
}
}
108 changes: 108 additions & 0 deletions src/DataEnricher/Processor/Hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace LegalThings\DataEnricher\Processor;

use LegalThings\DataEnricher\Node;
use LegalThings\DataEnricher\Processor;
use StephenHill\Base58;

/**
* Hash processor
*/
class Hash implements Processor
{
use Processor\Implementation;

/**
* Apply processing to a single node
*
* @param Node $node
*/
public function applyToNode(Node $node)
{
$instruction = $node->getInstruction($this);

if (is_array($instruction) || is_object($instruction)) {
$instruction = (object)$instruction;
}

if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->algo)) {
return;
}

if (!method_exists($this, $instruction->algo)) {
return;
}

$hmac = isset($instruction->hmac) ? $instruction->hmac : null;
$result = call_user_func_array([$this, $instruction->algo], [$instruction->input, $hmac]);

$node->setResult($result);
}


/**
* md5 hash
*
* @param string $input
* @param string $hmac
*/
public function md5($input, $hmac = null)
{
return $hmac ?
hash_hmac('md5', $input, $hmac) :
hash('md5', $input);
}

/**
* sha1 hash
*
* @param string $input
* @param string $hmac
*/
public function sha1($input, $hmac = null)
{
return $hmac ?
hash_hmac('sha1', $input, $hmac) :
hash('sha1', $input);
}

/**
* sha256 hash
*
* @param string $input
* @param string $hmac
*/
public function sha256($input, $hmac = null)
{
return $hmac ?
hash_hmac('sha256', $input, $hmac) :
hash('sha256', $input);
}

/**
* sha512 hash
*
* @param string $input
* @param string $hmac
*/
public function sha512($input, $hmac = null)
{
return $hmac ?
hash_hmac('sha512', $input, $hmac) :
hash('sha512', $input);
}

/**
* crc32 hash
*
* @param string $input
* @param string $hmac
*/
public function crc32($input, $hmac = null)
{
return $hmac ?
hash_hmac('crc32', $input, $hmac) :
hash('crc32', $input);
}
}
Loading

0 comments on commit 211e209

Please sign in to comment.