Skip to content

Commit f41ca0a

Browse files
committed
replace string conversion with StringEx
1 parent d1f283a commit f41ca0a

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
}
1212
],
1313
"support": {
14-
"issues": "https://github.com/modethirteen/XArray.php/issues"
14+
"issues": "https://github.com/modethirteen/XArray/issues"
1515
},
1616
"require": {
1717
"php": "^7.2.0",
18-
"ext-json": "*"
18+
"ext-json": "*",
19+
"modethirteen/type-ex": "1.0.0"
1920
},
2021
"require-dev": {
2122
"phpstan/phpstan": "~0.12.0",

src/XArray.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
namespace modethirteen\XArray;
1818

19-
use Closure;
19+
use modethirteen\TypeEx\StringEx;
2020

2121
/**
2222
* Class XArray - get/set accessors for arrays
@@ -63,33 +63,6 @@ private static function getValHelper(array $array, string $key, bool $isArrayRet
6363
return $array;
6464
}
6565

66-
/**
67-
* Get string representation of value
68-
*
69-
* @param mixed $value
70-
* @return string
71-
*/
72-
private static function getStringValue($value) : string {
73-
if($value === null) {
74-
return '';
75-
}
76-
if(is_string($value)) {
77-
return $value;
78-
}
79-
if(is_bool($value)) {
80-
return $value ? 'true' : 'false';
81-
}
82-
if(is_array($value)) {
83-
return implode(',', array_map(function($v) {
84-
return self::getStringValue($v);
85-
}, $value));
86-
}
87-
if($value instanceof Closure) {
88-
return strval($value());
89-
}
90-
return strval($value);
91-
}
92-
9366
/**
9467
* @param array $array
9568
* @param string $key
@@ -217,7 +190,7 @@ public function getVal(string $key, $default = null) {
217190
* @return string - string representation of value
218191
*/
219192
public function getString(string $key, string $default = '') : string {
220-
return self::getStringValue($this->getVal($key, $default));
193+
return StringEx::stringify($this->getVal($key, $default));
221194
}
222195

223196
/**
@@ -243,7 +216,9 @@ public function setVal(string $key, $value = null) : void {
243216
public function toXml(string $outer = null) : string {
244217
$result = '';
245218
foreach($this->array as $key => $value) {
246-
$key = self::getStringValue($key);
219+
$key = StringEx::stringify($key);
220+
221+
/** @noinspection PhpStatementHasEmptyBodyInspection */
247222
if(strncmp($key, '@', 1) === 0) {
248223

249224
// skip attributes
@@ -261,17 +236,17 @@ public function toXml(string $outer = null) : string {
261236
// attribute list found
262237
$attrs = '';
263238
foreach($value as $attrKey => $attrValue) {
264-
$attrKey = self::getStringValue($attrKey);
239+
$attrKey = StringEx::stringify($attrKey);
265240
if(strncmp($attrKey, '@', 1) === 0) {
266-
$attrValue = self::getStringValue($attrValue);
241+
$attrValue = StringEx::stringify($attrValue);
267242
$attrs .= ' ' . htmlspecialchars(substr($attrKey, 1), ENT_QUOTES) . '="' . htmlspecialchars($attrValue, ENT_QUOTES) . '"';
268243
}
269244
}
270245
$x = new XArray($value);
271246
$result .= '<' . $encodedTag . $attrs . '>' . $x->toXml() . '</' . $encodedTag . '>';
272247
unset($x);
273248
} else {
274-
$value = self::getStringValue($value);
249+
$value = StringEx::stringify($value);
275250
if($encodedTag !== '#text') {
276251
$result .= '<' . $encodedTag . '>' . htmlspecialchars($value, ENT_QUOTES) . '</' . $encodedTag . '>';
277252
} else {

0 commit comments

Comments
 (0)