@@ -22,10 +22,10 @@ class Editor {
22
22
const TYPE_RAW = 0 ; // simple and headless match and edit,(insert, modify, delete)
23
23
const TYPE_VARIABLE = 1 ; // a variable of array.
24
24
const TYPE_ARRAY_IN_VARIABLE_VALUE = 2 ; // a value array in a variable
25
- // eg. $variable_lv1 = [ 'key1_lv1' => [ 'targetKey' => 'targetValueToEdit', 'key2_lv2' => '...val_lv2' ], 'key2_lv1' => '....val2_lv2' ] ;
25
+ // eg. $variable_lv1 = [ 'key1_lv1' => [ 'targetKey' => 'targetValueToEdit', 'key2_lv2' => '...val_lv2' ], 'key2_lv1' => '....val2_lv2' ] ;
26
26
const TYPE_KV_PAIR = 3 ; // simple kv pair
27
27
const TYPE_ARRAY_IN_KV_PAIR = 4 ; // multi_level_kv_pair
28
- // eg. 'some_key' => [ 'key1_lv1' => [ 'targetKey' => 'targetValueToEdit', 'key2_lv2' => '...val_lv2' ], 'key2_lv1' => '....val2_lv2' ] ;
28
+ // eg. 'some_key' => [ 'key1_lv1' => [ 'targetKey' => 'targetValueToEdit', 'key2_lv2' => '...val_lv2' ], 'key2_lv1' => '....val2_lv2' ] ;
29
29
const TYPE_EOB_COMMA = 5 ; // ], end of bloc
30
30
const TYPE_EOB_SEMI_COLON = 6 ; // ]; end of bloc
31
31
@@ -193,36 +193,40 @@ public function insert($data, $insertType = self::INSERT_TYPE_RAW ){
193
193
switch ($ insertType ){
194
194
case self ::INSERT_TYPE_RAW :
195
195
case self ::INSERT_TYPE_BEFORE :
196
- $ arr = [];
197
- foreach ($ this ->_editArea as $ idx => $ line ) {
198
- if ($ idx != $ this ->_targetLineNumber ){
199
- $ arr [] = $ line ;
200
- } else {
201
- $ arr [] = $ data ;
202
- $ arr [] = $ line ;
203
- }
196
+ $ arr = [];
197
+ foreach ($ this ->_editArea as $ idx => $ line ) {
198
+ if ($ idx != $ this ->_targetLineNumber ){
199
+ $ arr [] = $ line ;
200
+ } else {
201
+ $ arr [] = $ data ;
202
+ $ arr [] = $ line ;
204
203
}
205
- $ this ->_editArea = $ arr ;
204
+ }
205
+ $ this ->_editArea = $ arr ;
206
206
break ;
207
207
case self ::INSERT_TYPE_AFTER :
208
- $ arr = [];
209
- foreach ($ this ->_editArea as $ idx => $ line ) {
210
- if ($ idx != $ this ->_targetLineNumber ){
211
- $ arr [] = $ line ;
212
- } else {
213
- $ arr [] = $ line ;
214
- $ arr [] = $ data ;
215
- }
208
+ $ arr = [];
209
+ foreach ($ this ->_editArea as $ idx => $ line ) {
210
+ if ($ idx != $ this ->_targetLineNumber ){
211
+ $ arr [] = $ line ;
212
+ } else {
213
+ $ arr [] = $ line ;
214
+ $ arr [] = $ data ;
216
215
}
217
- $ this ->_editArea = $ arr ;
216
+ }
217
+ $ this ->_editArea = $ arr ;
218
218
break ;
219
219
case self ::INSERT_TYPE_ARRAY :
220
220
$ items = $ this ->getTargetLines ();
221
- echo '' .__FILE__ .'-> ' .__method__.'() line: ' .__line__.' $items = ' .print_r ($ items , true );
221
+ // echo ''.__FILE__.'->'.__method__.'() line:'.__line__.' $items = '.print_r($items, true);
222
222
// delete "EOL" and ",", then add ',EOL' for every line -> then add the new line
223
223
foreach ($ items as $ key => $ val ){
224
224
if (trim ($ val ) == '' ) continue ;
225
- $ items [$ key ] = ' ' .trim (trim ($ val ), ', ' ).', ' .PHP_EOL ;
225
+ if (self ::endsWith (trim ($ val ), ', ' )){
226
+ $ items [$ key ] = ' ' .trim (trim ($ val ), ', ' ).', ' .PHP_EOL ; //
227
+ } else {
228
+ $ items [$ key ] = ' ' .trim (trim ($ val ), ', ' ).PHP_EOL ;
229
+ }
226
230
}
227
231
$ items [] = ' ' .$ data ;
228
232
$ this ->_editArea = array_merge ([$ this ->_editArea [0 ]] , $ items , array_slice ($ this ->_editArea , -1 ) );
0 commit comments