Test Failed
Push — master ( fc4f20...ea5a85 )
by stéphane
13:54
created

Dumper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 46
dl 0
loc 128
ccs 9
cts 9
cp 1
rs 10
c 2
b 1
f 0
wmc 23

7 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpMultiDoc() 0 3 2
A dump() 0 10 4
A toFile() 0 3 1
A iteratorToString() 0 20 6
A toString() 0 7 3
A dumpYamlObject() 0 11 5
A __construct() 0 4 2
1
<?php
2
namespace Dallgoot\Yaml;
3
4
5
/**
6
 *  Convert PHP datatypes to a YAML string syntax
7
 *
8
 * @author  Stéphane Rebai <[email protected]>
9
 * @license Apache 2.0
10
 * @link    https://github.com/dallgoot/yaml
11
 */
12
class Dumper
13
{
14
    private const LINEFEED = "\n";
15
    public const INDENT = 2;
16
    // private const WIDTH  = 120; //TODO forget this feature for the moment
17
    private const OPTIONS = 0b00000;
18
    public const DATE_FORMAT = 'Y-m-d';
19
20
    public $options;
21
    //options
22
    public const EXPAND_SHORT = 0b00001;
23
    public const SERIALIZE_CUSTOM_OBJECTS = 0b00010;
24
    /** @var int */
25
    public $floatPrecision = 4;
26
    /** @var bool */
27
    private $multipleDocs = false;
28
    /** @var bool */
29
    public $_compactMode = false;
30
    /** @var null|DumperHandlers */
31
    private $handler;
32
33
34
    public function __construct($options=null)
35
    {
36
        $this->options = is_int($options) ? $options : self::OPTIONS;
37
        $this->handler = new DumperHandlers($this);
38 2
    }
39
    /**
40 2
     * Returns (as a string) the YAML representation of the $dataType provided
41 2
     *
42 2
     * @param mixed    $dataType The data type
43 2
     * @param int|null $options  The options
44
     *
45 1
     * @throws \Exception datatype cannot be null
46
     *
47 2
     * @return string The Yaml string content
48
     */
49
    public function toString($dataType, int $options = null):string
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function toString($dataType, /** @scrutinizer ignore-unused */ int $options = null):string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        if (empty($dataType)) throw new \Exception(self::class.": No content to convert to Yaml");
52
        if (is_scalar($dataType)) {
53
            return "--- ".$this->handler->dumpScalar($dataType). self::LINEFEED ;
0 ignored issues
show
Bug introduced by
The method dumpScalar() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            return "--- ".$this->handler->/** @scrutinizer ignore-call */ dumpScalar($dataType). self::LINEFEED ;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        }
55
        return $this->dump($dataType, 0);
56
    }
57
58
    /**
59
     * Calls and saves the result of Dumper::toString to the file $filePath provided
60
     *
61 1
     * @param string   $filePath The file path
62
     * @param mixed    $dataType The data type
63 1
     * @param int|null $options  The options
64
     *
65
     * @throws \Exception datatype cannot be null
66
     *
67
     * @return bool true = if the file has been correctly saved  ( return value from 'file_put_contents')
68
     */
69
    public function toFile(string $filePath, $dataType, int $options = null):bool
70
    {
71
        return !is_bool(file_put_contents($filePath, $this->toString($dataType, $options)));
72
    }
73
74
75
76
    public function dump($dataType, int $indent):string
77
    {
78
        if (is_null($dataType)) {
79
            return '';
80
        } elseif (is_resource($dataType)) {
81
            return get_resource_type($dataType);
82
        } elseif (is_scalar($dataType)) {
83
            return $this->handler->dumpScalar($dataType);
84
        } else {
85
            return $this->handler->dumpCompound($dataType, $indent);
86
        }
87
    }
88
89
90
    public function dumpMultiDoc($arrayOfYamlObject)
91
    {
92
        foreach ($arrayOfYamlObject as $key => $yamlObject) {
93
94
        }
95
    }
96
97
    /**
98
     * Dumps an yaml object to a YAML string
99
     *
100
     * @param      YamlObject  $obj    The object
101
     *
102
     * @return     string      YAML formatted string
103
     * @todo  export comment from YamlObject
104
     */
105
    public function dumpYamlObject(YamlObject $obj):string
106
    {
107
        if ($this->multipleDocs || $obj->hasDocStart() || $obj->isTagged()) {
108
           $this->multipleDocs = true;
109
          // && $this->$result instanceof DLL) $this->$result->push("---");
110
        }
111
        // $this->insertComments($obj->getComment());
112
        if (count($obj) > 0) {
113
            return $this->iteratorToString($obj, '-', "\n", 0);
114
        } else {
115
            return $this->iteratorToString(new \ArrayIterator(get_object_vars($obj)), '%s:', "\n", 0);
116
        }
117
    }
118
119
120
    public function iteratorToString(\Iterator $iterable,
121
                                      string $keyMask, string $itemSeparator, int $indent):string
122
    {
123
        $pairs = [];
124
        $valueIndent = $indent + self::INDENT;
125
        foreach ($iterable as $key => $value) {
126
            $separator = "\n";
127
            if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) {
128
                $separator   = ' ';
129
                $valueIndent = 0;
130
            }
131
            if ($this->_compactMode) {
132
                $pairs[] = sprintf($keyMask, $key).$this->dump($value, $valueIndent);
133
            } else {
134
                $pairs[] = str_repeat(' ', $indent).sprintf($keyMask, $key).$separator.$this->dump($value, $valueIndent);
135
            }
136
137
        }
138
        // $processItem = function ()
139
        return implode($itemSeparator, $pairs);
140
        // return implode($itemSeparator, array_map(callback, arr1));
141
    }
142
}
143