@@ -43,8 +43,9 @@ of them to type-safe objects.
43
43
- [All fields are required](#all-fields-are-required)
44
44
- [No fields are required](#no-fields-are-required)
45
45
- [ Callbacks] ( #callbacks )
46
- - [Mapped object callbacks](#mapped-object-callbacks)
47
- - [Field callbacks](#field-callbacks)
46
+ - [Validation object callbacks](#validation-object-callbacks)
47
+ - [After mapping object callbacks](#after-mapping-object-callbacks)
48
+ - [Validation field callbacks](#validation-field-callbacks)
48
49
- [Returned value](#returned-value)
49
50
- [Context](#callback-context)
50
51
- [ Dependencies] ( #dependencies )
@@ -1223,7 +1224,7 @@ use Orisai\ObjectMapper\Rules\MixedValue;
1223
1224
final class ListOfInput implements MappedObject
1224
1225
{
1225
1226
1226
- /** @var list<int , mixed > */
1227
+ /** @var list<mixed > */
1227
1228
#[ListOf(new MixedValue())]
1228
1229
public array $field;
1229
1230
@@ -1253,7 +1254,7 @@ final class ListOfInput implements MappedObject
1253
1254
{
1254
1255
1255
1256
/**
1256
- * @var list<int , mixed >
1257
+ * @var list<mixed >
1257
1258
* @ListOf(
1258
1259
* @MixedValue(),
1259
1260
* )
@@ -2224,13 +2225,13 @@ final class WithCallbackInput implements MappedObject
2224
2225
callbacks are called and overwrites any of set values.
2225
2226
2226
2227
In all callbacks are used [ field names] ( #mapping-field-names-to-properties ) , not property names.
2227
- In [ field callbacks] ( #field-callbacks ) , current field name can be accessed via [ context] ( #callback-context ) .
2228
+ In [ field callbacks] ( #validation- field-callbacks ) , current field name can be accessed via [ context] ( #callback-context ) .
2228
2229
2229
2230
Callbacks can be both static and non-static, object mapper initializes object to call non-static callbacks when needed.
2230
2231
2231
2232
Callbacks can have any visibility - public, protected or private.
2232
2233
2233
- ### Mapped object callbacks
2234
+ ### Validation object callbacks
2234
2235
2235
2236
Modify and check data before and after processing fields with their rules
2236
2237
@@ -2319,7 +2320,95 @@ final class WithMappedObjectCallbacksInput implements MappedObject
2319
2320
```
2320
2321
</details >
2321
2322
2322
- ### Field callbacks
2323
+ ### After mapping object callbacks
2324
+
2325
+ Validate object after being fully initialized
2326
+
2327
+ <details open >
2328
+ <summary><code>#[Attributes()]</code></summary>
2329
+
2330
+ ``` php
2331
+ use Orisai\ObjectMapper\Callbacks\AfterMapping;
2332
+ use Orisai\ObjectMapper\Callbacks\Context\ObjectContext;
2333
+ use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
2334
+ use Orisai\ObjectMapper\MappedObject;
2335
+ use Orisai\ObjectMapper\Processing\Value;
2336
+ use Orisai\ObjectMapper\Rules\ListOf;
2337
+ use Orisai\ObjectMapper\Rules\StringValue;
2338
+ use Orisai\ObjectMapper\Types\MessageType;
2339
+
2340
+ #[AfterMapping('afterObject')]
2341
+ final class AfterMappingCallbackInput implements MappedObject
2342
+ {
2343
+
2344
+ /** @var list<string > */
2345
+ #[ListOf(new StringValue())]
2346
+ public array $allowed = [];
2347
+
2348
+ /** @var list<string > */
2349
+ #[ListOf(new StringValue())]
2350
+ public array $forbidden = [];
2351
+
2352
+ private function afterObject(ObjectContext $context): void
2353
+ {
2354
+ if ($this->allowed !== [] && $this->forbidden !== []) {
2355
+ $context->getType()->addError(ValueDoesNotMatch::create(
2356
+ new MessageType("Specify either 'allowed' or 'forbidden', not both."),
2357
+ Value::none(),
2358
+ ));
2359
+ }
2360
+ }
2361
+
2362
+ }
2363
+ ```
2364
+ </details >
2365
+
2366
+ <details >
2367
+ <summary><code>@Annotations()</code></summary>
2368
+
2369
+ ``` php
2370
+ use Orisai\ObjectMapper\Callbacks\AfterMapping;
2371
+ use Orisai\ObjectMapper\Callbacks\Context\ObjectContext;
2372
+ use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
2373
+ use Orisai\ObjectMapper\MappedObject;
2374
+ use Orisai\ObjectMapper\Processing\Value;
2375
+ use Orisai\ObjectMapper\Rules\ListOf;
2376
+ use Orisai\ObjectMapper\Rules\StringValue;
2377
+ use Orisai\ObjectMapper\Types\MessageType;
2378
+
2379
+ /**
2380
+ * @AfterMapping("afterObject")
2381
+ */
2382
+ final class AfterMappingCallbackInput implements MappedObject
2383
+ {
2384
+
2385
+ /**
2386
+ * @var list<string >
2387
+ * @ListOf(@StringValue())
2388
+ */
2389
+ public array $allowed = [];
2390
+
2391
+ /**
2392
+ * @var list<string >
2393
+ * @ListOf(@StringValue())
2394
+ */
2395
+ public array $forbidden = [];
2396
+
2397
+ private function afterObject(ObjectContext $context): void
2398
+ {
2399
+ if ($this->allowed !== [] && $this->forbidden !== []) {
2400
+ $context->getType()->addError(ValueDoesNotMatch::create(
2401
+ new MessageType("Specify either 'allowed' or 'forbidden', not both."),
2402
+ Value::none(),
2403
+ ));
2404
+ }
2405
+ }
2406
+
2407
+ }
2408
+ ```
2409
+ </details >
2410
+
2411
+ ### Validation field callbacks
2323
2412
2324
2413
Modify and check data before and after processing field with its rule
2325
2414
@@ -2441,7 +2530,8 @@ $input = $processor->process(['field' => 'new value'], WithNotInvokedCallbackInp
2441
2530
2442
2531
### Returned value
2443
2532
2444
- Callbacks are by default expected to return a value:
2533
+ [ Validation object callbacks] ( #validation-object-callbacks ) and
2534
+ [ validation field callbacks] ( #validation-field-callbacks ) are by default expected to return a value:
2445
2535
2446
2536
<details open >
2447
2537
<summary><code>#[Attributes()]</code></summary>
@@ -2556,23 +2646,21 @@ final class WithNotReturningCallbackInput implements MappedObject
2556
2646
2557
2647
### Callback context
2558
2648
2559
- Both [ mapped object callbacks] ( #mapped -object-callbacks ) and [ field callbacks] ( #field-callbacks ) have additional context
2649
+ [ Validation object callbacks] ( #validation -object-callbacks ) and [ validation field callbacks] ( #validation- field-callbacks ) have additional context
2560
2650
available as a second parameter, for extended processing:
2561
2651
2562
2652
Mapped object and field contexts
2563
2653
2564
2654
``` php
2565
2655
$context->getProcessor(); // Processor
2566
2656
$context->getOptions(); // Options
2567
- $context->shouldMapDataToObjects (); // bool
2657
+ $context->shouldInitializeObjects (); // bool
2568
2658
$context->getType(); // Type
2569
2659
```
2570
2660
2571
2661
Field context
2572
2662
2573
2663
``` php
2574
- $context->hasDefaultValue(); // bool
2575
- $context->getDefaultValue(); // mixed|exception
2576
2664
$context->getFieldName(); // int|string
2577
2665
$context->getPropertyName(); // string
2578
2666
```
0 commit comments