This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscount.php
More file actions
95 lines (77 loc) · 2.18 KB
/
Discount.php
File metadata and controls
95 lines (77 loc) · 2.18 KB
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
<?php
/**
* Created by enea dhack - 29/09/2017 12:02 PM.
*/
namespace Enea\Cashier\Modifiers;
use Enea\Cashier\Calculations\Percentager;
use Enea\Cashier\HasProperties;
use Enea\Cashier\IsJsonable;
class Discount implements DiscountContract
{
use IsJsonable, HasProperties;
protected string $code;
protected string $description;
protected float $discount;
protected bool $percentage;
public function __construct(
string $code,
float $discount,
string $description,
bool $percentage = true,
array $properties = []
) {
$this->code = $code;
$this->description = $description;
$this->discount = $discount;
$this->percentage = $percentage;
$this->setProperties($properties);
}
public static function percentage(float $percentage, array $properties = []): self
{
return new static(static::generateCode(), $percentage, 'discount percentage', true, $properties);
}
public static function value(float $discount, array $properties = []): self
{
return new static(static::generateCode(), $discount, 'discount value', false, $properties);
}
protected static function generateCode(): string
{
return hash('adler32', microtime(true), false);
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getDiscountCode(): string
{
return $this->code;
}
public function getDescription(): string
{
return $this->description;
}
public function extract(float $total): float
{
if (! $this->percentage) {
return $this->discount;
}
return Percentager::excluded($total, $this->discount)->calculate();
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return [
'code' => $this->getDiscountCode(),
'description' => $this->getDescription(),
'properties' => $this->getProperties(),
];
}
}