Skip to content

Commit c813773

Browse files
Merge branch 'feature/use-trait-initializer' into develop
2 parents 477b45e + e2bb9cd commit c813773

File tree

8 files changed

+59
-53
lines changed

8 files changed

+59
-53
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: php
22

33
php:
4-
- 7.2
4+
- "7.2"
5+
- "7.3"
56
- nightly
67

78
cache:
@@ -10,10 +11,8 @@ cache:
1011

1112
env:
1213
matrix:
13-
- LARAVEL_VERSION="5.6.*" COMPOSER_FLAGS="--prefer-lowest"
14-
- LARAVEL_VERSION="5.6.*" COMPOSER_FLAGS="--prefer-stable"
15-
- LARAVEL_VERSION="5.7.*" COMPOSER_FLAGS="--prefer-lowest"
16-
- LARAVEL_VERSION="5.7.*" COMPOSER_FLAGS="--prefer-stable"
14+
- LARAVEL_VERSION="5.8.*" COMPOSER_FLAGS="--prefer-lowest"
15+
- LARAVEL_VERSION="5.8.*" COMPOSER_FLAGS="--prefer-stable"
1716
- LARAVEL_VERSION="dev-master" ORCHESTRA_VERSION="dev-master" COMPOSER_FLAGS="--prefer-lowest" MINIMUM_STABILITY="dev"
1817
- LARAVEL_VERSION="dev-master" ORCHESTRA_VERSION="dev-master" COMPOSER_FLAGS="--prefer-stable" MINIMUM_STABILITY="dev"
1918

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

9+
### Added
10+
11+
- Automatically detect a model's boolean date fields
12+
13+
### Changed
14+
15+
- Add support for Laravel 5.8 (requires PHP 7.2 or higher)
16+
- Renamed `BooleanDates` trait to `HasBooleanDates`
17+
18+
### Removed
19+
20+
- Dropped support for Laravel 5.7 and lower
21+
922
## 1.1.1 (2018-09-04)
1023

1124
### Fixed

README.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
[![Follow @sebastiaanluca on Twitter][twitter-profile-badge]][link-twitter]
1111
[![Share this package on Twitter][twitter-share-badge]][link-twitter-share]
1212

13-
Say you've got a registration page for users where they need to accept your terms and perhaps can opt-in to certain features using checkboxes. With the new(-ish) GDPR privacy laws, you're somewhat required to not just keep track of the fact *if* they accepted those (or not), but also *when* they did.
13+
**A package to automatically convert boolean fields to dates (and back to booleans) so you always know when something was accepted or changed.**
1414

15-
**This package automatically converts those boolean fields to dates so you always know when something was accepted or changed.**
15+
Say you've got a registration page for users where they need to accept your terms and perhaps can opt-in to certain features using checkboxes. With the new(-ish) GDPR privacy laws, you're somewhat required to not just keep track of the fact *if* they accepted those (or not), but also *when* they did.
1616

1717
### Example
1818

@@ -40,7 +40,7 @@ $user->has_accepted_terms_and_conditions;
4040
$user->accepted_terms_and_conditions_at;
4141

4242
/*
43-
* 2018-05-10 16:24:22 (string or Carbon instance)
43+
* 2018-05-10 16:24:22 (Carbon instance)
4444
*/
4545
```
4646

@@ -76,17 +76,17 @@ $user->accepted_terms_and_conditions_at;
7676
composer require sebastiaanluca/laravel-boolean-dates
7777
```
7878

79-
**Require the `BooleanDates` trait** in your Eloquent model, then add the `$booleanDates` and `$dates` (optional) fields:
79+
**Require the `HasBooleanDates` trait** in your Eloquent model, then add the `$booleanDates` field:
8080

8181
```php
8282
<?php
8383

8484
use Illuminate\Database\Eloquent\Model;
85-
use SebastiaanLuca\BooleanDates\BooleanDates;
85+
use SebastiaanLuca\BooleanDates\HasBooleanDates;
8686

8787
class User extends Model
8888
{
89-
use BooleanDates;
89+
use HasBooleanDates;
9090

9191
/**
9292
* @var array
@@ -96,22 +96,9 @@ class User extends Model
9696
'allows_data_processing' => 'accepted_processing_at',
9797
'has_agreed_to_something' => 'agreed_to_something_at',
9898
];
99-
100-
/**
101-
* The attributes that should be mutated to dates.
102-
*
103-
* @var array
104-
*/
105-
protected $dates = [
106-
'accepted_terms_at',
107-
'accepted_processing_at',
108-
'agreed_to_something_at',
109-
];
11099
}
111100
```
112101

113-
Adding the boolean date fields to the `$dates` array is **optional**, but encouraged as it'll convert all your boolean datetimes to Carbon instances.
114-
115102
To wrap up, create a **migration** to create a new or alter your existing table and add the timestamp fields:
116103

117104
```php
@@ -210,7 +197,7 @@ $user = User::findOrFail(42);
210197
$user->accepted_terms_at;
211198

212199
/*
213-
* 2018-05-10 16:24:22 (string or Carbon instance)
200+
* 2018-05-10 16:24:22 (Carbon instance)
214201
*/
215202

216203
$user->accepted_processing_at;
@@ -233,7 +220,7 @@ $user->toArray();
233220
* Which will return something like:
234221
*
235222
* [
236-
* 'accepted_terms_at' => '2018-05-10 16:24:22',
223+
* 'accepted_terms_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
237224
* 'accepted_processing_at' => NULL,
238225
* 'agreed_to_something_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
239226
* 'accepted_terms_and_conditions' => true,

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
}
2525
],
2626
"require": {
27-
"laravel/framework": "^5.6",
27+
"laravel/framework": "^5.8",
2828
"nesbot/carbon": "^1.22.1|^2.0",
2929
"php": "^7.2"
3030
},
3131
"require-dev": {
32-
"kint-php/kint": "^2.2",
33-
"mockery/mockery": "^1.1",
34-
"orchestra/testbench": "^3.6",
35-
"phpunit/phpunit": "^7.2"
32+
"dms/phpunit-arraysubset-asserts": "^0.1.0",
33+
"kint-php/kint": "^3.1",
34+
"mockery/mockery": "^1.2",
35+
"orchestra/testbench": "^3.8",
36+
"phpunit/phpunit": "^8.0"
3637
},
3738
"autoload": {
3839
"psr-4": {

src/BooleanDates.php renamed to src/HasBooleanDates.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66

77
use Carbon\Carbon;
88

9-
trait BooleanDates
9+
trait HasBooleanDates
1010
{
11+
/**
12+
* Initialize the trait.
13+
*/
14+
public function initializeHasBooleanDates() : void
15+
{
16+
$this->dates = array_unique(
17+
array_merge(
18+
$this->dates,
19+
array_values($this->getBooleanDates())
20+
)
21+
);
22+
}
23+
1124
/**
1225
* Convert the model's attributes to an array.
1326
*

tests/Feature/BooleanArrayTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
66

77
use Carbon\Carbon;
8+
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
89
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
910
use SebastiaanLuca\BooleanDates\Tests\TestCase;
1011

1112
class BooleanArrayTest extends TestCase
1213
{
14+
use ArraySubsetAsserts;
15+
1316
/**
1417
* @test
1518
*/
@@ -58,7 +61,7 @@ public function it returns all attributes() : void
5861
'agreed_to_something_at' => null,
5962
];
6063

61-
$this->assertArraySubset(
64+
static::assertArraySubset(
6265
$expected,
6366
$model->toArray()
6467
);

tests/Feature/BooleanAttributeTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function it sets the date from a true boolean() : void
4848
$model->has_accepted_terms_and_conditions = true;
4949

5050
$this->assertSame(
51-
Carbon::now()->format('Y-m-d H:i:s'),
52-
$model->accepted_terms_at
51+
Carbon::now()->format('Y-m-d H:i'),
52+
$model->accepted_terms_at->format('Y-m-d H:i')
5353
);
5454
}
5555

@@ -63,8 +63,8 @@ public function it sets the date from a non empty string() : void
6363
$model->has_accepted_terms_and_conditions = 'yes';
6464

6565
$this->assertSame(
66-
Carbon::now()->format('Y-m-d H:i:s'),
67-
$model->accepted_terms_at
66+
Carbon::now()->format('Y-m-d H:i'),
67+
$model->accepted_terms_at->format('Y-m-d H:i')
6868
);
6969
}
7070

@@ -78,8 +78,8 @@ public function it sets the date from a positive integer value() : void
7878
$model->has_agreed_to_something = 1;
7979

8080
$this->assertSame(
81-
Carbon::now()->format('Y-m-d H:i:s'),
82-
$model->agreed_to_something_at
81+
Carbon::now()->format('Y-m-d H:i'),
82+
$model->agreed_to_something_at->format('Y-m-d H:i')
8383
);
8484
}
8585

@@ -93,8 +93,8 @@ public function it sets the date from a positive integer string value()
9393
$model->has_accepted_terms_and_conditions = '1';
9494

9595
$this->assertSame(
96-
Carbon::now()->format('Y-m-d H:i:s'),
97-
$model->accepted_terms_at
96+
Carbon::now()->format('Y-m-d H:i'),
97+
$model->accepted_terms_at->format('Y-m-d H:i')
9898
);
9999
}
100100

tests/resources/TestModel.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace SebastiaanLuca\BooleanDates\Tests\resources;
66

77
use Illuminate\Database\Eloquent\Model;
8-
use SebastiaanLuca\BooleanDates\BooleanDates;
8+
use SebastiaanLuca\BooleanDates\HasBooleanDates;
99

1010
class TestModel extends Model
1111
{
12-
use BooleanDates;
12+
use HasBooleanDates;
1313

1414
/**
1515
* Set the date of fields to the current date and time if a counterpart boolean field is
@@ -24,14 +24,4 @@ class TestModel extends Model
2424
'allows_data_processing' => 'accepted_processing_at',
2525
'has_agreed_to_something' => 'agreed_to_something_at',
2626
];
27-
28-
/**
29-
* The attributes that should be mutated to dates.
30-
*
31-
* @var array
32-
*/
33-
protected $dates = [
34-
'accepted_processing_at',
35-
'tested_at',
36-
];
3727
}

0 commit comments

Comments
 (0)