Skip to content

Commit b907fb1

Browse files
committed
Allow options to be marked as deprecated [Closes #27]
1 parent 58cd31f commit b907fb1

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ $processor->process($schema, ['additional' => 1]); // OK
257257
$processor->process($schema, ['additional' => true]); // ERROR
258258
```
259259

260+
Deprecations
261+
------------
262+
263+
You can deprecate property using the `deprecated([string $message])` method. Deprecation notices are returned by `$processor->getWarnings()`.
264+
265+
```php
266+
$schema = Expect::structure([
267+
'old' => Expect::int()->deprecated('The option %path% is deprecated'),
268+
]);
269+
270+
$processor->process($schema, ['old' => 1]); // OK
271+
$processor->getWarnings(); // ["The option 'old' is deprecated"]
272+
```
273+
260274
Ranges: min() max()
261275
-------------------
262276

src/Schema/Elements/Base.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ trait Base
3333
/** @var string|null */
3434
private $castTo;
3535

36+
/** @var string|null */
37+
private $deprecated;
38+
3639

3740
public function default($value): self
3841
{
@@ -69,6 +72,14 @@ public function assert(callable $handler, string $description = null): self
6972
}
7073

7174

75+
/** Marks option as deprecated */
76+
public function deprecated(string $message = 'Option %path% is deprecated.'): self
77+
{
78+
$this->deprecated = $message;
79+
return $this;
80+
}
81+
82+
7283
public function completeDefault(Context $context)
7384
{
7485
if ($this->required) {
@@ -95,7 +106,6 @@ private function doValidate($value, string $expected, Context $context): bool
95106
{
96107
try {
97108
Nette\Utils\Validators::assert($value, $expected, 'option %path%');
98-
return true;
99109
} catch (Nette\Utils\AssertionException $e) {
100110
$context->addError(
101111
$e->getMessage(),
@@ -104,6 +114,14 @@ private function doValidate($value, string $expected, Context $context): bool
104114
);
105115
return false;
106116
}
117+
118+
if ($this->deprecated !== null) {
119+
$context->addWarning(
120+
$this->deprecated,
121+
Nette\Schema\Message::DEPRECATED
122+
);
123+
}
124+
return true;
107125
}
108126

109127

src/Schema/Message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ final class Message
3131
/** variables: {hint: string} */
3232
public const UNEXPECTED_KEY = 'schema.unexpectedKey';
3333

34+
/** no variables */
35+
public const DEPRECATED = 'schema.deprecated';
36+
3437
/** @var string */
3538
public $message;
3639

tests/Schema/Expect.anyOf.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ test('required & nullable', function () {
159159
});
160160

161161

162+
test('deprecated item', function () {
163+
$schema = Expect::anyOf('one', true, Expect::int()->deprecated());
164+
165+
$processor = new Processor;
166+
Assert::same('one', $processor->process($schema, 'one'));
167+
Assert::same([], $processor->getWarnings());
168+
169+
Assert::same(true, $processor->process($schema, true));
170+
Assert::same([], $processor->getWarnings());
171+
172+
Assert::same(123, $processor->process($schema, 123));
173+
Assert::same(['Option is deprecated.'], $processor->getWarnings());
174+
});
175+
176+
162177
test('nullable anyOf', function () {
163178
$schema = Expect::anyOf(Expect::string(), true)->nullable();
164179

tests/Schema/Expect.structure.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,31 @@ test('processing without default values', function () {
426426
$processor->process($schema, ['d' => 'newval'])
427427
);
428428
});
429+
430+
431+
test('deprecated item', function () {
432+
$schema = Expect::structure([
433+
'b' => Expect::string()->deprecated('depr %path%'),
434+
]);
435+
436+
$processor = new Processor;
437+
Assert::equal(
438+
(object) ['b' => 'val'],
439+
$processor->process($schema, ['b' => 'val'])
440+
);
441+
Assert::same(["depr 'b'"], $processor->getWarnings());
442+
});
443+
444+
445+
test('deprecated other items', function () {
446+
$schema = Expect::structure([
447+
'key' => Expect::string(),
448+
])->otherItems(Expect::string()->deprecated());
449+
450+
$processor = new Processor;
451+
Assert::equal((object) ['key' => null], $processor->process($schema, []));
452+
Assert::same([], $processor->getWarnings());
453+
454+
Assert::equal((object) ['key' => null, 'other' => 'foo'], $processor->process($schema, ['other' => 'foo']));
455+
Assert::same(["Option 'other' is deprecated."], $processor->getWarnings());
456+
});

0 commit comments

Comments
 (0)