1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
6
|
|
|
use Gedmo\Uploadable\Mapping\Driver\Fluent as UploadableDriver; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
10
|
|
|
|
11
|
|
|
class UploadableFile implements Buildable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ExtensibleClassMetadata |
15
|
|
|
*/ |
16
|
|
|
private $classMetadata; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $fieldName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $validTypes = ['Path', 'Name', 'Size', 'MimeType']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* UploadableFile constructor. |
35
|
|
|
* |
36
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
37
|
|
|
* @param string $fieldName |
38
|
|
|
* @param string $type |
39
|
|
|
*/ |
40
|
83 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $type = 'Name') |
41
|
|
|
{ |
42
|
83 |
|
$this->validateType($type); |
43
|
|
|
|
44
|
82 |
|
$this->classMetadata = $classMetadata; |
45
|
82 |
|
$this->fieldName = $fieldName; |
46
|
82 |
|
$this->type = "file{$type}Field"; |
47
|
82 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Enable the UploadableFile extension. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
78 |
|
public static function enable() |
55
|
|
|
{ |
56
|
78 |
|
foreach (self::$validTypes as $type) { |
57
|
78 |
|
Field::macro("asFile$type", function (Field $builder) use ($type) { |
58
|
77 |
|
return new static($builder->getClassMetadata(), $builder->getName(), $type); |
59
|
78 |
|
}); |
60
|
78 |
|
} |
61
|
78 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the build process |
65
|
|
|
*/ |
66
|
82 |
|
public function build() |
67
|
|
|
{ |
68
|
82 |
|
$this->classMetadata->appendExtension(UploadableDriver::EXTENSION_NAME, [ |
69
|
82 |
|
$this->type => $this->fieldName, |
70
|
82 |
|
]); |
71
|
82 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Validate the given type of file field. |
75
|
|
|
* |
76
|
|
|
* @param string $type |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
83 |
|
private function validateType($type) |
81
|
|
|
{ |
82
|
83 |
|
if (!in_array($type, self::$validTypes)) { |
83
|
1 |
|
throw new InvalidMappingException( |
84
|
1 |
|
'Invalid uploadable field type reference. Must be one of: ' . implode(', ', self::$validTypes) |
85
|
1 |
|
); |
86
|
|
|
} |
87
|
82 |
|
} |
88
|
|
|
} |
89
|
|
|
|