AbstractTransformer   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 18
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B recursiveUnset() 0 14 5
B recursiveSetValues() 0 14 6
B recursiveFlattenOneElementObjectsToScalarType() 0 14 6
A unserialize() 0 4 1
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/17/15
6
 * Time: 11:40 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Serializer\Transformer;
12
13
use InvalidArgumentException;
14
use NilPortugues\Serializer\Serializer;
15
use NilPortugues\Serializer\Strategy\StrategyInterface;
16
17
abstract class AbstractTransformer implements StrategyInterface
18
{
19
    /**
20
     * @param array $array
21
     * @param array $unwantedKey
22
     */
23
    protected function recursiveUnset(array &$array, array $unwantedKey)
24
    {
25
        foreach ($unwantedKey as $key) {
26
            if (\array_key_exists($key, $array)) {
27
                unset($array[$key]);
28
            }
29
        }
30
31
        foreach ($array as &$value) {
32
            if (\is_array($value)) {
33
                $this->recursiveUnset($value, $unwantedKey);
34
            }
35
        }
36
    }
37
38
    /**
39
     * @param array $array
40
     */
41
    protected function recursiveSetValues(array &$array)
42
    {
43
        if (\array_key_exists(Serializer::SCALAR_VALUE, $array)) {
44
            $array = $array[Serializer::SCALAR_VALUE];
45
        }
46
47
        if (\is_array($array) && !array_key_exists(Serializer::SCALAR_VALUE, $array)) {
48
            foreach ($array as &$value) {
49
                if (\is_array($value)) {
50
                    $this->recursiveSetValues($value);
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param array $array
58
     */
59
    protected function recursiveFlattenOneElementObjectsToScalarType(array &$array)
60
    {
61
        if (1 === \count($array) && \is_scalar(\end($array))) {
62
            $array = \array_pop($array);
63
        }
64
65
        if (\is_array($array)) {
66
            foreach ($array as &$value) {
67
                if (\is_array($value)) {
68
                    $this->recursiveFlattenOneElementObjectsToScalarType($value);
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param $value
76
     *
77
     * @throws \InvalidArgumentException
78
     *
79
     * @return array
80
     */
81
    public function unserialize($value)
82
    {
83
        throw new InvalidArgumentException(\sprintf('%s does not perform unserializations.', __CLASS__));
84
    }
85
}
86