| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of PurchaseCodeInfo |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class PurchaseCodeInfo |
| 12 |
{ |
| 13 |
public const TARIFF_CODE_FREE = 'free'; |
| 14 |
|
| 15 |
|
| 16 |
public const FIELD_MESSAGE = 'message'; |
| 17 |
public const FIELD_VALID_FROM = 'valid_from'; |
| 18 |
public const FIELD_VALID_TO = 'valid_to'; |
| 19 |
public const FIELD_TARIFF_CODE = 'tariff_code'; |
| 20 |
public const FIELD_TARIFF_FROM = 'tariff_from'; |
| 21 |
public const FIELD_TARIFF_TO = 'tariff_to'; |
| 22 |
public const FIELD_LIMITS = 'limits'; |
| 23 |
public const FIELD_COUNT = 'count'; |
| 24 |
|
| 25 |
|
| 26 |
private ?string $message = null; |
| 27 |
private ?string $validFrom = null; |
| 28 |
private ?string $validTo = null; |
| 29 |
private ?string $tariffCode = null; |
| 30 |
private ?string $tariffFrom = null; |
| 31 |
private ?string $tariffTo = null; |
| 32 |
private ?PurchaseCodeInfoLimits $limits = null; |
| 33 |
private ?PurchaseCodeInfoCount $count = null; |
| 34 |
|
| 35 |
|
| 36 |
public function getMessage(): ?string |
| 37 |
{ |
| 38 |
return $this->message; |
| 39 |
} |
| 40 |
|
| 41 |
public function setMessage(?string $message): self |
| 42 |
{ |
| 43 |
$this->message = $message; |
| 44 |
|
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
public function getValidFrom(): ?string |
| 49 |
{ |
| 50 |
return $this->validFrom; |
| 51 |
} |
| 52 |
|
| 53 |
public function setValidFrom(?string $validFrom): self |
| 54 |
{ |
| 55 |
$this->validFrom = $validFrom; |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
public function getValidTo(): ?string |
| 61 |
{ |
| 62 |
return $this->validTo; |
| 63 |
} |
| 64 |
|
| 65 |
public function setValidTo(?string $validTo): self |
| 66 |
{ |
| 67 |
$this->validTo = $validTo; |
| 68 |
|
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
public function getValidToStamp(): ?string |
| 73 |
{ |
| 74 |
if ($this->validTo) { |
| 75 |
return strtotime($this->validTo); |
| 76 |
} |
| 77 |
|
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
public function getTariffCode(): ?string |
| 82 |
{ |
| 83 |
return $this->tariffCode; |
| 84 |
} |
| 85 |
|
| 86 |
public function isTariffCodeFree(): bool |
| 87 |
{ |
| 88 |
return ($this->tariffCode === self::TARIFF_CODE_FREE || !$this->tariffCode); |
| 89 |
} |
| 90 |
|
| 91 |
public function setTariffCode(?string $tariffCode): self |
| 92 |
{ |
| 93 |
$this->tariffCode = $tariffCode; |
| 94 |
|
| 95 |
return $this; |
| 96 |
} |
| 97 |
|
| 98 |
public function getTariffFrom(): ?string |
| 99 |
{ |
| 100 |
return $this->tariffFrom; |
| 101 |
} |
| 102 |
|
| 103 |
public function setTariffFrom(?string $tariffFrom): self |
| 104 |
{ |
| 105 |
$this->tariffFrom = $tariffFrom; |
| 106 |
|
| 107 |
return $this; |
| 108 |
} |
| 109 |
|
| 110 |
public function getTariffTo(): ?string |
| 111 |
{ |
| 112 |
return $this->tariffTo; |
| 113 |
} |
| 114 |
|
| 115 |
public function setTariffTo(?string $tariffTo): self |
| 116 |
{ |
| 117 |
$this->tariffTo = $tariffTo; |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
public function getTariffToStamp(): ?string |
| 123 |
{ |
| 124 |
if ($this->tariffTo) { |
| 125 |
return strtotime($this->tariffTo); |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
public function getSupportedUntilStamp(): ?string |
| 132 |
{ |
| 133 |
$valid_to = $this->getValidToStamp(); |
| 134 |
$tariff_to = $this->getTariffToStamp(); |
| 135 |
|
| 136 |
return ($valid_to && $tariff_to && $tariff_to > $valid_to) ? $tariff_to : $valid_to; |
| 137 |
} |
| 138 |
|
| 139 |
public function getLimits(): ?PurchaseCodeInfoLimits |
| 140 |
{ |
| 141 |
return $this->limits; |
| 142 |
} |
| 143 |
|
| 144 |
public function setLimits(?PurchaseCodeInfoLimits $limits): self |
| 145 |
{ |
| 146 |
$this->limits = $limits; |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
public function getCount(): ?PurchaseCodeInfoCount |
| 152 |
{ |
| 153 |
return $this->count; |
| 154 |
} |
| 155 |
|
| 156 |
public function setCount(?PurchaseCodeInfoCount $count): self |
| 157 |
{ |
| 158 |
$this->count = $count; |
| 159 |
|
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
public function getPackageUsageInPercent(): ?float |
| 164 |
{ |
| 165 |
if (!$this->getLimits()) { |
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
if (!$this->getCount()) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
$limitSum = $this->getLimits()->getAll(); |
| 174 |
$countSum = $this->getCount()->getSum(); |
| 175 |
|
| 176 |
return $countSum * 100 / $limitSum; |
| 177 |
} |
| 178 |
|
| 179 |
public function toArray(): array |
| 180 |
{ |
| 181 |
return [ |
| 182 |
self::FIELD_MESSAGE => $this->getMessage(), |
| 183 |
self::FIELD_VALID_FROM => $this->getValidFrom(), |
| 184 |
self::FIELD_VALID_TO => $this->getValidTo(), |
| 185 |
self::FIELD_TARIFF_CODE => $this->getTariffCode(), |
| 186 |
self::FIELD_TARIFF_FROM => $this->getTariffFrom(), |
| 187 |
self::FIELD_TARIFF_TO => $this->getTariffTo(), |
| 188 |
self::FIELD_LIMITS => $this->getLimits()?->toArray(), |
| 189 |
self::FIELD_COUNT => $this->getCount()?->toArray(), |
| 190 |
]; |
| 191 |
} |
| 192 |
} |
| 193 |
|