Skip to content

Commit b53e318

Browse files
authored
Merge pull request #35 from WebFiori/dev
Added a Condition for Numbers in Case Converter
2 parents 28c9e89 + 36d0076 commit b53e318

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

tests/webfiori/tests/json/JsonTest.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,10 @@ public function testToJsonString10() {
356356
. ' "obj":{'."\r\n"
357357
. ' },'."\r\n"
358358
. ' "general":{'."\r\n"
359-
. ' "property00":"1",'."\r\n"
360-
. ' "property01":"3",'."\r\n"
361-
. ' "property02":99,'."\r\n"
362-
. ' "property04":"ok"'."\r\n"
359+
. ' "property_00":"1",'."\r\n"
360+
. ' "property_01":"3",'."\r\n"
361+
. ' "property_02":99,'."\r\n"
362+
. ' "property_04":"ok"'."\r\n"
363363
. ' }'."\r\n"
364364
. ' },'."\r\n"
365365
. ' "o":{'."\r\n"
@@ -369,7 +369,7 @@ public function testToJsonString10() {
369369
. ' }'."\r\n"
370370
. '}',$j.'');
371371
$j->setIsFormatted(false);
372-
$this->assertEquals('{"jsonx":{"number_one":1,"arr":[],"obj":{},"general":{"property00":"1","property01":"3","property02":99,"property04":"ok"}},"o":{"property_00":"1","property_01":2,"property_02":3}}',$j.'');
372+
$this->assertEquals('{"jsonx":{"number_one":1,"arr":[],"obj":{},"general":{"property_00":"1","property_01":"3","property_02":99,"property_04":"ok"}},"o":{"property_00":"1","property_01":2,"property_02":3}}',$j.'');
373373
return $j;
374374
}
375375
/**
@@ -390,16 +390,16 @@ public function testToJsonXString10(Json $json) {
390390
. ' <json:object name="obj">'."\r\n"
391391
. ' </json:object>'."\r\n"
392392
. ' <json:object name="general">'."\r\n"
393-
. ' <json:string name="property00">'."\r\n"
393+
. ' <json:string name="property_00">'."\r\n"
394394
. ' 1'."\r\n"
395395
. ' </json:string>'."\r\n"
396-
. ' <json:string name="property01">'."\r\n"
396+
. ' <json:string name="property_01">'."\r\n"
397397
. ' 3'."\r\n"
398398
. ' </json:string>'."\r\n"
399-
. ' <json:number name="property02">'."\r\n"
399+
. ' <json:number name="property_02">'."\r\n"
400400
. ' 99'."\r\n"
401401
. ' </json:number>'."\r\n"
402-
. ' <json:string name="property04">'."\r\n"
402+
. ' <json:string name="property_04">'."\r\n"
403403
. ' ok'."\r\n"
404404
. ' </json:string>'."\r\n"
405405
. ' </json:object>'."\r\n"
@@ -445,6 +445,35 @@ public function testToJsonString11() {
445445
. '"3":{"Property00":"1","Property01":2,"Property02":3,"Property04":5},"4":{"good":true}},"1":{"bad":false}}}',$json.'');
446446
return $json;
447447
}
448+
/**
449+
* @test
450+
*/
451+
public function testToJsonString12() {
452+
$j = new Json([
453+
'123_56_oKo' => 5,
454+
'hell-55-o-p-P-x' => 'Ok'
455+
]);
456+
$this->assertEquals('{"123_56_oKo":5,"hell-55-o-p-P-x":"Ok"}', $j->toJSONString());
457+
$j->setPropsStyle('snake');
458+
$this->assertEquals('{"123_56_o_ko":5,"hell_55_o_p_p_x":"Ok"}', $j->toJSONString());
459+
$j->setPropsStyle('kebab');
460+
$this->assertEquals('{"123-56-o-ko":5,"hell-55-o-p-p-x":"Ok"}', $j->toJSONString());
461+
}
462+
/**
463+
* @test
464+
*/
465+
public function testToJsonString13() {
466+
$j = new Json([
467+
'_123_56_oKo' => 5,
468+
'hell-55-o-p-P-x' => 'Ok'
469+
]);
470+
$this->assertEquals('{"_123_56_oKo":5,"hell-55-o-p-P-x":"Ok"}', $j->toJSONString());
471+
$j->setPropsStyle('kebab');
472+
$this->assertEquals('{"-123-56-o-ko":5,"hell-55-o-p-p-x":"Ok"}', $j->toJSONString());
473+
$j->setPropsStyle('snake');
474+
$this->assertEquals('{"_123_56_o_ko":5,"hell_55_o_p_p_x":"Ok"}', $j->toJSONString());
475+
476+
}
448477
/**
449478
* @depends testToJsonString11
450479
* @param Json $json

webfiori/json/CaseConverter.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,55 @@ private static function _isUpper($char) {
122122
private static function _toSnakeOrKebab($value, $from, $to) {
123123
$attr1 = str_replace($from, $to, trim($value));
124124
$retVal = '';
125-
125+
$isNumFound = false;
126+
$snakeOrKebabFound = false;
127+
126128
for ($x = 0 ; $x < strlen($attr1) ; $x++) {
127129
$char = $attr1[$x];
128-
129-
if (self::_isUpper($char) && $x != 0) {
130-
$retVal .= $to.strtolower($char);
131-
} else if (self::_isUpper($char) && $x == 0) {
132-
$retVal .= strtolower($char);
133-
} else {
130+
131+
if ($char == $to) {
132+
$snakeOrKebabFound = true;
134133
$retVal .= $char;
134+
} else if ($char >= '0' && $char <= '9') {
135+
$retVal .= self::addNumber($x, $isNumFound, $to, $char, $snakeOrKebabFound);
136+
} else {
137+
$retVal .= self::addChar($x, $isNumFound, $to, $char, $snakeOrKebabFound);
135138
}
136139
}
137140

138141
return $retVal;
139142
}
143+
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
144+
$isUpper = self::_isUpper($char);
145+
$retVal = '';
146+
147+
if (($isUpper || $isNumFound) && $x != 0 && !$snakeOrKebabFound) {
148+
$retVal .= $to.strtolower($char);
149+
} else if ($isUpper && $x == 0) {
150+
$retVal .= strtolower($char);
151+
} else if ($isUpper && $x != 0 && $snakeOrKebabFound) {
152+
$retVal .= strtolower($char);
153+
154+
} else {
155+
$retVal .= $char;
156+
}
157+
$snakeOrKebabFound = false;
158+
$isNumFound = false;
159+
return $retVal;
160+
}
161+
162+
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
163+
$retVal = '';
164+
if ($x == 0) {
165+
$isNumFound = true;
166+
$retVal .= $char;
167+
} else if ($isNumFound || $snakeOrKebabFound) {
168+
$retVal .= $char;
169+
$snakeOrKebabFound = false;
170+
} else {
171+
$retVal .= $to.$char;
172+
}
173+
$isNumFound = true;
174+
return $retVal;
175+
}
140176
}

0 commit comments

Comments
 (0)