Skip to content

Commit c778d32

Browse files
author
Andrey Helldar
committed
Fixed error saving values by type
1 parent 8fb83be commit c778d32

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/Concerns/HasCastable.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Helldar\PrettyArray\Concerns;
4+
5+
use function addslashes;
6+
use function is_bool;
7+
use function is_numeric;
8+
9+
trait HasCastable
10+
{
11+
/**
12+
* Castable value.
13+
*
14+
* @param mixed $value
15+
*
16+
* @return mixed
17+
*/
18+
protected function castValue($value)
19+
{
20+
if (is_numeric($value)) {
21+
return $value;
22+
}
23+
24+
if (is_bool($value)) {
25+
return $value ? 'true' : 'false';
26+
}
27+
28+
return "'" . addslashes($value) . "'";
29+
}
30+
}

src/Services/Formatter.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Helldar\PrettyArray\Services;
44

55
use Helldar\PrettyArray\Concerns\HasCases;
6+
use Helldar\PrettyArray\Concerns\HasCastable;
67
use Helldar\PrettyArray\Contracts\Caseable;
78
use Helldar\Support\Facades\Arr;
89

910
final class Formatter implements Caseable
1011
{
11-
use HasCases;
12+
use HasCases, HasCastable;
1213

1314
protected $key_as_string = false;
1415

@@ -68,11 +69,7 @@ protected function value($value, int $pad = 1)
6869
return $this->raw($value, $pad);
6970
}
7071

71-
if (is_numeric($value)) {
72-
return $value;
73-
}
74-
75-
return "'" . addslashes($value) . "'";
72+
return $this->castValue($value);
7673
}
7774

7875
protected function key($key, int $size = 0)

0 commit comments

Comments
 (0)