|
| 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 license@prestashop.com so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <contact@prestashop.com> |
| 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\Attribute; |
| 22 | + |
| 23 | +use ApiPlatform\Metadata\ApiProperty; |
| 24 | +use ApiPlatform\Metadata\ApiResource; |
| 25 | +use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage; |
| 26 | +use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex; |
| 27 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\AddAttributeCommand; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\DeleteAttributeCommand; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\EditAttributeCommand; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeConstraintException; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeNotFoundException; |
| 32 | +use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Query\GetAttributeForEditing; |
| 33 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate; |
| 34 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete; |
| 35 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; |
| 36 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate; |
| 37 | +use PrestaShopBundle\ApiPlatform\Metadata\LocalizedValue; |
| 38 | +use Symfony\Component\HttpFoundation\Response; |
| 39 | +use Symfony\Component\Validator\Constraints as Assert; |
| 40 | + |
| 41 | +#[ApiResource( |
| 42 | + operations: [ |
| 43 | + new CQRSGet( |
| 44 | + uriTemplate: '/attributes/attribute/{attributeId}', |
| 45 | + CQRSQuery: GetAttributeForEditing::class, |
| 46 | + scopes: [ |
| 47 | + 'attribute_read', |
| 48 | + ], |
| 49 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 50 | + ), |
| 51 | + new CQRSCreate( |
| 52 | + uriTemplate: '/attributes/attribute', |
| 53 | + validationContext: ['groups' => ['Default', 'Create']], |
| 54 | + CQRSCommand: AddAttributeCommand::class, |
| 55 | + CQRSQuery: GetAttributeForEditing::class, |
| 56 | + scopes: [ |
| 57 | + 'attribute_write', |
| 58 | + ], |
| 59 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 60 | + CQRSCommandMapping: self::CREATE_COMMAND_MAPPING, |
| 61 | + ), |
| 62 | + new CQRSPartialUpdate( |
| 63 | + uriTemplate: '/attributes/attribute/{attributeId}', |
| 64 | + validationContext: ['groups' => ['Default', 'Update']], |
| 65 | + CQRSCommand: EditAttributeCommand::class, |
| 66 | + CQRSQuery: GetAttributeForEditing::class, |
| 67 | + scopes: [ |
| 68 | + 'attribute_write', |
| 69 | + ], |
| 70 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 71 | + CQRSCommandMapping: self::UPDATE_COMMAND_MAPPING, |
| 72 | + ), |
| 73 | + new CQRSDelete( |
| 74 | + uriTemplate: '/attributes/attribute/{attributeId}', |
| 75 | + CQRSCommand: DeleteAttributeCommand::class, |
| 76 | + scopes: [ |
| 77 | + 'attribute_write', |
| 78 | + ], |
| 79 | + ), |
| 80 | + ], |
| 81 | + exceptionToStatus: [ |
| 82 | + AttributeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 83 | + AttributeNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 84 | + ], |
| 85 | +)] |
| 86 | +class Attribute |
| 87 | +{ |
| 88 | + #[ApiProperty(identifier: true)] |
| 89 | + public int $attributeId; |
| 90 | + |
| 91 | + #[ApiProperty(openapiContext: ['type' => 'integer', 'example' => 1])] |
| 92 | + public int $attributeGroupId; |
| 93 | + |
| 94 | + #[LocalizedValue] |
| 95 | + #[DefaultLanguage(groups: ['Create'], fieldName: 'names')] |
| 96 | + #[DefaultLanguage(groups: ['Update'], fieldName: 'names', allowNull: true)] |
| 97 | + #[Assert\All(constraints: [ |
| 98 | + new TypedRegex([ |
| 99 | + 'type' => TypedRegex::TYPE_CATALOG_NAME, |
| 100 | + ]), |
| 101 | + ])] |
| 102 | + public array $names; |
| 103 | + |
| 104 | + public string $color; |
| 105 | + |
| 106 | + #[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])] |
| 107 | + #[Assert\NotBlank(allowNull: true)] |
| 108 | + public array $shopIds; |
| 109 | + |
| 110 | + public int $position; |
| 111 | + |
| 112 | + public const QUERY_MAPPING = [ |
| 113 | + '[localizedNames]' => '[names]', |
| 114 | + '[name]' => '[names]', |
| 115 | + '[associatedShopIds]' => '[shopIds]', |
| 116 | + ]; |
| 117 | + |
| 118 | + public const CREATE_COMMAND_MAPPING = [ |
| 119 | + '[names]' => '[localizedValue]', |
| 120 | + '[shopIds]' => '[associatedShopIds]', |
| 121 | + ]; |
| 122 | + |
| 123 | + public const UPDATE_COMMAND_MAPPING = [ |
| 124 | + '[names]' => '[localizedNames]', |
| 125 | + '[shopIds]' => '[associatedShopIds]', |
| 126 | + ]; |
| 127 | +} |
0 commit comments