1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-06 17:41:01 +00:00
wallabag/src/Form/DataTransformer/StringToListTransformer.php

53 lines
1.1 KiB
PHP
Raw Normal View History

2015-10-11 17:30:58 +02:00
<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Form\DataTransformer;
2015-10-11 17:30:58 +02:00
use Symfony\Component\Form\DataTransformerInterface;
/**
* Transforms a comma-separated list to a proper PHP array.
2015-12-08 09:20:03 +01:00
* Example: the string "foo, bar" will become the array ["foo", "bar"].
*/
2015-10-11 17:30:58 +02:00
class StringToListTransformer implements DataTransformerInterface
{
/**
2016-08-17 14:18:28 +02:00
* @param string $separator The separator used in the list
*/
public function __construct(
private $separator = ',',
) {
2015-10-11 17:30:58 +02:00
}
/**
* Transforms a list to a string.
*
* @param array|null $list
*
* @return string
*/
public function transform($list)
{
if (null === $list) {
return '';
}
return implode($this->separator, $list);
}
/**
* Transforms a string to a list.
*
2015-12-08 09:20:03 +01:00
* @param string $string
2015-10-11 17:30:58 +02:00
*
* @return array|null
*/
public function reverseTransform($string)
{
2017-10-09 16:47:15 +02:00
if (null === $string) {
2023-08-08 02:27:21 +01:00
return null;
2015-10-11 17:30:58 +02:00
}
return array_values(array_filter(array_map('trim', explode($this->separator, $string))));
2015-10-11 17:30:58 +02:00
}
}