Skip to content

Commit df72960

Browse files
authored
Update getting-started.md - Examples with PHP 8.0 (api-platform#1244)
1 parent cd8dabb commit df72960

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

core/getting-started.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,39 @@ use Doctrine\Common\Collections\ArrayCollection;
4444
use Symfony\Component\Validator\Constraints as Assert;
4545

4646
/**
47-
* @ApiResource
4847
* @ORM\Entity
4948
*/
49+
#[ApiResource]
5050
class Product // The class name will be used to name exposed resources
5151
{
5252
/**
53-
* @ORM\Column(type="integer")
5453
* @ORM\Id
5554
* @ORM\GeneratedValue(strategy="AUTO")
55+
* @ORM\Column(type="integer")
5656
*/
57-
private $id;
57+
private ?int $id = null;
5858

5959
/**
60-
* @var string $name A name property - this description will be available in the API documentation too.
60+
* A name property - this description will be available in the API documentation too.
6161
*
6262
* @ORM\Column
63-
* @Assert\NotBlank
6463
*/
65-
public $name;
64+
#[Assert\NotBlank]
65+
public string $name = '';
6666

6767
// Notice the "cascade" option below, this is mandatory if you want Doctrine to automatically persist the related entity
6868
/**
69+
* @var Offer[]|ArrayCollection
70+
*
6971
* @ORM\OneToMany(targetEntity="Offer", mappedBy="product", cascade={"persist"})
7072
*/
71-
public $offers;
73+
public iterable $offers;
7274

7375
public function __construct()
7476
{
7577
$this->offers = new ArrayCollection(); // Initialize $offers as a Doctrine collection
7678
}
77-
79+
7880
public function getId(): ?int
7981
{
8082
return $this->id;
@@ -93,7 +95,7 @@ class Product // The class name will be used to name exposed resources
9395
$offer->product = null;
9496
$this->offers->removeElement($offer);
9597
}
96-
98+
9799
// ...
98100
}
99101
```
@@ -111,35 +113,33 @@ use Symfony\Component\Validator\Constraints as Assert;
111113
/**
112114
* An offer from my shop - this description will be automatically extracted from the PHPDoc to document the API.
113115
*
114-
* @ApiResource(iri="http://schema.org/Offer")
115116
* @ORM\Entity
116117
*/
118+
#[ApiResource(iri: 'http://schema.org/Offer')]
117119
class Offer
118120
{
119121
/**
120122
* @ORM\Column(type="integer")
121123
* @ORM\Id
122124
* @ORM\GeneratedValue(strategy="AUTO")
123125
*/
124-
private $id;
126+
private ?int $id = null;
125127

126128
/**
127129
* @ORM\Column(type="text")
128130
*/
129-
public $description;
131+
public string $description = '';
130132

131133
/**
132134
* @ORM\Column(type="float")
133-
* @Assert\NotBlank
134-
* @Assert\Range(min=0, minMessage="The price must be superior to 0.")
135-
* @Assert\Type(type="float")
136135
*/
137-
public $price;
136+
#[Assert\Range(minMessage: 'The price must be superior to 0.', min: 0)]
137+
public float $price = -1.0;
138138

139139
/**
140140
* @ORM\ManyToOne(targetEntity="Product", inversedBy="offers")
141141
*/
142-
public $product;
142+
public ?Product $product = null;
143143

144144
public function getId(): ?int
145145
{

0 commit comments

Comments
 (0)