Skip to content

Commit

Permalink
Add generic stringifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nikolaev committed Dec 9, 2019
1 parent 5ed934f commit 7848432
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 49 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@
7.2.1: Use "object" type hint.

7.2.2: Rename stringifier interface to Doctrine stringifier.

7.2.3: Add generic stringifier.
54 changes: 5 additions & 49 deletions Strings/Stringifier/DoctrineStringifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
class DoctrineStringifier implements DoctrineStringifierInterface
{
use StringifierTrait;

private const DATETIME_FORMATS = [
Types::DATE_MUTABLE => 'd.m.Y',
Types::DATETIME_MUTABLE => 'd.m.Y H:i:s',
Expand Down Expand Up @@ -46,9 +48,6 @@ public function stringify($value, string $dataType): string
if (null === $value) {
return '';
}
if (is_string($value)) {
return $value;
}
if (is_object($value) && method_exists($value, '__toString')) {
return (string)$value;
}
Expand Down Expand Up @@ -88,53 +87,10 @@ public function stringify($value, string $dataType): string
}

/**
* @param array $value Value to stringify
*
* @return string
*/
private function stringifyArray(array $value): string
{
return json_encode($value);
}

/**
* @param bool $value Value to stringify
*
* @return string
*/
private function stringifyBoolean(bool $value): string
{
return $this->translator->trans(sprintf('boolean.%s', $value ? 'yes' : 'no'));
}

/**
* @param \DateTime $value Value to stringify
* @param string $format Datetime format
*
* @return string
*/
private function stringifyDatetime(\DateTime $value, string $format): string
{
return $value->format($format);
}

/**
* @param object $value Value to stringify
*
* @return string
*/
private function stringifyObject(object $value): string
{
return serialize($value);
}

/**
* @param mixed $value Value to stringify
*
* @return string
* {@inheritDoc}
*/
private function stringifyScalar($value): string
protected function getTranslator(): TranslatorInterface
{
return (string)$value;
return $this->translator;
}
}
71 changes: 71 additions & 0 deletions Strings/Stringifier/Stringifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);
/**
* @author Igor Nikolaev <[email protected]>
* @copyright Copyright (c) 2019, Darvin Studio
* @link https://www.darvin-studio.ru
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Darvin\Utils\Strings\Stringifier;

use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Stringifier
*/
class Stringifier implements StringifierInterface
{
use StringifierTrait;

/**
* @var \Symfony\Contracts\Translation\TranslatorInterface
*/
private $translator;

/**
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator Translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

/**
* {@inheritDoc}
*/
public function stringify($value): string
{
$type = gettype($value);

switch ($type) {
case 'boolean':
return $this->stringifyBoolean($value);

case 'integer':
case 'double':
return $this->stringifyScalar($value);

case 'string':
return $value;

case 'array':
return $this->stringifyArray($value);

case 'object':
return $this->stringifyObject($value);

default:
return '';
}
}

/**
* {@inheritDoc}
*/
protected function getTranslator(): TranslatorInterface
{
return $this->translator;
}
}
24 changes: 24 additions & 0 deletions Strings/Stringifier/StringifierInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/**
* @author Igor Nikolaev <[email protected]>
* @copyright Copyright (c) 2019, Darvin Studio
* @link https://www.darvin-studio.ru
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Darvin\Utils\Strings\Stringifier;

/**
* Stringifier
*/
interface StringifierInterface
{
/**
* @param mixed $value Value to stringify
*
* @return string
*/
public function stringify($value): string;
}
82 changes: 82 additions & 0 deletions Strings/Stringifier/StringifierTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php declare(strict_types=1);
/**
* @author Igor Nikolaev <[email protected]>
* @copyright Copyright (c) 2019, Darvin Studio
* @link https://www.darvin-studio.ru
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Darvin\Utils\Strings\Stringifier;

use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Stringifier trait
*/
trait StringifierTrait
{
/**
* @return \Symfony\Contracts\Translation\TranslatorInterface
*/
abstract protected function getTranslator(): TranslatorInterface;

/**
* @param array $value Value to stringify
*
* @return string
*/
private function stringifyArray(array $value): string
{
return json_encode($value);
}

/**
* @param bool $value Value to stringify
*
* @return string
*/
private function stringifyBoolean(bool $value): string
{
return $this->getTranslator()->trans(sprintf('boolean.%s', $value ? 'yes' : 'no'));
}

/**
* @param object $value Value to stringify
*
* @return string
*/
private function stringifyObject(object $value): string
{
if ($value instanceof \DateTime) {
return $this->stringifyDatetime($value);
}
if (method_exists($value, '__toString')) {
return (string)$value;
}

return serialize($value);
}

/**
* @param \DateTime $value Value to stringify
* @param string $format Datetime format
*
* @return string
*/
private function stringifyDatetime(\DateTime $value, string $format = 'd.m.Y H:i:s'): string
{
return $value->format($format);
}

/**
* @param mixed $value Value to stringify
*
* @return string
*/
private function stringifyScalar($value): string
{
return (string)$value;
}
}

0 comments on commit 7848432

Please sign in to comment.