Skip to content

Commit 0357c2c

Browse files
author
jetwaves@office
committed
fix : when perform insertion, it systematically add a , after every line before the target area.
1 parent 1805d2e commit 0357c2c

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/EditArrayInFileEditor.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class Editor {
2222
const TYPE_RAW = 0; // simple and headless match and edit,(insert, modify, delete)
2323
const TYPE_VARIABLE = 1; // a variable of array.
2424
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' ] ;
2626
const TYPE_KV_PAIR = 3; // simple kv pair
2727
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' ] ;
2929
const TYPE_EOB_COMMA = 5; // ], end of bloc
3030
const TYPE_EOB_SEMI_COLON = 6; // ]; end of bloc
3131

@@ -193,36 +193,40 @@ public function insert($data, $insertType = self::INSERT_TYPE_RAW ){
193193
switch ($insertType){
194194
case self::INSERT_TYPE_RAW:
195195
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;
204203
}
205-
$this->_editArea = $arr;
204+
}
205+
$this->_editArea = $arr;
206206
break;
207207
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;
216215
}
217-
$this->_editArea = $arr;
216+
}
217+
$this->_editArea = $arr;
218218
break;
219219
case self::INSERT_TYPE_ARRAY :
220220
$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);
222222
// delete "EOL" and ",", then add ',EOL' for every line -> then add the new line
223223
foreach($items as $key => $val){
224224
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+
}
226230
}
227231
$items[] = ' '.$data;
228232
$this->_editArea = array_merge([$this->_editArea[0]] , $items, array_slice($this->_editArea, -1) );

0 commit comments

Comments
 (0)