Skip to content

Commit

Permalink
Merge pull request #129 from seboettg/feature/116-csif-not-working-if…
Browse files Browse the repository at this point in the history
…-type-and-variable-set-and-match=none

#116 cs:if not working if type and variable set and match="none"
  • Loading branch information
seboettg authored May 23, 2022
2 parents 4a0df22 + 09d78cc commit e162a8b
Show file tree
Hide file tree
Showing 21 changed files with 1,720 additions and 199 deletions.
78 changes: 46 additions & 32 deletions src/Constraint/AbstractConstraint.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php /** @noinspection PhpUnused */
<?php
declare(strict_types=1);
/*
* citeproc-php: AbstractConstraint.php
* User: Sebastian Böttger <sebastian.boettger@thomascook.de>
* created at 26.10.19, 14:12
* @link http://github.com/seboettg/citeproc-php for the source repository
* @copyright Copyright (c) 2019 Sebastian Böttger.
* @license https://opensource.org/licenses/MIT
*/

namespace Seboettg\CiteProc\Constraint;

use Seboettg\Collection\ArrayList;
use stdClass;

/**
* Class AbstractConstraint
* @package Seboettg\CiteProc\Constraint
* @noinspection PhpUnused
*/
abstract class AbstractConstraint implements Constraint
{

Expand All @@ -23,7 +20,7 @@ abstract class AbstractConstraint implements Constraint
protected $match;

/**
* @var array
* @var ArrayList\ArrayListInterface
*/
protected $conditionVariables;

Expand All @@ -32,53 +29,70 @@ abstract class AbstractConstraint implements Constraint
* @param stdClass $data;
* @return bool
*/
abstract protected function matchForVariable($variable, $data);
abstract protected function matchForVariable(string $variable, stdClass $data): bool;

/**
* Variable constructor.
* @param string $value
* @param string $variableValues
* @param string $match
*/
/** @noinspection PhpUnused */
public function __construct($value, $match = "any")
public function __construct(string $variableValues, string $match = "any")
{
$this->conditionVariables = explode(" ", $value);
$this->conditionVariables = new ArrayList(...explode(" ", $variableValues));
$this->match = $match;
}

/**
* @param $value
* @param stdClass $data
* @param int|null $citationNumber
* @return bool
*/
public function validate($value, $citationNumber = null)
public function validate(stdClass $data, int $citationNumber = null): bool
{
switch ($this->match) {
case Constraint::MATCH_ALL:
return $this->matchAll($value);
return $this->matchAll($data);
case Constraint::MATCH_NONE:
return !$this->matchAny($value); //no match for any value
return $this->matchNone($data); //no match for any value
case Constraint::MATCH_ANY:
default:
return $this->matchAny($value);
return $this->matchAny($data);
}
}

private function matchAny($value)
private function matchAny(stdClass $data): bool
{
$conditionMatched = false;
foreach ($this->conditionVariables as $variable) {
$conditionMatched |= $this->matchForVariable($variable, $value);
}
return (bool)$conditionMatched;
return $this->conditionVariables
->map(function (string $conditionVariable) use ($data) {
return $this->matchForVariable($conditionVariable, $data);
})
->filter(function (bool $match) {
return $match === true;
})
->count() > 0;
}

private function matchAll($value)
private function matchAll(stdClass $data): bool
{
$conditionMatched = true;
foreach ($this->conditionVariables as $variable) {
$conditionMatched &= $this->matchForVariable($variable, $value);
}
return (bool)$conditionMatched;
return $this->conditionVariables
->map(function (string $conditionVariable) use ($data) {
return $this->matchForVariable($conditionVariable, $data);
})
->filter(function (bool $match) {
return $match === true;
})
->count() === $this->conditionVariables->count();
}

private function matchNone(stdClass $data): bool
{
return $this->conditionVariables
->map(function (string $conditionVariable) use ($data) {
return $this->matchForVariable($conditionVariable, $data);
})
->filter(function (bool $match) {
return $match === false;
})
->count() === $this->conditionVariables->count();
}
}
9 changes: 6 additions & 3 deletions src/Constraint/Constraint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand All @@ -9,6 +10,8 @@

namespace Seboettg\CiteProc\Constraint;

use stdClass;

/**
* Interface ConstraintInterface
* @package Seboettg\CiteProc\Constraint
Expand All @@ -25,9 +28,9 @@ interface Constraint
const MATCH_ALL = "all";

/**
* @param $value
* @param null $citationNumber
* @param stdClass $data
* @param int|null $citationNumber
* @return bool
*/
public function validate($value, $citationNumber = null);
public function validate(stdClass $data, int $citationNumber = null): bool;
}
14 changes: 5 additions & 9 deletions src/Constraint/Disambiguate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand All @@ -9,27 +10,22 @@

namespace Seboettg\CiteProc\Constraint;

use stdClass;

/**
* Class Disambiguate
* When set totrue” (the only allowed value), the element content is only rendered if it disambiguates two otherwise
* identical citations. This attempt at disambiguation is only made when all other disambiguation methods have failed
* to uniquely identify the target source.
*
* @package Seboettg\CiteProc\Constraint
*
* @author Sebastian Böttger <seboettg@gmail.com>
*
*/
/** @noinspection PhpUnused */
class Disambiguate implements Constraint
{
/**
* @codeCoverageIgnore
* @param $value
* @param stdClass $data
* @param int|null $citationNumber
* @return bool
*/
public function validate($value, $citationNumber = null)
public function validate(stdClass $data, $citationNumber = null): bool
{
return false;
}
Expand Down
13 changes: 2 additions & 11 deletions src/Constraint/Factory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand All @@ -12,24 +13,14 @@
use Seboettg\CiteProc\Exception\ClassNotFoundException;
use function Seboettg\CiteProc\ucfirst;

/**
* Class Factory
* @package Seboettg\CiteProc\Constraint
*
* @author Sebastian Böttger <seboettg@gmail.com>
*/
class Factory extends \Seboettg\CiteProc\Util\Factory
{
const NAMESPACE_CONSTRAINTS = "Seboettg\\CiteProc\\Constraint\\";

/**
* @param string $name
* @param string $value
* @param string $match
* @return mixed
* @throws ClassNotFoundException
*/
public static function createConstraint(string $name, string $value, string $match)
public static function createConstraint(string $name, string $value, string $match): Constraint
{
$parts = explode("-", $name);
$className = implode("", array_map(function ($part) {
Expand Down
5 changes: 3 additions & 2 deletions src/Constraint/IsNumeric.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand Down Expand Up @@ -29,7 +30,7 @@ class IsNumeric extends AbstractConstraint
* @param stdClass $data
* @return bool
*/
protected function matchForVariable($variable, $data)
protected function matchForVariable(string $variable, stdClass $data): bool
{
if (isset($data->{$variable})) {
return $this->parseValue($data->{$variable});
Expand All @@ -46,7 +47,7 @@ protected function matchForVariable($variable, $data)
* @param $evalValue
* @return bool
*/
private function parseValue($evalValue)
private function parseValue($evalValue): bool
{
if (is_numeric($evalValue)) {
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/Constraint/IsUncertainDate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand Down Expand Up @@ -28,7 +29,7 @@ class IsUncertainDate extends AbstractConstraint
* @param stdClass $data ;
* @return bool
*/
protected function matchForVariable($variable, $data)
protected function matchForVariable(string $variable, stdClass $data): bool
{
if (!empty($data->{$variable})) {
if (isset($data->{$variable}->{'circa'})) {
Expand Down
8 changes: 5 additions & 3 deletions src/Constraint/Jurisdiction.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand All @@ -9,22 +10,23 @@

namespace Seboettg\CiteProc\Constraint;

use stdClass;

/**
* Class Jurisdiction
* @package Seboettg\CiteProc\Constraint
*
* @author Sebastian Böttger <seboettg@gmail.com>
*/
/** @noinspection PhpUnused */
class Jurisdiction implements Constraint
{
/**
* @codeCoverageIgnore
* @param $value
* @param stdClass $data
* @param int|null $citationNumber
* @return bool
*/
public function validate($value, $citationNumber = null)
public function validate(stdClass $data, int $citationNumber = null): bool
{
return false;
}
Expand Down
11 changes: 3 additions & 8 deletions src/Constraint/Locator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* citeproc-php
*
Expand All @@ -10,26 +11,20 @@
namespace Seboettg\CiteProc\Constraint;

use Seboettg\CiteProc\CiteProc;
use stdClass;

/**
* Class Locator
*
* Tests whether the locator matches the given locator types (see Locators). Usesub-verboto test for the
*sub verbolocator type.
*
* @codeCoverageIgnore
*
* @package Seboettg\CiteProc\Constraint
*
* @author Sebastian Böttger <seboettg@gmail.com>
*/
/** @noinspection PhpUnused */
class Locator extends AbstractConstraint
{
/**
* @inheritDoc
*/
protected function matchForVariable($variable, $data)
protected function matchForVariable(string $variable, stdClass $data): bool
{
if (!empty($data->id)) {
$citationItem = CiteProc::getContext()->getCitationItemById($data->id);
Expand Down
Loading

0 comments on commit e162a8b

Please sign in to comment.