-
Notifications
You must be signed in to change notification settings - Fork 22
/
SoftDeleteable.php
95 lines (80 loc) · 2.18 KB
/
SoftDeleteable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
use Gedmo\SoftDeleteable\Mapping\Driver\Fluent as FluentDriver;
use LaravelDoctrine\Fluent\Buildable;
use LaravelDoctrine\Fluent\Builders\Builder;
use LaravelDoctrine\Fluent\Builders\Field;
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
use LaravelDoctrine\Fluent\Extensions\Extension;
class SoftDeleteable implements Buildable, Extension
{
const MACRO_METHOD = 'softDelete';
/**
* @var ExtensibleClassMetadata
*/
protected $classMetadata;
/**
* @var string
*/
protected $fieldName;
/**
* @var string
*/
protected $value;
/**
* @var bool
*/
protected $timeAware = false;
/**
* @param ExtensibleClassMetadata $classMetadata
* @param string $fieldName
*/
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
{
$this->classMetadata = $classMetadata;
$this->fieldName = $fieldName;
}
/**
* Return the name of the actual extension.
*
* @return string
*/
public function getExtensionName()
{
return FluentDriver::EXTENSION_NAME;
}
/**
* @param bool $value
*
* @return $this
*/
public function timeAware($value = true)
{
$this->timeAware = $value;
return $this;
}
/**
* @return void
*/
public static function enable()
{
Builder::macro(static::MACRO_METHOD, function (Builder $builder, $fieldName = 'deletedAt', $type = 'dateTime') {
$builder->{$type}($fieldName)->nullable();
return new static($builder->getClassMetadata(), $fieldName);
});
Field::macro(static::MACRO_METHOD, function (Field $builder) {
return new static($builder->getClassMetadata(), $builder->getName());
});
}
/**
* Execute the build process.
*/
public function build()
{
$this->classMetadata->addExtension($this->getExtensionName(), [
'softDeleteable' => true,
'fieldName' => $this->fieldName,
'timeAware' => $this->timeAware,
]);
}
}