Skip to content

Commit 29b35e7

Browse files
committed
fix: Fix multiple off implementation
1 parent 80638e7 commit 29b35e7

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/JsonSchema/Constraints/Drafts/Draft06/MultipleOfConstraint.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
2929
return;
3030
}
3131

32-
if (fmod($value, $schema->multipleOf) === 0.0 || $this->isMultipleOf((string) $value, (string) $schema->multipleOf)) {
32+
if ($this->isMultipleOf($value, $schema->multipleOf)) {
3333
return;
3434
}
3535

3636
$this->addError(ConstraintError::MULTIPLE_OF(), $path, ['multipleOf' => $schema->multipleOf, 'found' => $value]);
3737
}
3838

39-
private function isMultipleOf(string $value, string $multipleOf): bool
39+
private function isMultipleOf($number1, $number2)
4040
{
41-
if (bccomp($multipleOf, '0', 20) === 0) {
42-
return false;
43-
}
41+
$modulus = ($number1 - round($number1 / $number2) * $number2);
42+
$precision = 0.0000000001;
4443

45-
$div = bcdiv($value, $multipleOf, 20);
44+
if (-$precision < $modulus && $modulus < $precision) {
45+
return true;
46+
}
4647

47-
return bccomp(bcmod($div, '1', 20), '0', 20) === 0;
48+
return false;
4849
}
4950
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JsonSchema\Constraints\Drafts\Draft06;
6+
7+
use JsonSchema\ConstraintError;
8+
use JsonSchema\Constraints\ConstraintInterface;
9+
use JsonSchema\Entity\ErrorBagProxy;
10+
use JsonSchema\Entity\JsonPointer;
11+
12+
class RefConstraint implements ConstraintInterface
13+
{
14+
use ErrorBagProxy;
15+
16+
/** @var Factory */
17+
private $factory;
18+
19+
public function __construct(?Factory $factory = null)
20+
{
21+
$this->factory = $factory ?: new Factory();
22+
$this->initialiseErrorBag($this->factory);
23+
}
24+
25+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
26+
{
27+
if (!property_exists($schema, '$ref')) {
28+
return;
29+
}
30+
31+
$refSchema = $this->factory->getSchemaStorage()->resolveRefSchema($schema);
32+
33+
$schemaConstraint = $this->factory->createInstanceFor('schema');
34+
$schemaConstraint->check($value, $refSchema, $path, $i);
35+
36+
if ($schemaConstraint->isValid()) {
37+
return;
38+
}
39+
40+
$this->addErrors($schemaConstraint->getErrors());
41+
}
42+
}

0 commit comments

Comments
 (0)