https://github.com/dantleech/symfony-form-array-to-delimited-string-transformer
Symfony Form Data Transformer which transforms arrays to delimited strings and vice-versa
https://github.com/dantleech/symfony-form-array-to-delimited-string-transformer
Last synced: about 1 year ago
JSON representation
Symfony Form Data Transformer which transforms arrays to delimited strings and vice-versa
- Host: GitHub
- URL: https://github.com/dantleech/symfony-form-array-to-delimited-string-transformer
- Owner: dantleech
- Created: 2014-07-10T09:08:00.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2020-08-10T18:10:36.000Z (almost 6 years ago)
- Last Synced: 2025-04-14T01:12:58.568Z (about 1 year ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Array to delimited string data transformer
==========================================
[](https://travis-ci.org/dantleech/symfony-form-array-to-delimited-string-transformer)
This is a data transformer for the Symfony form framework.
It is meant for transforming text fields containing delimited strings to
arrays.
Usage
-----
You can use the data transformer as follows:
````php
$form->add(
$builder->create('tags', 'text')->addModelTransformer(
new ArrayToDelimitedStringTransformer()
)
);
````
This will transform the ``tags`` text field as follows:
````php
// Tranform
array('one', 'two', 'three', 'four') === 'one, two, three, four'
// Reverse transform
' one , two , three, four,' === array('one', 'two', 'three', 'four')
````
Changing the delimiter
----------------------
You can change the delimiting string with the first constructor argument:
````php
new ArrayToDelimitedStringTransformer(';')
````
Will result in:
````php
// Transform
array('one', 'two', 'three', 'four') === 'one; two; three; four'
// Reverse Transform
'one ; two;three ; four' => array('one', 'two', 'three')
````
Output padding
--------------
Additionally you can change the way in which the output is formatted with
by setting the amount of whitespace (padding) before and after the text
elements produced by a transformation:
````php
new ArrayToDelimitedStringTransformer('%', 1, 1)
````
Will result in:
````php
// Transform
array('one', 'two', 'three', 'four') === 'one % two % three % four'
// Reverse Transform
'one % two%three % four' => array('one', 'two', 'three')
````