Skip to content

Commit

Permalink
Fixed error saving values by type
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Jan 28, 2020
1 parent 8fb83be commit c778d32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/Concerns/HasCastable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Helldar\PrettyArray\Concerns;

use function addslashes;
use function is_bool;
use function is_numeric;

trait HasCastable
{
/**
* Castable value.
*
* @param mixed $value
*
* @return mixed
*/
protected function castValue($value)
{
if (is_numeric($value)) {
return $value;
}

if (is_bool($value)) {
return $value ? 'true' : 'false';
}

return "'" . addslashes($value) . "'";
}
}
9 changes: 3 additions & 6 deletions src/Services/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Helldar\PrettyArray\Services;

use Helldar\PrettyArray\Concerns\HasCases;
use Helldar\PrettyArray\Concerns\HasCastable;
use Helldar\PrettyArray\Contracts\Caseable;
use Helldar\Support\Facades\Arr;

final class Formatter implements Caseable
{
use HasCases;
use HasCases, HasCastable;

protected $key_as_string = false;

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

if (is_numeric($value)) {
return $value;
}

return "'" . addslashes($value) . "'";
return $this->castValue($value);
}

protected function key($key, int $size = 0)
Expand Down

0 comments on commit c778d32

Please sign in to comment.