Skip to content

Commit

Permalink
constants are PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 4, 2022
1 parent 7a5bc2f commit 9189c65
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/Schema/Elements/AnyOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function normalize($value, Context $context)

public function merge($value, $base)
{
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
return $value;
}

Expand Down Expand Up @@ -112,7 +112,7 @@ public function complete($value, Context $context)
} else {
$context->addError(
'The %label% %path% expects to be %expected%, %value% given.',
Nette\Schema\Message::TYPE_MISMATCH,
Nette\Schema\Message::TypeMismatch,
[
'value' => $value,
'expected' => implode('|', array_unique($expecteds)),
Expand All @@ -127,7 +127,7 @@ public function completeDefault(Context $context)
if ($this->required) {
$context->addError(
'The mandatory item %path% is missing.',
Nette\Schema\Message::MISSING_ITEM
Nette\Schema\Message::MissingItem
);
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function completeDefault(Context $context)
if ($this->required) {
$context->addError(
'The mandatory item %path% is missing.',
Nette\Schema\Message::MISSING_ITEM
Nette\Schema\Message::MissingItem
);
return null;
}
Expand All @@ -109,7 +109,7 @@ private function doDeprecation(Context $context): void
if ($this->deprecated !== null) {
$context->addWarning(
$this->deprecated,
Nette\Schema\Message::DEPRECATED
Nette\Schema\Message::Deprecated
);
}
}
Expand All @@ -121,7 +121,7 @@ private function doValidate($value, string $expected, Context $context): bool
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
$context->addError(
'The %label% %path% expects to be %expected%, %value% given.',
Nette\Schema\Message::TYPE_MISMATCH,
Nette\Schema\Message::TypeMismatch,
['value' => $value, 'expected' => $expected]
);
return false;
Expand All @@ -143,15 +143,15 @@ private function doValidateRange($value, array $range, Context $context, string
if (!self::isInRange($length, $range)) {
$context->addError(
"The length of %label% %path% expects to be in range %expected%, %length% $label given.",
Nette\Schema\Message::LENGTH_OUT_OF_RANGE,
Nette\Schema\Message::LengthOutOfRange,
['value' => $value, 'length' => $length, 'expected' => implode('..', $range)]
);
return false;
}
} elseif ((is_int($value) || is_float($value)) && !self::isInRange($value, $range)) {
$context->addError(
'The %label% %path% expects to be in range %expected%, %value% given.',
Nette\Schema\Message::VALUE_OUT_OF_RANGE,
Nette\Schema\Message::ValueOutOfRange,
['value' => $value, 'expected' => implode('..', $range)]
);
return false;
Expand Down Expand Up @@ -188,7 +188,7 @@ private function doFinalize($value, Context $context)
$expected = $description ?: (is_string($handler) ? "$handler()" : "#$i");
$context->addError(
'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
Nette\Schema\Message::FAILED_ASSERTION,
Nette\Schema\Message::FailedAssertion,
['value' => $value, 'assertion' => $expected]
);
return;
Expand Down
12 changes: 6 additions & 6 deletions src/Schema/Elements/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function skipDefaults(bool $state = true): self

public function normalize($value, Context $context)
{
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
unset($value[Helpers::PREVENT_MERGING]);
if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
unset($value[Helpers::PreventMerging]);
}

$value = $this->doNormalize($value, $context);
Expand All @@ -107,7 +107,7 @@ public function normalize($value, Context $context)
}

if ($prevent) {
$value[Helpers::PREVENT_MERGING] = true;
$value[Helpers::PreventMerging] = true;
}
}

Expand All @@ -117,8 +117,8 @@ public function normalize($value, Context $context)

public function merge($value, $base)
{
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
$base = null;
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public function complete($value, Context $context)
$hint = Nette\Utils\ObjectHelpers::getSuggestion($keys, (string) $key);
$context->addError(
'Unexpected item %path%' . ($hint ? ", did you mean '%hint%'?" : '.'),
Nette\Schema\Message::UNEXPECTED_ITEM,
Nette\Schema\Message::UnexpectedItem,
['hint' => $hint]
)->path[] = $key;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Schema/Elements/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public function pattern(?string $pattern): self

public function normalize($value, Context $context)
{
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
unset($value[Helpers::PREVENT_MERGING]);
if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
unset($value[Helpers::PreventMerging]);
}

$value = $this->doNormalize($value, $context);
Expand All @@ -134,7 +134,7 @@ public function normalize($value, Context $context)
}

if ($prevent && is_array($value)) {
$value[Helpers::PREVENT_MERGING] = true;
$value[Helpers::PreventMerging] = true;
}

return $value;
Expand All @@ -143,8 +143,8 @@ public function normalize($value, Context $context)

public function merge($value, $base)
{
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
return $value;
}

Expand All @@ -171,8 +171,8 @@ public function merge($value, $base)
public function complete($value, Context $context)
{
$merge = $this->merge;
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
$merge = false;
}

Expand All @@ -191,7 +191,7 @@ public function complete($value, Context $context)
if ($value !== null && $this->pattern !== null && !preg_match("\x01^(?:$this->pattern)$\x01Du", $value)) {
$context->addError(
"The %label% %path% expects to match pattern '%pattern%', %value% given.",
Nette\Schema\Message::PATTERN_MISMATCH,
Nette\Schema\Message::PatternMismatch,
['value' => $value, 'pattern' => $this->pattern]
);
return;
Expand Down
7 changes: 4 additions & 3 deletions src/Schema/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ final class Helpers
{
use Nette\StaticClass;

public const PREVENT_MERGING = '_prevent_merging';
public const PreventMerging = '_prevent_merging';
public const PREVENT_MERGING = self::PreventMerging;


/**
Expand All @@ -29,8 +30,8 @@ final class Helpers
*/
public static function merge($value, $base)
{
if (is_array($value) && isset($value[self::PREVENT_MERGING])) {
unset($value[self::PREVENT_MERGING]);
if (is_array($value) && isset($value[self::PreventMerging])) {
unset($value[self::PreventMerging]);
return $value;
}

Expand Down
26 changes: 18 additions & 8 deletions src/Schema/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,38 @@ final class Message
use Nette\SmartObject;

/** variables: {value: mixed, expected: string} */
public const TYPE_MISMATCH = 'schema.typeMismatch';
public const TypeMismatch = 'schema.typeMismatch';

/** variables: {value: mixed, expected: string} */
public const VALUE_OUT_OF_RANGE = 'schema.valueOutOfRange';
public const ValueOutOfRange = 'schema.valueOutOfRange';

/** variables: {value: mixed, length: int, expected: string} */
public const LENGTH_OUT_OF_RANGE = 'schema.lengthOutOfRange';
public const LengthOutOfRange = 'schema.lengthOutOfRange';

/** variables: {value: string, pattern: string} */
public const PATTERN_MISMATCH = 'schema.patternMismatch';
public const PatternMismatch = 'schema.patternMismatch';

/** variables: {value: mixed, assertion: string} */
public const FAILED_ASSERTION = 'schema.failedAssertion';
public const FailedAssertion = 'schema.failedAssertion';

/** no variables */
public const MISSING_ITEM = 'schema.missingItem';
public const MissingItem = 'schema.missingItem';

/** variables: {hint: string} */
public const UNEXPECTED_ITEM = 'schema.unexpectedItem';
public const UnexpectedItem = 'schema.unexpectedItem';

/** no variables */
public const DEPRECATED = 'schema.deprecated';
public const Deprecated = 'schema.deprecated';

/** Deprecated */
public const TYPE_MISMATCH = self::TypeMismatch;
public const VALUE_OUT_OF_RANGE = self::ValueOutOfRange;
public const LENGTH_OUT_OF_RANGE = self::LengthOutOfRange;
public const PATTERN_MISMATCH = self::PatternMismatch;
public const FAILED_ASSERTION = self::FailedAssertion;
public const MISSING_ITEM = self::MissingItem;
public const UNEXPECTED_ITEM = self::UnexpectedItem;
public const DEPRECATED = self::Deprecated;

/** @var string */
public $message;
Expand Down
4 changes: 2 additions & 2 deletions tests/Schema/Expect.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test('merging', function () {
'arr' => ['newitem'],
],
(new Processor)->process($schema, [
Helpers::PREVENT_MERGING => true,
Helpers::PreventMerging => true,
'key1' => 'newval',
'key3' => 'newval',
'newval3',
Expand All @@ -125,7 +125,7 @@ test('merging', function () {
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => [Helpers::PREVENT_MERGING => true, 'newitem'],
'arr' => [Helpers::PreventMerging => true, 'newitem'],
])
);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Schema/Helpers.merge.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require __DIR__ . '/../bootstrap.php';
$obj = new stdClass;
$arr1 = ['a' => 'b', 'x'];
$arr2 = ['c' => 'd', 'y'];
$arr3 = [Helpers::PREVENT_MERGING => true, 'c' => 'd', 'y'];
$arr3 = [Helpers::PreventMerging => true, 'c' => 'd', 'y'];


Assert::same(null, Helpers::merge(null, null));
Expand Down Expand Up @@ -46,7 +46,7 @@ Assert::same($arr2, Helpers::merge($arr2, []));
Assert::same(['a' => 'b', 'x', 'c' => 'd', 'y'], Helpers::merge($arr2, $arr1));
Assert::same(['c' => 'd', 'y'], Helpers::merge($arr3, $arr1));
Assert::same(['inner' => ['c' => 'd', 'y']], Helpers::merge(['inner' => $arr3], ['inner' => $arr1]));
Assert::same([['a' => 'b', 'x'], [Helpers::PREVENT_MERGING => true, 'c' => 'd', 'y']], Helpers::merge([$arr3], [$arr1]));
Assert::same([Helpers::PREVENT_MERGING => true, 'c' => 'd', 'y', 'a' => 'b', 'x'], Helpers::merge($arr1, $arr3));
Assert::same([['a' => 'b', 'x'], [Helpers::PreventMerging => true, 'c' => 'd', 'y']], Helpers::merge([$arr3], [$arr1]));
Assert::same([Helpers::PreventMerging => true, 'c' => 'd', 'y', 'a' => 'b', 'x'], Helpers::merge($arr1, $arr3));
Assert::same([20 => 'b', 10 => 'a'], Helpers::merge([10 => 'a'], [20 => 'b']));
Assert::same(['b', 'a'], Helpers::merge(['a'], ['b']));
2 changes: 1 addition & 1 deletion tests/Schema/Processor.context.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('', function () {
[
new Nette\Schema\Message(
'The mandatory item %path% is missing.',
Nette\Schema\Message::MISSING_ITEM,
Nette\Schema\Message::MissingItem,
['first', 'r'],
['isKey' => false]
),
Expand Down

0 comments on commit 9189c65

Please sign in to comment.