-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Igor Nikolaev
committed
Dec 9, 2019
1 parent
5ed934f
commit 7848432
Showing
5 changed files
with
184 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |