-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPropertyMappingFactory.php
More file actions
142 lines (115 loc) · 4.6 KB
/
Copy pathPropertyMappingFactory.php
File metadata and controls
142 lines (115 loc) · 4.6 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Iphp\FileStoreBundle\Mapping;
use Iphp\FileStoreBundle\Mapping\PropertyMapping;
use Iphp\FileStoreBundle\Driver\AnnotationDriver;
use Iphp\FileStoreBundle\Naming\NamerServiceInvoker;
use Iphp\FileStoreBundle\Mapping\Annotation\UploadableField;
/**
* PropertyMappingFactory.
*
* @author Vitiko <[email protected]>
*/
class PropertyMappingFactory
{
/**
* @var \Iphp\FileStoreBundle\Naming\NamerServiceInvoker $namerServiceInvoker
*/
protected $namerServiceInvoker;
/**
* @var \Iphp\FileStoreBundle\Driver\AnnotationDriver $driver
*/
protected $driver;
/**
* @var array $mappingsConfig MappingConfiguration
*/
protected $mappingsConfig = array();
/**
* Constructs a new instance of PropertyMappingFactory.
*
* @param \Iphp\FileStoreBundle\Naming\NamerServiceInvoker $namerServiceInvoker Object for invoke rename methods.
* @param \Iphp\FileStoreBundle\Driver\AnnotationDriver $driver The driver.
* @param array $mappings The configured mappings.
*/
public function __construct(NamerServiceInvoker $namerServiceInvoker,
AnnotationDriver $driver,
array $mappingsConfig)
{
$this->namerServiceInvoker = $namerServiceInvoker;
$this->driver = $driver;
$this->mappingsConfig = $mappingsConfig;
}
/**
* Creates an array of PropetyMapping objects which contain the
* configuration for the uploadable fields in the specified
* object.
*
* @param object $obj The object.
* @param \ReflectionClass $class
* @return \Iphp\FileStoreBundle\Mapping\PropertyMapping[] objects.
*/
public function getMappingsFromObject($obj, \ReflectionClass $class)
{
if (!$this->hasAnnotations($class)) return array();
$mappings = array();
foreach ($this->driver->readUploadableFields($class) as $field) {
$mappings[] = $this->createMapping($obj, $class, $field);
}
return $mappings;
}
/**
* Creates a property mapping object which contains the
* configuration for the specified uploadable field.
*
* @param object $obj The object.
* @param \ReflectionClass $class
* @param string $field entity field name
* @param bool $allFields search all fields (if upload field and file data field are separate)
* @return null|\Iphp\FileStoreBundle\Mapping\PropertyMapping The property mapping.
*/
public function getMappingFromField($obj, \ReflectionClass $class, $field, $allFields = true)
{
if (!$this->hasAnnotations($class)) return null;
$annotation = $this->driver->readUploadableField($class, $field);
if (!$annotation && $allFields) {
$propertyAnnotations= $this->driver->readUploadableFields($class);
foreach ($propertyAnnotations as $propertyAnnotation)
{
if ($propertyAnnotation->getFileDataPropertyName() == $field ||
$propertyAnnotation->getFileUploadPropertyName() == $field)
{
$annotation = $propertyAnnotation;
break;
}
}
}
if (null === $annotation) return null;
return $this->createMapping($obj, $class, $annotation);
}
public function hasAnnotations(\ReflectionClass $class)
{
return null !== $this->driver->readUploadable($class);
}
/**
* Creates the property mapping from the read annotation and configured mapping.
*
* @param object $obj The object.
* @param \ReflectionClass $class
* @param \Iphp\FileStoreBundle\Mapping\Annotation\UploadableField $field The read annotation.
* @return \Iphp\FileStoreBundle\Mapping\PropertyMapping The property mapping.
* @throws \InvalidArgumentException
*/
protected function createMapping($obj, \ReflectionClass $class, UploadableField $field)
{
if (!array_key_exists($field->getMapping(), $this->mappingsConfig)) {
throw new \InvalidArgumentException(sprintf(
'No mapping named "%s" configured.', $field->getMapping()
));
}
$config = $this->mappingsConfig[$field->getMapping()];
$mapping = new PropertyMapping($obj, $config, $this->namerServiceInvoker);
$mapping->setFileUploadProperty($class->getProperty($field->getFileUploadPropertyName()));
$mapping->setFileDataProperty($class->getProperty($field->getFileDataPropertyName()));
$mapping->setMappingName($field->getMapping());
return $mapping;
}
}