Skip to content

Commit 3599b3a

Browse files
authored
Merge pull request #20 from FrittenKeeZ/feature/with-start-expire-null
Allow `Vouchers::withStart*()` and `Vouchers::withExpire*()` methods to accept `null`
2 parents ca67e80 + e7c97b3 commit 3599b3a

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Release Notes
22

3+
## [v0.6.2 (2025-07-10)](https://github.com/FrittenKeeZ/laravel-vouchers/compare/0.6.1...0.6.2)
4+
5+
### Changed
6+
- Allow `Vouchers::withStart*()` and `Vouchers::withExpire*()` methods to accept `null`
7+
38
## [v0.6.1 (2025-07-09)](https://github.com/FrittenKeeZ/laravel-vouchers/compare/0.6.0...0.6.1)
49

510
### Changed
6-
- Allow withEntities to accept any iterable
11+
- Allow `Vouchers::withEntities()` method to accept any `iterable`
712

813
## [v0.6.0 (2025-02-27)](https://github.com/FrittenKeeZ/laravel-vouchers/compare/0.5.0...0.6.0)
914

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ Following methods only apply to `Vouchers::create()` call.
116116
// Add metadata to voucher.
117117
Vouchers::withMetadata(array $metadata);
118118
// Set voucher start time.
119-
Vouchers::withStartTime(DateTime $timestamp);
119+
Vouchers::withStartTime(DateTime|null $timestamp);
120120
// Set voucher start time using interval.
121-
Vouchers::withStartTimeIn(DateInterval $interval);
121+
Vouchers::withStartTimeIn(DateInterval|null $interval);
122122
// Set voucher start date - time component is zeroed.
123-
Vouchers::withStartDate(DateTime $timestamp);
123+
Vouchers::withStartDate(DateTime|null $timestamp);
124124
// Set voucher start date using interval - time component is zeroed.
125-
Vouchers::withStartDateIn(DateInterval $interval);
125+
Vouchers::withStartDateIn(DateInterval|null $interval);
126126
// Set voucher expire time.
127-
Vouchers::withExpireTime(DateTime $timestamp);
127+
Vouchers::withExpireTime(DateTime|null $timestamp);
128128
// Set voucher expire time using interval.
129-
Vouchers::withExpireTimeIn(DateInterval $interval);
129+
Vouchers::withExpireTimeIn(DateInterval|null $interval);
130130
// Set voucher expire date - time component is set to end of day (23:59:59).
131-
Vouchers::withExpireDate(DateTime $timestamp);
131+
Vouchers::withExpireDate(DateTime|null $timestamp);
132132
// Set voucher expire date using interval - time component is set to end of day (23:59:59).
133-
Vouchers::withExpireDateIn(DateInterval $interval);
133+
Vouchers::withExpireDateIn(DateInterval|null $interval);
134134
// Set related entities to voucher - using spread operater.
135135
Vouchers::withEntities(Illuminate\Database\Eloquent\Model ...$entities);
136136
// Set related entities to voucher - iterable.

src/Config.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,35 +174,39 @@ public function getStartTime(): ?Carbon
174174
/**
175175
* With start time.
176176
*/
177-
public function withStartTime(DateTime $timestamp): self
177+
public function withStartTime(?DateTime $timestamp): self
178178
{
179-
Arr::set($this->options, 'starts_at', Carbon::instance($timestamp));
179+
if ($timestamp === null) {
180+
Arr::forget($this->options, 'starts_at');
181+
} else {
182+
Arr::set($this->options, 'starts_at', Carbon::instance($timestamp));
183+
}
180184

181185
return $this;
182186
}
183187

184188
/**
185189
* With start time in the given interval.
186190
*/
187-
public function withStartTimeIn(DateInterval $interval): self
191+
public function withStartTimeIn(?DateInterval $interval): self
188192
{
189-
return $this->withStartTime(Carbon::now()->add($interval));
193+
return $this->withStartTime($interval ? Carbon::now()->add($interval) : null);
190194
}
191195

192196
/**
193197
* With start date - time component is set to 00:00:00.000000.
194198
*/
195-
public function withStartDate(DateTime $timestamp): self
199+
public function withStartDate(?DateTime $timestamp): self
196200
{
197-
return $this->withStartTime(Carbon::instance($timestamp)->startOfDay());
201+
return $this->withStartTime($timestamp ? Carbon::instance($timestamp)->startOfDay() : null);
198202
}
199203

200204
/**
201205
* With start date in the given interval - time component is set to 00:00:00.000000.
202206
*/
203-
public function withStartDateIn(DateInterval $interval): self
207+
public function withStartDateIn(?DateInterval $interval): self
204208
{
205-
return $this->withStartTime(Carbon::now()->add($interval)->startOfDay());
209+
return $this->withStartTime($interval ? Carbon::now()->add($interval)->startOfDay() : null);
206210
}
207211

208212
/**
@@ -216,35 +220,39 @@ public function getExpireTime(): ?Carbon
216220
/**
217221
* With expire time.
218222
*/
219-
public function withExpireTime(DateTime $timestamp): self
223+
public function withExpireTime(?DateTime $timestamp): self
220224
{
221-
Arr::set($this->options, 'expires_at', Carbon::instance($timestamp));
225+
if ($timestamp === null) {
226+
Arr::forget($this->options, 'expires_at');
227+
} else {
228+
Arr::set($this->options, 'expires_at', Carbon::instance($timestamp));
229+
}
222230

223231
return $this;
224232
}
225233

226234
/**
227235
* With expire time in the given interval.
228236
*/
229-
public function withExpireTimeIn(DateInterval $interval): self
237+
public function withExpireTimeIn(?DateInterval $interval): self
230238
{
231-
return $this->withExpireTime(Carbon::now()->add($interval));
239+
return $this->withExpireTime($interval ? Carbon::now()->add($interval) : null);
232240
}
233241

234242
/**
235243
* With expire date - time component is set to 23:59:59.999999.
236244
*/
237-
public function withExpireDate(DateTime $timestamp): self
245+
public function withExpireDate(?DateTime $timestamp): self
238246
{
239-
return $this->withExpireTime(Carbon::instance($timestamp)->endOfDay());
247+
return $this->withExpireTime($timestamp ? Carbon::instance($timestamp)->endOfDay() : null);
240248
}
241249

242250
/**
243251
* With expire date in the given interval - time component is set to 23:59:59.999999.
244252
*/
245-
public function withExpireDateIn(DateInterval $interval): self
253+
public function withExpireDateIn(?DateInterval $interval): self
246254
{
247-
return $this->withExpireTime(Carbon::now()->add($interval)->endOfDay());
255+
return $this->withExpireTime($interval ? Carbon::now()->add($interval)->endOfDay() : null);
248256
}
249257

250258
/**

tests/Feature/ConfigTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,45 @@ function _assert_array_structure(array $expected, array $actual): void
170170
Carbon::now()->toDateTimeString(),
171171
$config->withStartTime(Carbon::now())->getStartTime()->toDateTimeString()
172172
);
173+
$this->assertNull($config->withStartTime(null)->getStartTime());
173174
$this->assertSame(
174175
Carbon::now()->add($interval)->toDateTimeString(),
175176
$config->withStartTimeIn($interval)->getStartTime()->toDateTimeString()
176177
);
178+
$this->assertNull($config->withStartTimeIn(null)->getStartTime());
177179
$this->assertSame(
178180
Carbon::now()->startOfDay()->toDateTimeString(),
179181
$config->withStartDate(Carbon::now())->getStartTime()->toDateTimeString()
180182
);
183+
$this->assertNull($config->withStartDate(null)->getStartTime());
181184
$this->assertSame(
182185
Carbon::now()->add($interval)->startOfDay()->toDateTimeString(),
183186
$config->withStartDateIn($interval)->getStartTime()->toDateTimeString()
184187
);
188+
$this->assertNull($config->withStartDateIn(null)->getStartTime());
185189

186190
// Test expire time.
187191
$interval = CarbonInterval::create('P15DT20M10S');
188192
$this->assertSame(
189193
Carbon::now()->toDateTimeString(),
190194
$config->withExpireTime(Carbon::now())->getExpireTime()->toDateTimeString()
191195
);
196+
$this->assertNull($config->withExpireTime(null)->getExpireTime());
192197
$this->assertSame(
193198
Carbon::now()->add($interval)->toDateTimeString(),
194199
$config->withExpireTimeIn($interval)->getExpireTime()->toDateTimeString()
195200
);
201+
$this->assertNull($config->withExpireTimeIn(null)->getExpireTime());
196202
$this->assertSame(
197203
Carbon::now()->endOfDay()->toDateTimeString(),
198204
$config->withExpireDate(Carbon::now())->getExpireTime()->toDateTimeString()
199205
);
206+
$this->assertNull($config->withExpireDate(null)->getExpireTime());
200207
$this->assertSame(
201208
Carbon::now()->add($interval)->endOfDay()->toDateTimeString(),
202209
$config->withExpireDateIn($interval)->getExpireTime()->toDateTimeString()
203210
);
211+
$this->assertNull($config->withExpireDateIn(null)->getExpireTime());
204212

205213
// Test owner.
206214
$owner = User::factory()->make();

0 commit comments

Comments
 (0)