๐Ÿ“ฆ tacman / api-parameter-issue

๐Ÿ“„ Product.php ยท 113 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113<?php

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\ExactFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;


#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ApiResource(
    operations: [
        new Get(
            normalizationContext: [
                'groups' => ['product.read', 'product.details'],
            ]
        ),
        new GetCollection(
            normalizationContext: [
                'groups' => ['product.read'],
            ],
            parameters: [
                // ?category=foo
                'brand' => new QueryParameter(
                    filter: new ExactFilter() // instance, ignored if not registered
                ),
                'search[:property]' => new QueryParameter(
                    filter: new PartialSearchFilter(),
                    properties: ['title', 'description']
                ),
                'filter[:property]' => new QueryParameter(
                    filter: new ExactFilter(),
                    properties: ['category','brand'], // self::FILTER_PROPS
                ),
                'order[:property]' => new QueryParameter(
                    filter: new OrderFilter(),
                    properties: ['rating']
                ),
            ]
        )],
    normalizationContext: ['groups' => ['product.read', 'product.details']],
)]


class Product
{

    public function __construct(
        #[ORM\Column(type: 'string', length: 255)]
        #[ORM\Id]
        #[Groups(['product.read'])]
        public ?string $sku,

        #[ORM\Column(type: 'string', length: 255)]
        #[ORM\Id]
        #[Groups(['product.read'])]
        public ?string $title,

        #[ORM\Column(type:Types::TEXT, nullable: true)]
        #[ORM\Id]
        #[Groups(['product.read'])]
        public ?string $description,


    )
    {
    }


    // virtual property
    #[Groups(['product.read'])]
    #[ORM\Column(nullable: true)]
    #[ApiProperty("category from extra, virtual but needs index")]
    public ?string $category;

    #[Groups(['product.read'])]
    #[ORM\Column(type: Types::STRING, nullable: true)]
    #[ApiProperty("the registered brand name")]
    public ?string $brand;

    #[ORM\Column(type: Types::SMALLFLOAT, nullable: true)]
    #[ApiProperty("exact price, float, for doctrine filters")]
    public ?float $exactPrice;

    #[Groups(['product.read'])]
    #[ApiProperty("rounded rating, for range slider")]
    #[ORM\Column(type: Types::INTEGER)]
    public int $rating;

    #[Groups(['product.read'])]
    #[ORM\Column(type: Types::INTEGER)]
    public int $stock;

    #[Groups(['product.read'])]
    #[ORM\Column(type: Types::JSON, nullable: true, options: ['jsonb' => true])]
    #[ApiProperty("array of tags")]
    public array $tags;
}