Completed
Push — master ( 66f480...24a417 )
by Tomasz
02:30 queued 17s
created

AbstractFormat::doSerialize()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
rs 8.5126
cc 5
eloc 13
nc 5
nop 2
1
<?php
2
namespace Thunder\Serializard\Format;
3
4
use Thunder\Serializard\HandlerContainer\HandlerContainerInterface as Handlers;
5
6
/**
7
 * @author Tomasz Kowalczyk <[email protected]>
8
 */
9
abstract class AbstractFormat implements FormatInterface
10
{
11
    protected function doSerialize($var, Handlers $handlers)
12
    {
13
        if(is_object($var)) {
14
            $class = get_class($var);
15
            $handler = $handlers->getHandler($class);
16
17
            if(null === $handler) {
18
                throw new \RuntimeException(sprintf('No serialization handler for class %s!', $class));
19
            }
20
21
            return $this->doSerialize(call_user_func_array($handler, array($var)), $handlers);
22
        }
23
24
        if(is_array($var)) {
25
            $return = array();
26
            foreach($var as $key => $value) {
27
                $return[$key] = $this->doSerialize($value, $handlers);
28
            }
29
30
            return $return;
31
        }
32
33
        return $var;
34
    }
35
}
36