Skip to content

Commit a075940

Browse files
committed
Update
1 parent 98f7e04 commit a075940

35 files changed

+1243
-1
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DB_HOST=
2+
DB_USER=
3+
DB_PASS=
4+
DB_DATABASE=

.idea/PHP8GIO.iml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Attributes/Get.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Attributes;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)]
10+
class Get extends Route
11+
{
12+
public function __construct(string $routePath)
13+
{
14+
parent::__construct($routePath);
15+
}
16+
}

app/Attributes/Post.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Attributes;
6+
7+
use App\Enums\HttpMethod;
8+
use Attribute;
9+
10+
#[Attribute(Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)]
11+
class Post extends Route
12+
{
13+
public function __construct(string $routePath)
14+
{
15+
parent::__construct($routePath, HttpMethod::Post);
16+
}
17+
}

app/Attributes/Put.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Attributes;
6+
7+
use App\Enums\HttpMethod;
8+
use Attribute;
9+
10+
#[Attribute(Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)]
11+
class Put extends Route
12+
{
13+
public function __construct(string $routePath)
14+
{
15+
parent::__construct($routePath, HttpMethod::Put);
16+
}
17+
}

app/Attributes/Route.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Attributes;
6+
7+
use App\Contracts\RouteInterface;
8+
use App\Enums\HttpMethod;
9+
use Attribute;
10+
11+
#[Attribute(Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)]
12+
class Route implements RouteInterface
13+
{
14+
public function __construct(public string $routePath, public HttpMethod $method = HttpMethod::Get)
15+
{
16+
}
17+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Contracts;
6+
7+
use App\DTO\EmailValidationResult;
8+
9+
interface EmailValidationInterface
10+
{
11+
public function verify(string $email): EmailValidationResult;
12+
}

app/Contracts/RouteInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Contracts;
6+
7+
interface RouteInterface
8+
{
9+
10+
}

app/Controllers/CurlController.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Controllers;
6+
7+
use App\Attributes\Get;
8+
use App\Contracts\EmailValidationInterface;
9+
10+
class CurlController
11+
{
12+
public function __construct(private EmailValidationInterface $emailValidationService)
13+
{
14+
}
15+
16+
#[Get('/curl')]
17+
public function index()
18+
{
19+
$email = '[email protected]';
20+
$result = $this->emailValidationService->verify($email);
21+
22+
$score = $result->score;
23+
$isDeliverable = $result->isDeliverable;
24+
25+
var_dump($score, $isDeliverable);
26+
27+
echo '<pre>';
28+
print_r($result);
29+
echo '</pre>';
30+
}
31+
}

app/Controllers/InvoiceController.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Controllers;
6+
7+
use App\Attributes\Get;
8+
use App\Enums\InvoiceStatus;
9+
use App\Models\Invoice;
10+
use App\View;
11+
use Carbon\Carbon;
12+
13+
class InvoiceController
14+
{
15+
#[Get('/invoices')]
16+
public function index(): View
17+
{
18+
$invoices = Invoice::query()->where('status', InvoiceStatus::Paid)->get();
19+
20+
return View::make('invoices/index', ['invoices' => $invoices]);
21+
}
22+
23+
#[Get('/invoices/new')]
24+
public function create()
25+
{
26+
$invoice = new Invoice();
27+
28+
$invoice->invoice_number = 5;
29+
$invoice->amount = 20;
30+
$invoice->status = InvoiceStatus::Pending;
31+
$invoice->due_date = (new Carbon())->addDay();
32+
33+
$invoice->save();
34+
35+
echo $invoice->id . ', ' . $invoice->due_date->format('m/d/Y');
36+
}
37+
}

app/CustomMailer.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App;
6+
7+
use Symfony\Component\Mailer\Envelope;
8+
use Symfony\Component\Mailer\MailerInterface;
9+
use Symfony\Component\Mailer\Transport;
10+
use Symfony\Component\Mailer\Transport\TransportInterface;
11+
use Symfony\Component\Mime\RawMessage;
12+
13+
class CustomMailer implements MailerInterface
14+
{
15+
protected TransportInterface $transport;
16+
17+
public function __construct(protected string $dsn)
18+
{
19+
$this->transport = Transport::fromDsn($dsn);
20+
}
21+
22+
public function send(RawMessage $message, Envelope $envelope = null): void
23+
{
24+
//... some logic
25+
26+
$this->transport->send($message, $envelope);
27+
28+
// ... some more logic
29+
}
30+
}

app/DTO/EmailValidationResult.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\DTO;
6+
7+
class EmailValidationResult
8+
{
9+
public function __construct(public readonly int $score, public readonly bool $isDeliverable)
10+
{
11+
}
12+
}

app/Entity/Invoice.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace App\Entity;
6+
7+
use App\Enums\InvoiceStatus;
8+
use Doctrine\Common\Collections\ArrayCollection;
9+
use Doctrine\Common\Collections\Collection;
10+
use Doctrine\DBAL\Types\Types;
11+
use Doctrine\ORM\Event\LifecycleEventArgs;
12+
use Doctrine\ORM\Mapping\Column;
13+
use Doctrine\ORM\Mapping\Entity;
14+
use Doctrine\ORM\Mapping\GeneratedValue;
15+
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Doctrine\ORM\Mapping\OneToMany;
18+
use Doctrine\ORM\Mapping\PrePersist;
19+
use Doctrine\ORM\Mapping\Table;
20+
21+
#[Entity]
22+
#[Table('invoices')]
23+
#[HasLifecycleCallbacks]
24+
class Invoice
25+
{
26+
#[Id]
27+
#[Column, GeneratedValue]
28+
private int $id;
29+
30+
#[Column(type: Types::DECIMAL, precision: 10, scale: 2)]
31+
private float $amount;
32+
33+
#[Column(name: 'invoice_number')]
34+
private string $invoiceNumber;
35+
36+
#[Column]
37+
private InvoiceStatus $status;
38+
39+
#[Column(name: 'created_at')]
40+
private \DateTime $createdAt;
41+
42+
#[Column(name: 'due_date')]
43+
private \DateTime $dueDate;
44+
45+
#[OneToMany(targetEntity: InvoiceItem::class, mappedBy: 'invoice', cascade: ['persist', 'remove'])]
46+
private Collection $items;
47+
48+
public function __construct()
49+
{
50+
$this->items = new ArrayCollection();
51+
}
52+
53+
#[PrePersist]
54+
public function onPrePersist(LifecycleEventArgs $args)
55+
{
56+
$this->createdAt = new \DateTime();
57+
}
58+
59+
public function getId(): int
60+
{
61+
return $this->id;
62+
}
63+
64+
public function getAmount(): float
65+
{
66+
return $this->amount;
67+
}
68+
69+
public function setAmount(float $amount): Invoice
70+
{
71+
$this->amount = $amount;
72+
73+
return $this;
74+
}
75+
76+
public function getInvoiceNumber(): string
77+
{
78+
return $this->invoiceNumber;
79+
}
80+
81+
public function setInvoiceNumber(string $invoiceNumber): Invoice
82+
{
83+
$this->invoiceNumber = $invoiceNumber;
84+
85+
return $this;
86+
}
87+
88+
public function getStatus(): InvoiceStatus
89+
{
90+
return $this->status;
91+
}
92+
93+
public function setStatus(InvoiceStatus $status): Invoice
94+
{
95+
$this->status = $status;
96+
97+
return $this;
98+
}
99+
100+
public function getCreatedAt(): \DateTime
101+
{
102+
return $this->createdAt;
103+
}
104+
105+
/**
106+
* @return Collection<InvoiceItem>
107+
*/
108+
public function getItems(): Collection
109+
{
110+
return $this->items;
111+
}
112+
113+
public function addItem(InvoiceItem $item): Invoice
114+
{
115+
$item->setInvoice($this);
116+
117+
$this->items->add($item);
118+
119+
return $this;
120+
}
121+
}

0 commit comments

Comments
 (0)