Skip to content

Commit 7846df6

Browse files
Merge branch 'release/1.0.0'
2 parents 80f1110 + d1f44d3 commit 7846df6

File tree

10 files changed

+334
-38
lines changed

10 files changed

+334
-38
lines changed

CHANGELOG.md

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

77
## Unreleased
88

9+
## 1.0.0 (2018-08-19)
10+
11+
### Fixed
12+
13+
- Removed `$booleanDates` trait variable to resolve conflict with the model it's used in
14+
- Always return a valid array when retrieving all boolean dates
15+
- Fixed issue where converting a model to an array didn't include the correct boolean field values
16+
917
## 0.2.1 (2018-07-26)
1018

1119
### Fixed

README.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Easily convert Eloquent model booleans to dates and back
1+
# Easily convert Eloquent model boolean fields to dates and back
22

33
[![Latest stable release][version-badge]][link-packagist]
44
[![Software license][license-badge]](LICENSE.md)
@@ -76,7 +76,7 @@ class User extends Model
7676
}
7777
```
7878

79-
Adding the boolean dates _values_ to the `$dates` array is optional, but encouraged as it'll convert all your boolean datetimes to Carbon instances.
79+
Adding the boolean dates _values_ to the `$dates` array is **optional**, but encouraged as it'll convert all your boolean datetimes to Carbon instances.
8080

8181
## How to use
8282

@@ -108,12 +108,18 @@ Of course you can also remove the saved date and time, for instance if a user re
108108
```php
109109
$user = User::findOrFail(42);
110110

111-
$user->allows_data_processing = false;
111+
$user->has_accepted_terms_and_conditions = false;
112+
// $user->has_accepted_terms_and_conditions = null;
113+
114+
$user->allows_data_processing = 0;
115+
// $user->allows_data_processing = '0';
116+
117+
$user->has_agreed_to_something = '';
112118

113119
$user->save();
114120
```
115121

116-
False values are converted to `NULL`.
122+
False or false-y values are converted to `NULL`.
117123

118124
### Retrieving values
119125

@@ -127,19 +133,7 @@ $user = User::findOrFail(42);
127133
$user->has_accepted_terms_and_conditions;
128134

129135
/*
130-
* true (boolean)
131-
*/
132-
133-
$user->allows_data_processing;
134-
135-
/*
136-
* false (boolean)
137-
*/
138-
139-
$user->has_agreed_to_something;
140-
141-
/*
142-
* true (boolean)
136+
* true or false (boolean)
143137
*/
144138
```
145139

@@ -161,12 +155,6 @@ $user->accepted_processing_at;
161155
/*
162156
* NULL
163157
*/
164-
165-
$user->agreed_to_something_at;
166-
167-
/*
168-
* 2018-05-10 16:24:22 (string or Carbon instance)
169-
*/
170158
```
171159

172160
### Array conversion
@@ -179,10 +167,10 @@ $user = User::findOrFail(42);
179167
$user->toArray();
180168

181169
/*
182-
* Which will return:
170+
* Which will return something like:
183171
*
184172
* [
185-
* 'accepted_terms_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
173+
* 'accepted_terms_at' => '2018-05-10 16:24:22',
186174
* 'accepted_processing_at' => NULL,
187175
* 'agreed_to_something_at' => \Carbon\Carbon('2018-05-10 16:24:22'),
188176
* 'accepted_terms_and_conditions' => true,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
}
2525
],
2626
"require": {
27-
"ext-json": "*",
2827
"laravel/framework": "^5.6",
2928
"nesbot/carbon": "^1.22",
3029
"php": "^7.2"
3130
},
3231
"require-dev": {
3332
"kint-php/kint": "^2.2",
33+
"mockery/mockery": "^1.1",
3434
"orchestra/testbench": "^3.6",
3535
"phpunit/phpunit": "^7.2"
3636
},

src/BooleanDates.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88

99
trait BooleanDates
1010
{
11-
/**
12-
* Set the date of fields to the current date and time if a counterpart boolean field is
13-
* true-ish.
14-
*
15-
* Keys and values should be in the format: `'boolean_field' => 'internal_timestamp_field'`.
16-
*
17-
* @var array
18-
*/
19-
protected $booleanDates = [];
20-
2111
/**
2212
* Convert the model's attributes to an array.
2313
*
@@ -76,7 +66,7 @@ public function setAttribute($key, $value)
7666
*/
7767
public function getBooleanDates() : array
7868
{
79-
return $this->booleanDates;
69+
return $this->booleanDates ?? [];
8070
}
8171

8272
/**
@@ -173,7 +163,7 @@ protected function addBooleanDateAttributesToArray(array $attributes) : array
173163
continue;
174164
}
175165

176-
$attributes[$booleanField] = $date !== null;
166+
$attributes[$booleanField] = $this->getBooleanDate($booleanField);
177167
}
178168

179169
return $attributes;

tests/Feature/.gitkeep

Whitespace-only changes.

tests/Feature/BooleanArrayTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
6+
7+
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
9+
use SebastiaanLuca\BooleanDates\Tests\TestCase;
10+
11+
class BooleanArrayTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it returns all boolean dates() : void
17+
{
18+
$model = new TestModel;
19+
20+
$expected = [
21+
'has_accepted_terms_and_conditions' => 'accepted_terms_at',
22+
'allows_data_processing' => 'accepted_processing_at',
23+
'has_agreed_to_something' => 'agreed_to_something_at',
24+
];
25+
26+
$this->assertSame(
27+
$expected,
28+
$model->getBooleanDates()
29+
);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function it returns all attributes() : void
36+
{
37+
Carbon::setTestNow('2018-01-01 10:42:06');
38+
39+
$model = new TestModel;
40+
41+
$model->something = 'something';
42+
$model->tested_at = Carbon::now();
43+
44+
$model->has_accepted_terms_and_conditions = false;
45+
$model->allows_data_processing = true;
46+
$model->has_agreed_to_something = '0';
47+
48+
$expected = [
49+
'something' => 'something',
50+
'tested_at' => '2018-01-01 10:42:06',
51+
52+
'has_accepted_terms_and_conditions' => false,
53+
'allows_data_processing' => true,
54+
'has_agreed_to_something' => false,
55+
56+
'accepted_terms_at' => null,
57+
'accepted_processing_at' => '2018-01-01 10:42:06',
58+
'agreed_to_something_at' => null,
59+
];
60+
61+
$this->assertArraySubset(
62+
$expected,
63+
$model->toArray()
64+
);
65+
}
66+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SebastiaanLuca\BooleanDates\Tests\Feature;
6+
7+
use Carbon\Carbon;
8+
use SebastiaanLuca\BooleanDates\Tests\resources\TestModel;
9+
use SebastiaanLuca\BooleanDates\Tests\TestCase;
10+
11+
class BooleanAttributeTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it skips returning invalid fields() : void
17+
{
18+
$model = new TestModel;
19+
20+
$this->assertNull($model->getAttribute(null));
21+
$this->assertNull($model->getAttribute(false));
22+
$this->assertNull($model->getAttribute(''));
23+
$this->assertNull($model->getAttribute('0'));
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function it leaves other attributes untouched() : void
30+
{
31+
$model = new TestModel;
32+
33+
$model->something = 'something';
34+
35+
$this->assertSame(
36+
'something',
37+
$model->something
38+
);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function it sets the date from a true boolean() : void
45+
{
46+
$model = new TestModel;
47+
48+
$model->has_accepted_terms_and_conditions = true;
49+
50+
$this->assertSame(
51+
Carbon::now()->format('Y-m-d H:i:s'),
52+
$model->accepted_terms_at
53+
);
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function it sets the date from a non empty string() : void
60+
{
61+
$model = new TestModel;
62+
63+
$model->has_accepted_terms_and_conditions = 'yes';
64+
65+
$this->assertSame(
66+
Carbon::now()->format('Y-m-d H:i:s'),
67+
$model->accepted_terms_at
68+
);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function it sets the date from a positive integer value() : void
75+
{
76+
$model = new TestModel;
77+
78+
$model->has_agreed_to_something = 1;
79+
80+
$this->assertSame(
81+
Carbon::now()->format('Y-m-d H:i:s'),
82+
$model->agreed_to_something_at
83+
);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function it sets the date from a positive integer string value() : void
90+
{
91+
$model = new TestModel;
92+
93+
$model->has_accepted_terms_and_conditions = '1';
94+
95+
$this->assertSame(
96+
Carbon::now()->format('Y-m-d H:i:s'),
97+
$model->accepted_terms_at
98+
);
99+
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function it clears the date from a false boolean() : void
105+
{
106+
$model = new TestModel;
107+
108+
$model->allows_data_processing = false;
109+
110+
$this->assertNull($model->accepted_processing_at);
111+
}
112+
113+
/**
114+
* @test
115+
*/
116+
public function it clears the date from null() : void
117+
{
118+
$model = new TestModel;
119+
120+
$model->has_agreed_to_something = null;
121+
122+
$this->assertNull($model->agreed_to_something_at);
123+
}
124+
125+
/**
126+
* @test
127+
*/
128+
public function it clears the date from a false string value() : void
129+
{
130+
$model = new TestModel;
131+
132+
$model->has_accepted_terms_and_conditions = '0';
133+
134+
$this->assertNull($model->accepted_terms_at);
135+
}
136+
137+
/**
138+
* @test
139+
*/
140+
public function it clears the date from an empty string value() : void
141+
{
142+
$model = new TestModel;
143+
144+
$model->has_accepted_terms_and_conditions = '0';
145+
146+
$this->assertNull($model->accepted_terms_at);
147+
}
148+
}

0 commit comments

Comments
 (0)