Skip to content

Commit 2f0f4c9

Browse files
committed
change
1 parent 02617b7 commit 2f0f4c9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/sale/Sale.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function __construct(
6161
CustomerInterface $customer,
6262
?PlanInterface $plan = null,
6363
?DateTimeImmutable $time = null,
64-
?string $data = null,
64+
$data = null,
6565
) {
6666
$this->id = $id;
6767
$this->target = $target;
6868
$this->customer = $customer;
6969
$this->plan = $plan;
7070
$this->time = $time ?? new DateTimeImmutable();
71-
$this->data = $data;
71+
$this->data = $this->setData($data);
7272
}
7373

7474
public function getId()
@@ -130,6 +130,26 @@ public function setId($id)
130130
$this->id = $id;
131131
}
132132

133+
public function setData($data): self
134+
{
135+
if (empty($data)) {
136+
$this->data = null;
137+
return $this;
138+
}
139+
140+
if (is_string($data) && json_validate($data)) {
141+
$this->data = $data;
142+
return $this;
143+
}
144+
145+
if (is_array($data)) {
146+
$this->data = Json::encode($data);
147+
return $this;
148+
}
149+
150+
throw new \Exception("Cannot assign data");
151+
}
152+
133153
public function getData(): ?array
134154
{
135155
return !empty($this->data) ? Json::decode($this->data, true) : null;

0 commit comments

Comments
 (0)