Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
devtoolboxuk committed Jun 22, 2019
1 parent 9c83c01 commit 5bc7aa7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 29 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -20,6 +20,7 @@
"require": {
"php": ">=5.4.16",
"devtoolboxuk/soteria": "^2.1.6",
"devtoolboxuk/utilitybundle": "^1.0.12",
"devtoolboxuk/ip-address": "^1.0.1",
"devtoolboxuk/lists": "^1.0.0"
},
Expand Down
42 changes: 16 additions & 26 deletions src/AbstractCerberus.php → src/AbstractService.php
Expand Up @@ -3,8 +3,9 @@
namespace devtoolboxuk\cerberus;

use devtoolboxuk\soteria\SoteriaService;
use devtoolboxuk\utilitybundle\UtilityService;

abstract class AbstractCerberus
abstract class AbstractService
{

protected $options = [];
Expand All @@ -16,12 +17,22 @@ abstract class AbstractCerberus
protected $output;
protected $handlerName;


protected $arrayUtility;

public function __construct()
{
$this->soteria = new SoteriaService();
$this->initiateServices();
$this->setOptions();
}

protected function initiateServices()
{
$this->soteria = new SoteriaService();
$utilityService = new UtilityService();
$this->arrayUtility = $utilityService->arrays();
}

/**
* @TODO - Add pass in json file
* @param $file
Expand All @@ -31,10 +42,10 @@ public function setOptionsWithFile($file)
if ($this->isYamlLoaded()) {
$this->soteria->sanitise()->disinfect($file, 'url');
if ($this->soteria->sanitise()->result()->isValid()) {
$this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
} else {
if (file_exists($file)) {
$this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
}
}
}
Expand All @@ -48,27 +59,6 @@ private function isYamlLoaded()
return extension_loaded('yaml');
}

/**
* @param array $merged
* @param array $array2
* @return array
*/
private function arrayMergeRecursiveDistinct($merged = [], $array2 = [])
{
if (empty($array2)) {
return $merged;
}

foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}

/**
* @param $handler
*/
Expand Down Expand Up @@ -175,7 +165,7 @@ protected function getOptions()
public function setOptions($options = [])
{
$baseOptions = new BaseOptions();
$this->options = $this->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options);
$this->options = $this->utilityService->arrays()->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options);
}


Expand Down
57 changes: 54 additions & 3 deletions src/CerberusService.php
Expand Up @@ -3,28 +3,40 @@
namespace devtoolboxuk\cerberus;

use devtoolboxuk\cerberus\handlers\Handler;
use devtoolboxuk\utilitybundle\UtilityService;
use ReflectionClass;

class CerberusService extends AbstractCerberus implements CerberusInterface
class CerberusService extends AbstractService implements CerberusInterface
{
private static $instance = null;
protected $handlers = [];
protected $references = [];

public function __construct()
{
$this->utilityService = new UtilityService();
$this->arrayUtility = $this->utilityService->arrays();
$this->resetHandlers();
}


/**
* @return $this
*/
public function resetHandlers()
{
$this->references = [];
$this->handlers = [];
self::$instance = null;
$this->initiateServices();
return $this;
}

/**
* @param $method
* @param array $arguments
* @return $this
* @throws \Exception
*/
public function __call($method, $arguments = [])
{
$handlers = new Handler($arguments);
Expand All @@ -33,9 +45,13 @@ public function __call($method, $arguments = [])
return $this;
}

/**
* @param $handler
* @param null $reference
* @return $this
*/
public function pushHandler($handler, $reference = null)
{

$handler->setReference($reference);
array_unshift($this->handlers, $handler);
$this->clearResults();
Expand All @@ -50,14 +66,29 @@ public function toArray()
{
return $this->process()->toArray();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function getJsonLogs()
{
return $this->process()->getJsonLogs();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function getArrayLogs()
{
return $this->process()->getArrayLogs();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function getReferences()
{
return $this->process()->getReferences();
Expand All @@ -82,26 +113,46 @@ public function process()
return self::$instance;
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function outputs()
{
return $this->process()->getOutputs();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function inputs()
{
return $this->process()->getInputs();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function hasScore()
{
return $this->process()->hasScore();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function getResult()
{
return $this->process()->getResult();
}

/**
* @return mixed
* @throws \ReflectionException
*/
public function getScore()
{
return $this->process()->getScore();
Expand Down

0 comments on commit 5bc7aa7

Please sign in to comment.