Skip to content

Commit 9c5fe0f

Browse files
committed
feat: add attributes
1 parent ed686d3 commit 9c5fe0f

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

src/Attributes.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* @author enea dhack <enea.so@live.com>
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Vaened\SwiftCart;
9+
10+
final class Attributes
11+
{
12+
public function __construct(private array $attributes)
13+
{
14+
}
15+
16+
public static function are(array $attributes): self
17+
{
18+
return new self($attributes);
19+
}
20+
21+
public static function empty(): self
22+
{
23+
return new self([]);
24+
}
25+
26+
public function has(string $name): bool
27+
{
28+
return isset($this->attributes[$name]);
29+
}
30+
31+
public function __get(string $name): mixed
32+
{
33+
return $this->attributes[$name] ?? null;
34+
}
35+
36+
public function __set(string $name, mixed $value): void
37+
{
38+
$this->attributes[$name] = $value;
39+
}
40+
}

src/Concerns/HasAttributes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @author enea dhack <enea.so@live.com>
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Vaened\SwiftCart\Concerns;
9+
10+
trait HasAttributes
11+
{
12+
private mixed $attributes = null;
13+
14+
public function attributes(): mixed
15+
{
16+
return $this->attributes;
17+
}
18+
19+
protected function setAttributes(mixed $attributes): void
20+
{
21+
$this->attributes = $attributes;
22+
}
23+
}

src/Entities/Attributable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* @author enea dhack <enea.so@live.com>
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Vaened\SwiftCart\Entities;
9+
10+
interface Attributable
11+
{
12+
public function attributes(): mixed;
13+
}

src/Items/CartItem.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
use Vaened\PriceEngine\Cashier;
1313
use Vaened\PriceEngine\Money\Amount;
1414
use Vaened\PriceEngine\TotalSummary;
15-
use Vaened\SwiftCart\Entities\Chargeable;
16-
use Vaened\SwiftCart\Entities\Discountable;
17-
use Vaened\SwiftCart\Entities\Identifiable;
18-
use Vaened\SwiftCart\Entities\Tradable;
15+
use Vaened\SwiftCart\Attributes;
16+
use Vaened\SwiftCart\Concerns\HasAttributes;
17+
use Vaened\SwiftCart\Entities\{Attributable, Chargeable, Discountable, Identifiable, Tradable};
1918
use Vaened\SwiftCart\SwiftCartConfig;
2019

2120
abstract class CartItem implements Identifiable
2221
{
22+
use HasAttributes;
23+
2324
private readonly Cashier $cashier;
2425

2526
public function __construct(
@@ -28,6 +29,7 @@ public function __construct(
2829
Taxes $taxes = new Taxes([]),
2930
)
3031
{
32+
$this->setAttributes($tradable instanceof Attributable ? $tradable->attributes() : Attributes::empty());
3133
$this->cashier = $this->createCashier(
3234
$tradable->amount(),
3335
$quantity,

0 commit comments

Comments
 (0)