Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Produce results in response of rule evaluations #95

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Exception/NoResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Hoa\Ruler\Exception;

class NoResult extends Exception
{
}
6 changes: 6 additions & 0 deletions Exception/RuleDoesNotValidate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Hoa\Ruler\Exception;

class RuleDoesNotValidate extends Exception
{
}
105 changes: 105 additions & 0 deletions Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2016, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

namespace Hoa\Ruler;


/**
* @property string name
* @property Rule rule
* @property mixed $result
*/
class Result
{
/**
* @var string
*/
private $name;

/**
* @var Rule
*/
private $rule;

/**
* @var mixed
*/
private $result;

/**
* @param string $name
* @param Rule $rule
* @param mixed $result
*/
public function __construct($name, Rule $rule, $result)
{
$this->name = $name;
$this->rule = $rule;
$this->result = $result;
}

/**
* @param string $name
*
* @throws \Error
*
* @return mixed
*/
public function __get($name)
{
switch (strtolower($name)) {
case 'name':
return $name;

case 'result':
if (is_object($this->result)) {
if ($this->result instanceof \Closure) {
return $this->result;
}

return clone $this->result;
}

return $this->result;

case 'rule':
return clone $this->rule;

default:
throw new Exception\Exception('Unable to get property ' . $name);
}
}
}
62 changes: 62 additions & 0 deletions Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2016, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

namespace Hoa\Ruler;

/**
* Interface \Hoa\Ruler\Rule.
*
* @copyright Copyright © 2007-2016 Hoa community
* @license New BSD License
*/
interface Rule
{
/**
* @param Ruler $ruler
* @param Context $context
*
* @return bool
*/
public function valid(Ruler $ruler, Context $context);

/**
* @param Ruler $ruler
* @param Context $context
*
* @return mixed
*/
public function execute(Ruler $ruler, Context $context);
}
160 changes: 160 additions & 0 deletions Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2016, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

namespace Hoa\Ruler;

use Hoa\Heap\Max;
use Hoa\Ruler\Exception\NoResult;
use Hoa\Ruler\Exception\RuleDoesNotValidate;

/**
* Class \Hoa\Ruler\Rules.
*
* A collection of rules ordered by priority.
*
* @copyright Copyright © 2007-2016 Hoa community
* @license New BSD License
*/
class Rules
{
/**
* @var Max
*/
private $queue;

public function __construct()
{
$this->queue = new Max();
}

/**
* @param string $name
* @param Rule $rule
* @param int $priority
*
* @return Rules
*/
public function add($name, Rule $rule, $priority = -1)
{
$this->queue->insert([$name, $rule], $priority);

return $this;
}

/**
* @param Ruler $ruler
* @param Context $context
*
* @throws NoResult
*
* @return Result
*/
public function getBestResult(Ruler $ruler, Context $context)
{
$ruler = self::initializeRuler($ruler, $context);

foreach ($this->queue as $nameAndRule) {
/**
* @var string $name
* @var Rule $rule
*/
list($name, $rule) = $nameAndRule;

try {
$context['.' . $name] = $rule->execute($ruler, $context);
} catch (RuleDoesNotValidate $exception) {
$context['.' . $name] = null;
}

if ($rule->valid($ruler, $context)) {
return new Result($name, $rule, $context['.' . $name]);
}
}

throw new NoResult();
}

/**
* @param Ruler $ruler
* @param Context $context
*
* @return Result[]
*/
public function getAllResults(Ruler $ruler, Context $context)
{
$ruler = self::initializeRuler($ruler, $context);
$results = [];

foreach ($this->queue as $nameAndRule) {
/**
* @var string $name
* @var Rule $rule
*/
list($name, $rule) = $nameAndRule;

try {
$context['.' . $name] = $rule->execute($ruler, $context);
} catch (RuleDoesNotValidate $exception) {
$context['.' . $name] = null;
}

$results[] = new Result($name, $rule, $context['.' . $name]);
}

return $results;
}

/**
* @param Ruler $ruler
* @param array $context
*
* @return Ruler
*/
private static function initializeRuler(Ruler $ruler, Context $context)
{
if ($ruler->getDefaultAsserter()->operatorExists('rule')) {
return $ruler;
}

$ruler = clone $ruler;

$ruler->getDefaultAsserter()->setOperator('rule', function($id) use ($context) {
return $context['.' . $id];
});

return $ruler;
}
}
Loading