Skip to content

Commit 4ad2bc9

Browse files
committed
Merge branch 'release/1.0.0' into main
2 parents 81729ad + c42a637 commit 4ad2bc9

File tree

14 files changed

+1027
-17
lines changed

14 files changed

+1027
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6-
## Unreleased
6+
## [1.0.0] - 2021-07-31
7+
8+
Initial release.

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@
2323
}
2424
],
2525
"require": {
26-
"php": "^7.4|^8.0"
26+
"php": "^7.4|^8.0",
27+
"laravel-json-api/eloquent": "^1.0.0",
28+
"tenantcloud/laravel-boolean-softdeletes": "^3.1"
2729
},
2830
"require-dev": {
31+
"orchestra/testbench": "^6.17",
2932
"phpunit/phpunit": "^9.5"
3033
},
3134
"autoload": {
3235
"psr-4": {
33-
"LaravelJsonApi\\BooleanSoftDeletes\\": "src/"
36+
"LaravelJsonApi\\BooleanSoftDeletes\\": "src"
3437
}
3538
},
3639
"autoload-dev": {
3740
"psr-4": {
38-
"LaravelJsonApi\\BooleanSoftDeletes\\Tests\\": "tests/"
41+
"App\\": "tests/app",
42+
"Database\\Factories\\": "tests/database/factories",
43+
"LaravelJsonApi\\BooleanSoftDeletes\\Tests\\": "tests/lib"
3944
}
4045
},
4146
"extra": {

phpunit.xml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2021 Cloud Creativity Limited
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
218
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
319
backupGlobals="false"
420
backupStaticAttributes="false"
@@ -14,17 +30,23 @@
1430
verbose="true"
1531
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
1632
>
17-
<coverage>
18-
<include>
19-
<directory suffix=".php">src/</directory>
20-
</include>
21-
</coverage>
22-
<testsuites>
23-
<testsuite name="Unit">
24-
<directory suffix="Test.php">./tests/Unit/</directory>
25-
</testsuite>
26-
<testsuite name="Integration">
27-
<directory suffix="Test.php">./tests/Integration/</directory>
28-
</testsuite>
29-
</testsuites>
33+
<coverage>
34+
<include>
35+
<directory suffix=".php">src/</directory>
36+
</include>
37+
</coverage>
38+
<testsuites>
39+
<testsuite name="Unit">
40+
<directory suffix="Test.php">./tests/lib/Unit/</directory>
41+
</testsuite>
42+
<testsuite name="Integration">
43+
<directory suffix="Test.php">./tests/lib/Integration/</directory>
44+
</testsuite>
45+
<testsuite name="Acceptance">
46+
<directory suffix="Test.php">./tests/lib/Acceptance/</directory>
47+
</testsuite>
48+
</testsuites>
49+
<php>
50+
<env name="DB_CONNECTION" value="testing"/>
51+
</php>
3052
</phpunit>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\BooleanSoftDeletes\Drivers;
21+
22+
use Illuminate\Database\Eloquent\Builder;
23+
use Illuminate\Database\Eloquent\Model;
24+
use InvalidArgumentException;
25+
use LaravelJsonApi\Eloquent\Drivers\StandardDriver;
26+
use Webkid\LaravelBooleanSoftdeletes\SoftDeletesBoolean;
27+
use function boolval;
28+
29+
class SoftDeleteBooleanDriver extends StandardDriver
30+
{
31+
32+
/**
33+
* SoftDeleteDriver constructor.
34+
*
35+
* @param Model|SoftDeletesBoolean $model
36+
*/
37+
public function __construct($model)
38+
{
39+
if (!in_array(SoftDeletesBoolean::class, class_uses($model), true)) {
40+
throw new InvalidArgumentException('Expecting a model that is boolean soft-deletable.');
41+
}
42+
43+
parent::__construct($model);
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function query(): Builder
50+
{
51+
/**
52+
* When querying specific resources, we use `withTrashed` as we want trashed
53+
* resources to exist in our API.
54+
*/
55+
return parent::query()->withTrashed();
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
public function persist(Model $model): bool
62+
{
63+
/**
64+
* If the model is being restored, the restore method executes a
65+
* save on the model. So we only need to run the restore method and all
66+
* dirty attributes will be saved.
67+
*/
68+
if ($this->willRestore($model)) {
69+
return $this->restore($model);
70+
}
71+
72+
/**
73+
* To ensure Laravel still executes its soft-delete logic (e.g. firing events)
74+
* we need to delete before a save when we are soft-deleting. Although this
75+
* may result in two database calls in this scenario, it means we can guarantee
76+
* that standard Laravel soft-delete logic is executed.
77+
*/
78+
if ($this->willSoftDelete($model)) {
79+
$model->delete();
80+
}
81+
82+
return (bool) $model->save();
83+
}
84+
85+
/**
86+
* @inheritDoc
87+
*/
88+
public function destroy(Model $model): bool
89+
{
90+
return (bool) $model->forceDelete();
91+
}
92+
93+
/**
94+
* @param Model|SoftDeletesBoolean $model
95+
* @return bool
96+
*/
97+
private function restore(Model $model): bool
98+
{
99+
return (bool) $model->restore();
100+
}
101+
102+
/**
103+
* Will the hydration operation restore the model?
104+
*
105+
* @param Model|SoftDeletesBoolean $model
106+
* @return bool
107+
*/
108+
private function willRestore(Model $model): bool
109+
{
110+
if (!$model->exists) {
111+
return false;
112+
}
113+
114+
$column = $model->getIsDeletedColumn();
115+
116+
return false !== boolval($model->getOriginal($column)) && false === boolval($model->{$column});
117+
}
118+
119+
/**
120+
* Will the hydration operation result in the model being soft deleted?
121+
*
122+
* @param Model|SoftDeletesBoolean $model
123+
* @return bool
124+
*/
125+
private function willSoftDelete(Model $model): bool
126+
{
127+
if (!$model->exists) {
128+
return false;
129+
}
130+
131+
$column = $model->getIsDeletedColumn();
132+
133+
return false === boolval($model->getOriginal($column)) && false !== boolval($model->{$column});
134+
}
135+
136+
}

src/SoftDeletesBoolean.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\BooleanSoftDeletes;
21+
22+
use LaravelJsonApi\BooleanSoftDeletes\Drivers\SoftDeleteBooleanDriver;
23+
use LaravelJsonApi\Eloquent\Contracts\Driver;
24+
25+
trait SoftDeletesBoolean
26+
{
27+
28+
/**
29+
* @return Driver
30+
*/
31+
protected function driver(): Driver
32+
{
33+
return new SoftDeleteBooleanDriver($this->newInstance());
34+
}
35+
}

tests/Unit/.gitkeep

Whitespace-only changes.

tests/app/Models/Post.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace App\Models;
21+
22+
use Illuminate\Database\Eloquent\Factories\HasFactory;
23+
use Illuminate\Database\Eloquent\Model;
24+
use Webkid\LaravelBooleanSoftdeletes\SoftDeletesBoolean;
25+
26+
class Post extends Model
27+
{
28+
29+
use HasFactory;
30+
use SoftDeletesBoolean;
31+
32+
/**
33+
* @var string[]
34+
*/
35+
protected $fillable = [
36+
'content',
37+
'slug',
38+
'title',
39+
];
40+
41+
/**
42+
* @var string[]
43+
*/
44+
protected $casts = [
45+
'is_deleted' => 'boolean',
46+
];
47+
}

0 commit comments

Comments
 (0)