22
33namespace Hboie \DataIOBundle \Mapper ;
44
5- use Doctrine \Persistence \ ObjectManager ;
5+ use Doctrine \ORM \ EntityManagerInterface ;
66use Symfony \Component \PropertyAccess \PropertyAccess ;
77
88class EntityMapper
99{
1010 /**
11- * @var ObjectManager
11+ * @var EntityManagerInterface
1212 */
13- private $ entity_manager ;
13+ private $ entityManager ;
1414
1515 /**
1616 * @var PropertyAccess $accessor
@@ -20,29 +20,35 @@ class EntityMapper
2020 /**
2121 * @var array
2222 */
23- private $ col_map ;
23+ private $ colMap ;
2424
2525 /**
2626 * @var array
2727 */
28- private $ default_values ;
28+ private $ defaultValues ;
2929
3030 /**
3131 * @var array
3232 */
33- private $ mandatory_fields ;
33+ private $ factors ;
34+
35+ /**
36+ * @var array
37+ */
38+ private $ mandatoryFields ;
3439
3540 /**
3641 * @var mixed
3742 */
3843 private $ object ;
3944
40- public function __construct (ObjectManager $ entity_manager )
45+ public function __construct (EntityManagerInterface $ entityManager )
4146 {
42- $ this ->entity_manager = $ entity_manager ;
43- $ this ->col_map = array ();
44- $ this ->default_values = array ();
45- $ this ->mandatory_fields = array ();
47+ $ this ->entityManager = $ entityManager ;
48+ $ this ->colMap = array ();
49+ $ this ->defaultValues = array ();
50+ $ this ->mandatoryFields = array ();
51+ $ this ->factors = array ();
4652
4753 $ this ->accessor = PropertyAccess::createPropertyAccessor ();
4854 }
@@ -54,30 +60,40 @@ public function loadXmlMapping($filename)
5460 {
5561 $ xml = new \SimpleXmlElement (file_get_contents ($ filename ));
5662 foreach ($ xml ->field as $ field ) {
57- $ field_attrib = $ field ->attributes ();
58- if (isset ($ field_attrib ['name ' ]))
63+ $ fieldAttrib = $ field ->attributes ();
64+ if (isset ($ fieldAttrib ['name ' ]))
5965 {
60- $ field_name = (string )$ field_attrib ['name ' ];
66+ $ fieldName = (string )$ fieldAttrib ['name ' ];
6167
62- if (isset ($ field_attrib ['mandatory ' ])) {
63- if ($ field_attrib ['mandatory ' ] == 'true ' ) {
64- array_push ($ this ->mandatory_fields , $ field_name );
68+ if (isset ($ fieldAttrib ['mandatory ' ])) {
69+ if ($ fieldAttrib ['mandatory ' ] == 'true ' ) {
70+ array_push ($ this ->mandatoryFields , $ fieldName );
6571 }
6672 }
6773
6874 foreach ($ field ->children () as $ child ) {
6975 /** @var \SimpleXMLElement $child */
70- if ($ child ->getName () == 'column ' ) {
71- $ column_attrib = $ child ->attributes ();
72- if (isset ($ column_attrib ['name ' ])) {
73- $ column_name = (string )$ column_attrib ['name ' ];
74- $ this ->col_map [strtolower ($ column_name )] = $ field_name ;
76+ $ childName = $ child ->getName ();
77+
78+ if ($ childName == 'column ' ) {
79+ $ columnAttrib = $ child ->attributes ();
80+ if (isset ($ columnAttrib ['name ' ])) {
81+ $ columnName = (string )$ columnAttrib ['name ' ];
82+ $ this ->colMap [strtolower ($ columnName )] = $ fieldName ;
7583 }
76- } else if ($ child ->getName () == 'default ' ) {
77- $ column_attrib = $ child ->attributes ();
78- if (isset ($ column_attrib ['value ' ])) {
79- $ default_value = (string )$ column_attrib ['value ' ];
80- $ this ->default_values [$ field_name ] = $ default_value ;
84+ } else if ($ childName == 'default ' ) {
85+ $ columnAttrib = $ child ->attributes ();
86+ if (isset ($ columnAttrib ['value ' ])) {
87+ $ defaultValue = (string )$ columnAttrib ['value ' ];
88+ $ this ->defaultValues [$ fieldName ] = $ defaultValue ;
89+ }
90+ } else if ($ childName == 'factor ' ) {
91+ $ columnAttrib = $ child ->attributes ();
92+ if (isset ($ columnAttrib ['value ' ])) {
93+ $ factorValue = (string )$ columnAttrib ['value ' ];
94+ try {
95+ $ this ->factors [$ fieldName ] = floatval ($ factorValue );
96+ } catch (\Exception $ e ) {}
8197 }
8298 }
8399 }
@@ -90,18 +106,24 @@ public function loadXmlMapping($filename)
90106 * @param string $value
91107 * @return bool
92108 */
93- public function insertValue ($ column_name , $ value )
109+ public function insertValue ($ columnName , $ value )
94110 {
95- $ key = strtolower ($ column_name );
111+ $ key = strtolower ($ columnName );
96112
97- if (!isset ($ this ->col_map [$ key ])) {
113+ if (!isset ($ this ->colMap [$ key ])) {
98114 return false ;
99115 }
100116
101- $ var_name = $ this ->col_map [$ key ];
117+ $ varName = $ this ->colMap [$ key ];
118+
119+ if ( isset ($ this ->factors [$ varName ]) ) {
120+ try {
121+ $ value = floatval ($ value ) * $ this ->factors [$ varName ];
122+ } catch (\Exception $ e ) {}
123+ }
102124
103125 try {
104- $ this ->accessor ->setValue ($ this ->object , $ var_name , $ value );
126+ $ this ->accessor ->setValue ($ this ->object , $ varName , $ value );
105127 } catch (\Exception $ e ) {
106128 return false ;
107129 }
@@ -116,14 +138,14 @@ public function insertValue($column_name, $value)
116138
117139 public function insertValues ($ row )
118140 {
119- $ value_inserted = false ;
120- foreach ($ row as $ col_name => $ value ) {
121- if ($ this ->insertValue ($ col_name , $ value )) {
122- $ value_inserted = true ;
141+ $ valueInserted = false ;
142+ foreach ($ row as $ colName => $ value ) {
143+ if ($ this ->insertValue ($ colName , $ value )) {
144+ $ valueInserted = true ;
123145 }
124146 }
125147
126- return $ value_inserted ;
148+ return $ valueInserted ;
127149 }
128150
129151 /**
@@ -132,31 +154,31 @@ public function insertValues($row)
132154
133155 public function insertDefaultValues ()
134156 {
135- $ values_inserted = true ;
136- foreach ( $ this ->default_values as $ var_name => $ value ) {
157+ $ valuesInserted = true ;
158+ foreach ( $ this ->defaultValues as $ varName => $ value ) {
137159 try {
138- $ this ->accessor ->setValue ($ this ->object , $ var_name , $ value );
160+ $ this ->accessor ->setValue ($ this ->object , $ varName , $ value );
139161 } catch (\Exception $ e ) {
140- $ values_inserted = false ;
162+ $ valuesInserted = false ;
141163 }
142164 }
143165
144- return $ values_inserted ;
166+ return $ valuesInserted ;
145167 }
146168
147169 public function flush ()
148170 {
149- $ this ->entity_manager ->persist ($ this ->object );
150- $ this ->entity_manager ->flush ();
171+ $ this ->entityManager ->persist ($ this ->object );
172+ $ this ->entityManager ->flush ();
151173 }
152174
153175 /**
154176 * @param array $fields_array
155177 * @return bool
156178 */
157- public function containsMandatoryFields ($ fields_array )
179+ public function containsMandatoryFields ($ fieldsArray )
158180 {
159- $ diff = array_diff ($ this ->mandatory_fields , $ fields_array );
181+ $ diff = array_diff ($ this ->mandatoryFields , $ fieldsArray );
160182
161183 if (count ($ diff ) == 0 ) {
162184 return true ;
@@ -178,7 +200,7 @@ public function setObject($object)
178200 */
179201 public function getMandatoryFields ()
180202 {
181- return $ this ->mandatory_fields ;
203+ return $ this ->mandatoryFields ;
182204 }
183205
184206 /**
@@ -187,9 +209,9 @@ public function getMandatoryFields()
187209 public function getMandatoryColumns ()
188210 {
189211 $ ret = array ();
190- foreach ($ this ->col_map as $ col_name => $ field_name ) {
191- if (in_array ($ field_name , $ this ->mandatory_fields )) {
192- $ ret [] = $ col_name ;
212+ foreach ($ this ->colMap as $ colName => $ fieldName ) {
213+ if (in_array ($ fieldName , $ this ->mandatoryFields )) {
214+ $ ret [] = $ colName ;
193215 }
194216 }
195217
@@ -200,9 +222,9 @@ public function getMandatoryColumns()
200222 * @param array $fields_array
201223 * @return bool
202224 */
203- public function containsMandatoryColumns ($ columns_array )
225+ public function containsMandatoryColumns ($ columnsArray )
204226 {
205- $ diff = array_diff ($ this ->getMandatoryColumns (), $ columns_array );
227+ $ diff = array_diff ($ this ->getMandatoryColumns (), $ columnsArray );
206228
207229 if (count ($ diff ) == 0 ) {
208230 return true ;
@@ -216,14 +238,14 @@ public function containsMandatoryColumns($columns_array)
216238 */
217239 public function getMap ()
218240 {
219- return $ this ->col_map ;
241+ return $ this ->colMap ;
220242 }
221243
222244 /**
223245 * @return array
224246 */
225247 public function getDefaultValues ()
226248 {
227- return $ this ->default_values ;
249+ return $ this ->defaultValues ;
228250 }
229251}
0 commit comments