TracksProvidedFields.php
43 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Api\InputTypes; |
| 6 | |
| 7 | /** |
| 8 | * Trait for input types to track which fields were explicitly provided in the GraphQL request. |
| 9 | * |
| 10 | * This allows mutations to distinguish between a field being missing (don't change it) |
| 11 | * and explicitly set to null (clear it). |
| 12 | */ |
| 13 | trait TracksProvidedFields { |
| 14 | /** |
| 15 | * Fields that were explicitly provided in the input. |
| 16 | * |
| 17 | * Using an underscore prefix to keep it invisible to the ApiBuilder |
| 18 | * (which only scans public properties for GraphQL fields). |
| 19 | * |
| 20 | * @var array<string, true> |
| 21 | */ |
| 22 | protected array $provided_fields = array(); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase -- internal tracking array |
| 23 | |
| 24 | /** |
| 25 | * Mark a field as explicitly provided in the input. |
| 26 | * |
| 27 | * @param string $field The field name. |
| 28 | */ |
| 29 | public function mark_provided( string $field ): void { |
| 30 | $this->provided_fields[ $field ] = true; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Check whether a field was explicitly provided in the input. |
| 35 | * |
| 36 | * @param string $field The field name. |
| 37 | * @return bool |
| 38 | */ |
| 39 | public function was_provided( string $field ): bool { |
| 40 | return isset( $this->provided_fields[ $field ] ); |
| 41 | } |
| 42 | } |
| 43 |