Skip to content

Commit 9913a6d

Browse files
Merge branch 'release/2.0.0'
2 parents 04f54e2 + e8ab181 commit 9913a6d

File tree

9 files changed

+103
-54
lines changed

9 files changed

+103
-54
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

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

1126
### Fixed

README.md

Lines changed: 9 additions & 22 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

@@ -66,7 +66,7 @@ $user->accepted_terms_and_conditions_at;
6666
## Requirements
6767

6868
- PHP 7.2 or higher
69-
- Laravel 5.6 or higher
69+
- Laravel 5.8 or higher
7070

7171
## How to install
7272

@@ -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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
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+
"kint-php/kint": "^3.1",
33+
"mockery/mockery": "^1.2",
34+
"orchestra/testbench": "^3.8",
35+
"phpunit/phpunit": "^8.0"
3636
},
3737
"autoload": {
3838
"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/Assert.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests;
6+
7+
use ArrayAccess;
8+
use PHPUnit\Framework\Assert as PHPUnit;
9+
use PHPUnit\Framework\Constraint\ArraySubset;
10+
use PHPUnit\Util\InvalidArgumentHelper;
11+
12+
class Assert extends PHPUnit
13+
{
14+
/**
15+
* Asserts that an array has a specified subset.
16+
*
17+
* This method was taken over from PHPUnit where it was deprecated. See link for more info.
18+
*
19+
* @param array|\ArrayAccess $subset
20+
* @param array|\ArrayAccess $array
21+
* @param bool $checkForObjectIdentity
22+
* @param string $message
23+
*
24+
* @return void
25+
*
26+
* @throws \Exception
27+
*
28+
* @link https://github.com/sebastianbergmann/phpunit/issues/3494
29+
*/
30+
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = '') : void
31+
{
32+
if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
33+
throw InvalidArgumentHelper::factory(1, 'array or ArrayAccess');
34+
}
35+
36+
if (! (is_array($array) || $array instanceof ArrayAccess)) {
37+
throw InvalidArgumentHelper::factory(2, 'array or ArrayAccess');
38+
}
39+
40+
$constraint = new ArraySubset($subset, $checkForObjectIdentity);
41+
42+
static::assertThat($array, $constraint, $message);
43+
}
44+
}

tests/Feature/BooleanArrayTest.php

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

77
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\Assert;
89
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
910
use SebastiaanLuca\BooleanDates\Tests\TestCase;
1011

@@ -58,7 +59,7 @@ public function it returns all attributes() : void
5859
'agreed_to_something_at' => null,
5960
];
6061

61-
$this->assertArraySubset(
62+
Assert::assertArraySubset(
6263
$expected,
6364
$model->toArray()
6465
);

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)