|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 5 | + * |
| 6 | + * NOTICE OF LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the Academic Free License version 3.0 |
| 9 | + * that is bundled with this package in the file LICENSE.md. |
| 10 | + * It is also available through the world-wide-web at this URL: |
| 11 | + * https://opensource.org/licenses/AFL-3.0 |
| 12 | + * If you did not receive a copy of the license and are unable to |
| 13 | + * obtain it through the world-wide-web, please send an email |
| 14 | + * to [email protected] so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <[email protected]> |
| 17 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 18 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 19 | + */ |
| 20 | + |
| 21 | +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Address; |
| 22 | + |
| 23 | +use ApiPlatform\Metadata\ApiProperty; |
| 24 | +use ApiPlatform\Metadata\ApiResource; |
| 25 | +use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex; |
| 26 | +use PrestaShop\PrestaShop\Core\Domain\Address\Command\EditCartAddressCommand; |
| 27 | +use PrestaShop\PrestaShop\Core\Domain\Address\Exception\AddressConstraintException; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\Address\Query\GetCustomerAddressForEditing; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CartNotFoundException; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\InvalidAddressTypeException; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\Country\Exception\CountryConstraintException; |
| 32 | +use PrestaShop\PrestaShop\Core\Domain\Country\ValueObject\CountryId; |
| 33 | +use PrestaShop\PrestaShop\Core\Domain\State\Exception\StateConstraintException; |
| 34 | +use PrestaShop\PrestaShop\Core\Domain\State\ValueObject\StateIdInterface; |
| 35 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate; |
| 36 | +use Symfony\Component\HttpFoundation\Response; |
| 37 | + |
| 38 | +#[ApiResource( |
| 39 | + operations: [ |
| 40 | + new CQRSPartialUpdate( |
| 41 | + uriTemplate: '/addresses/carts/{cartId}', |
| 42 | + CQRSCommand: EditCartAddressCommand::class, |
| 43 | + CQRSQuery: GetCustomerAddressForEditing::class, |
| 44 | + scopes: [ |
| 45 | + 'address_write', |
| 46 | + ], |
| 47 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 48 | + CQRSCommandMapping: self::COMMAND_MAPPING, |
| 49 | + validationContext: ['groups' => ['Default', 'Update']], |
| 50 | + ), |
| 51 | + ], |
| 52 | + exceptionToStatus: [ |
| 53 | + AddressConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 54 | + CountryConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 55 | + StateConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 56 | + InvalidAddressTypeException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 57 | + CartNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 58 | + ], |
| 59 | +)] |
| 60 | +class CartAddress |
| 61 | +{ |
| 62 | + // Identifiers from URI |
| 63 | + #[ApiProperty(identifier: true)] |
| 64 | + public int $cartId = 0; |
| 65 | + |
| 66 | + public string $addressType; |
| 67 | + |
| 68 | + // Optional address fields for update |
| 69 | + public ?string $addressAlias = null; |
| 70 | + |
| 71 | + public ?string $firstName = null; |
| 72 | + |
| 73 | + public ?string $lastName = null; |
| 74 | + |
| 75 | + #[TypedRegex([ |
| 76 | + 'type' => TypedRegex::TYPE_ADDRESS, |
| 77 | + ])] |
| 78 | + public ?string $address = null; |
| 79 | + |
| 80 | + #[TypedRegex([ |
| 81 | + 'type' => TypedRegex::TYPE_ADDRESS, |
| 82 | + ])] |
| 83 | + public ?string $address2 = null; |
| 84 | + |
| 85 | + #[TypedRegex([ |
| 86 | + 'type' => TypedRegex::TYPE_CITY_NAME, |
| 87 | + ])] |
| 88 | + public ?string $city = null; |
| 89 | + |
| 90 | + #[TypedRegex([ |
| 91 | + 'type' => TypedRegex::TYPE_POST_CODE, |
| 92 | + ])] |
| 93 | + public ?string $postCode = null; |
| 94 | + |
| 95 | + public ?CountryId $countryId = null; |
| 96 | + |
| 97 | + public ?StateIdInterface $stateId = null; |
| 98 | + |
| 99 | + #[TypedRegex([ |
| 100 | + 'type' => TypedRegex::TYPE_PHONE_NUMBER, |
| 101 | + ])] |
| 102 | + public ?string $homePhone = null; |
| 103 | + |
| 104 | + #[TypedRegex([ |
| 105 | + 'type' => TypedRegex::TYPE_PHONE_NUMBER, |
| 106 | + ])] |
| 107 | + public ?string $mobilePhone = null; |
| 108 | + |
| 109 | + public ?string $company = null; |
| 110 | + |
| 111 | + public ?string $vatNumber = null; |
| 112 | + |
| 113 | + public ?string $other = null; |
| 114 | + |
| 115 | + #[TypedRegex([ |
| 116 | + 'type' => TypedRegex::TYPE_DNI_LITE, |
| 117 | + ])] |
| 118 | + public ?string $dni = null; |
| 119 | + |
| 120 | + public const QUERY_MAPPING = [ |
| 121 | + '[id]' => '[addressId]', |
| 122 | + ]; |
| 123 | + |
| 124 | + public const COMMAND_MAPPING = [ |
| 125 | + '[postCode]' => '[postcode]', |
| 126 | + '[homePhone]' => '[phone]', |
| 127 | + '[mobilePhone]' => '[phone_mobile]', |
| 128 | + '[vatNumber]' => '[vat_number]', |
| 129 | + '[stateId]' => '[id_state]', |
| 130 | + ]; |
| 131 | +} |
0 commit comments