1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\SoftDeleteable\Mapping\Driver\Fluent as FluentDriver; |
6
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
7
|
|
|
use LaravelDoctrine\Fluent\Builders\Builder; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
10
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
11
|
|
|
|
12
|
|
|
class SoftDeleteable implements Buildable, Extension |
13
|
|
|
{ |
14
|
|
|
const MACRO_METHOD = 'softDelete'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ExtensibleClassMetadata |
18
|
|
|
*/ |
19
|
|
|
protected $classMetadata; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $fieldName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $value; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $timeAware = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
38
|
|
|
* @param string $fieldName |
39
|
|
|
*/ |
40
|
4 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
41
|
|
|
{ |
42
|
4 |
|
$this->classMetadata = $classMetadata; |
43
|
4 |
|
$this->fieldName = $fieldName; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the name of the actual extension. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
2 |
|
public function getExtensionName() |
52
|
|
|
{ |
53
|
2 |
|
return FluentDriver::EXTENSION_NAME; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param bool $value |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
1 |
|
public function timeAware($value = true) |
61
|
|
|
{ |
62
|
1 |
|
$this->timeAware = $value; |
63
|
|
|
|
64
|
1 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
80 |
|
public static function enable() |
71
|
|
|
{ |
72
|
|
|
Builder::macro(static::MACRO_METHOD, function (Builder $builder, $fieldName = 'deletedAt', $type = 'dateTime') { |
73
|
|
|
|
74
|
1 |
|
$builder->{$type}($fieldName)->nullable(); |
75
|
|
|
|
76
|
1 |
|
return new static($builder->getClassMetadata(), $fieldName); |
|
|
|
|
77
|
80 |
|
}); |
78
|
|
|
|
79
|
80 |
|
Field::macro(static::MACRO_METHOD, function (Field $builder) { |
80
|
1 |
|
return new static($builder->getClassMetadata(), $builder->getName()); |
81
|
80 |
|
}); |
82
|
80 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Execute the build process |
86
|
|
|
*/ |
87
|
2 |
|
public function build() |
88
|
|
|
{ |
89
|
2 |
|
$this->classMetadata->addExtension($this->getExtensionName(), [ |
90
|
2 |
|
'softDeleteable' => true, |
91
|
2 |
|
'fieldName' => $this->fieldName, |
92
|
2 |
|
'timeAware' => $this->timeAware |
93
|
2 |
|
]); |
94
|
2 |
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.