Skip to content

Commit 4e26a93

Browse files
committed
fix: Fix multiple off implementation
1 parent 80638e7 commit 4e26a93

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
class Draft06Constraint extends Constraint
1414
{
15-
public function __construct(\JsonSchema\Constraints\Factory $factory = null)
15+
public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
1616
{
1717

1818
parent::__construct(new Factory(
1919
$factory ? $factory->getSchemaStorage() : new SchemaStorage(),
2020
$factory ? $factory->getUriRetriever() : new UriRetriever(),
21-
$factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL,
21+
$factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL
2222
));
2323
}
2424

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)