Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
8 / 8
ArrayTransformer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
8 / 8
 transform
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 reverseTransform
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
5 / 5
1<?php
2
3// {{{ License
4// This file is part of GNU social - https://www.gnu.org/software/social
5//
6// GNU social is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Affero General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU social is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU Affero General Public License for more details.
15//
16// You should have received a copy of the GNU Affero General Public License
17// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
18// }}}
19
20/**
21 * Handle network public feed
22 *
23 * @package  GNUsocial
24 * @category Form
25 *
26 * @author    Hugo Sales <hugo@hsal.es>
27 * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
28 * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
29 */
30
31namespace App\Util\Form;
32
33use App\Util\Formatting;
34use Symfony\Component\Form\DataTransformerInterface;
35use Symfony\Component\Form\Exception\TransformationFailedException;
36
37class ArrayTransformer implements DataTransformerInterface
38{
39    // Can't use type annotations, to conform to interface
40
41    /**
42     * @param array $a
43     *
44     * @return string
45     */
46    public function transform($a)
47    {
48        if (!is_array($a)) {
49            throw new TransformationFailedException();
50        }
51        return Formatting::toString($a, Formatting::SPLIT_BY_SPACE);
52    }
53
54    /**
55     * @param string $s
56     *
57     * @return array
58     */
59    public function reverseTransform($s)
60    {
61        if (empty($s)) {
62            return [];
63        }
64
65        if (is_string($s) && Formatting::toArray($s, $arr, Formatting::SPLIT_BY_BOTH)) {
66            return $arr;
67        } else {
68            throw new TransformationFailedException();
69        }
70    }
71}