@@ -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,83 @@ 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\MappedObject;
2334
+ use Orisai\ObjectMapper\Rules\ListOf;
2335
+ use Orisai\ObjectMapper\Rules\StringValue;
2336
+
2337
+ #[AfterMapping('afterObject')]
2338
+ final class AfterMappingCallbackInput implements MappedObject
2339
+ {
2340
+
2341
+ /** @var list<string > */
2342
+ #[ListOf(new StringValue())]
2343
+ public array $allowed = [];
2344
+
2345
+ /** @var list<string > */
2346
+ #[ListOf(new StringValue())]
2347
+ public array $forbidden = [];
2348
+
2349
+ private function afterObject(ObjectContext $context): void
2350
+ {
2351
+ if ($this->allowed !== [] && $this->forbidden !== []) {
2352
+ $context->getType()->addError("Specify either 'allowed' or 'forbidden', not both.");
2353
+ }
2354
+ }
2355
+
2356
+ }
2357
+ ```
2358
+ </details >
2359
+
2360
+ <details >
2361
+ <summary><code>@Annotations()</code></summary>
2362
+
2363
+ ``` php
2364
+ use Orisai\ObjectMapper\Callbacks\AfterMapping;
2365
+ use Orisai\ObjectMapper\Callbacks\Context\ObjectContext;
2366
+ use Orisai\ObjectMapper\MappedObject;
2367
+ use Orisai\ObjectMapper\Rules\ListOf;
2368
+ use Orisai\ObjectMapper\Rules\StringValue;
2369
+
2370
+ /**
2371
+ * @AfterMapping("afterObject")
2372
+ */
2373
+ final class AfterMappingCallbackInput implements MappedObject
2374
+ {
2375
+
2376
+ /**
2377
+ * @var list<string >
2378
+ * @ListOf(@StringValue())
2379
+ */
2380
+ public array $allowed = [];
2381
+
2382
+ /**
2383
+ * @var list<string >
2384
+ * @ListOf(@StringValue())
2385
+ */
2386
+ public array $forbidden = [];
2387
+
2388
+ private function afterObject(ObjectContext $context): void
2389
+ {
2390
+ if ($this->allowed !== [] && $this->forbidden !== []) {
2391
+ $context->getType()->addError("Specify either 'allowed' or 'forbidden', not both.");
2392
+ }
2393
+ }
2394
+
2395
+ }
2396
+ ```
2397
+ </details >
2398
+
2399
+ ### Validation field callbacks
2323
2400
2324
2401
Modify and check data before and after processing field with its rule
2325
2402
@@ -2441,7 +2518,8 @@ $input = $processor->process(['field' => 'new value'], WithNotInvokedCallbackInp
2441
2518
2442
2519
### Returned value
2443
2520
2444
- Callbacks are by default expected to return a value:
2521
+ [ Validation object callbacks] ( #validation-object-callbacks ) and
2522
+ [ validation field callbacks] ( #validation-field-callbacks ) are by default expected to return a value:
2445
2523
2446
2524
<details open >
2447
2525
<summary><code>#[Attributes()]</code></summary>
@@ -2556,23 +2634,21 @@ final class WithNotReturningCallbackInput implements MappedObject
2556
2634
2557
2635
### Callback context
2558
2636
2559
- Both [ mapped object callbacks] ( #mapped -object-callbacks ) and [ field callbacks] ( #field-callbacks ) have additional context
2637
+ [ Validation object callbacks] ( #validation -object-callbacks ) and [ validation field callbacks] ( #validation- field-callbacks ) have additional context
2560
2638
available as a second parameter, for extended processing:
2561
2639
2562
2640
Mapped object and field contexts
2563
2641
2564
2642
``` php
2565
2643
$context->getProcessor(); // Processor
2566
2644
$context->getOptions(); // Options
2567
- $context->shouldMapDataToObjects (); // bool
2645
+ $context->shouldInitializeObjects (); // bool
2568
2646
$context->getType(); // Type
2569
2647
```
2570
2648
2571
2649
Field context
2572
2650
2573
2651
``` php
2574
- $context->hasDefaultValue(); // bool
2575
- $context->getDefaultValue(); // mixed|exception
2576
2652
$context->getFieldName(); // int|string
2577
2653
$context->getPropertyName(); // string
2578
2654
```
0 commit comments