| @@ 8-29 (lines=22) @@ | ||
| 5 | use TreeHouse\Feeder\Exception\TransformationFailedException; |
|
| 6 | use TreeHouse\Feeder\Modifier\Data\Transformer\TransformerInterface; |
|
| 7 | ||
| 8 | class HtmlEntitiesTransformer implements TransformerInterface |
|
| 9 | { |
|
| 10 | public function transform($value) |
|
| 11 | { |
|
| 12 | if (is_null($value)) { |
|
| 13 | return $value; |
|
| 14 | } |
|
| 15 | ||
| 16 | if (is_scalar($value)) { |
|
| 17 | $value = (string) $value; |
|
| 18 | } |
|
| 19 | ||
| 20 | if (!is_string($value)) { |
|
| 21 | throw new TransformationFailedException( |
|
| 22 | sprintf('Expected a string to transform, got %s instead', json_encode($value)) |
|
| 23 | ); |
|
| 24 | } |
|
| 25 | ||
| 26 | // strip tags just to be sure |
|
| 27 | return html_entity_decode($value); |
|
| 28 | } |
|
| 29 | } |
|
| 30 | ||
| @@ 8-29 (lines=22) @@ | ||
| 5 | use TreeHouse\Feeder\Exception\TransformationFailedException; |
|
| 6 | use TreeHouse\Feeder\Modifier\Data\Transformer\TransformerInterface; |
|
| 7 | ||
| 8 | class MultiSpaceToSingleSpaceTransformer implements TransformerInterface |
|
| 9 | { |
|
| 10 | public function transform($value) |
|
| 11 | { |
|
| 12 | if (is_null($value)) { |
|
| 13 | return $value; |
|
| 14 | } |
|
| 15 | ||
| 16 | if (is_scalar($value)) { |
|
| 17 | $value = (string) $value; |
|
| 18 | } |
|
| 19 | ||
| 20 | if (!is_string($value)) { |
|
| 21 | throw new TransformationFailedException( |
|
| 22 | sprintf('Expected a string to transform, got %s instead', json_encode($value)) |
|
| 23 | ); |
|
| 24 | } |
|
| 25 | ||
| 26 | // replace double spaces with single space |
|
| 27 | return preg_replace('/ {2,}/', ' ', $value); |
|
| 28 | } |
|
| 29 | } |
|
| 30 | ||
| @@ 11-32 (lines=22) @@ | ||
| 8 | /** |
|
| 9 | * Normalizes an input to be a guaranteed string. |
|
| 10 | */ |
|
| 11 | class NormalizedStringTransformer implements TransformerInterface |
|
| 12 | { |
|
| 13 | public function transform($value) |
|
| 14 | { |
|
| 15 | if (is_null($value)) { |
|
| 16 | return $value; |
|
| 17 | } |
|
| 18 | ||
| 19 | if (is_scalar($value)) { |
|
| 20 | $value = (string) $value; |
|
| 21 | } |
|
| 22 | ||
| 23 | if (!is_string($value)) { |
|
| 24 | throw new TransformationFailedException( |
|
| 25 | sprintf('Expected a string to transform, got %s instead', json_encode($value)) |
|
| 26 | ); |
|
| 27 | } |
|
| 28 | ||
| 29 | // strip tags just to be sure |
|
| 30 | return trim(strip_tags($value)); |
|
| 31 | } |
|
| 32 | } |
|
| 33 | ||