Completed
Pull Request — master (#24)
by Adam
02:31
created

AbstractString::toVOXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Format\String;
4
5
use BestServedCold\PhalueObjects\VOString;
6
use BestServedCold\PhalueObjects\VOArray\Mixin;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BestServedCold\PhalueObjects\Format\String\Mixin.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
/**
9
 * Class AbstractString
10
 *
11
 * @package BestServedCold\PhalueObjects\Format\String
12
 */
13
abstract class AbstractString extends VOString
14
{
15
    use Mixin;
16
17
    /**
18
     * @return array
19
     */
20
    abstract public function toArray();
21
22
    /**
23
     * @param  array      $array
24
     * @throws \Exception
25
     */
26
    public static function fromArray(array $array)
27
    {
28
        throw new \Exception(
29
            'Method [fromArray] with must be implemented in child class'.
30
            'and type ['.gettype($array).'] must be array.'
31
        );
32
    }
33
34
    /**
35
     * @param  Yaml $yaml
36
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
37
     */
38
    public static function fromVOYaml(Yaml $yaml)
39
    {
40
        return static::fromArray($yaml->toArray());
41
    }
42
43
    /**
44
     * @param  $yaml
45
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
46
     */
47
    public static function fromYaml($yaml)
48
    {
49
        return static::fromVOYaml(Yaml::fromString($yaml));
50
    }
51
52
    /**
53
     * @return Yaml
54
     */
55
    public function toVOYaml()
56
    {
57
        return Yaml::fromArray($this->toArray());
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function toYaml()
64
    {
65
        return self::toVOYaml()->getValue();
66
    }
67
68
    /**
69
     * @param  Xml    $xml
70
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
71
     */
72
    public function fromVOXml(Xml $xml)
73
    {
74
        return static::fromArray($xml->toArray());
75
    }
76
77
    /**
78
     * @param  $xml
79
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
80
     */
81
    public function fromXml($xml)
82
    {
83
        return self::fromVOXml(Xml::fromString($xml));
84
    }
85
86
    /**
87
     * @return Xml
88
     */
89
    public function toVOXml()
90
    {
91
        return Xml::fromArray($this->toArray());
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function toXml()
98
    {
99
        return $this->toVOXml()->getValue();
100
    }
101
102
    /**
103
     * @param  Json  $json
104
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
105
     */
106
    public static function fromVOJson(Json $json)
107
    {
108
        return static::fromArray($json->toArray());
109
    }
110
111
    /**
112
     * @param  string $json
113
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
114
     */
115
    public static function fromJson($json)
116
    {
117
        return self::fromVOJson(Json::fromString($json));
118
    }
119
120
    /**
121
     * @return static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Json.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
122
     */
123
    public function toVOJson()
124
    {
125
        return Json::fromArray($this->toArray());
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function toJson()
132
    {
133
        return $this->toVOJson()->getValue();
134
    }
135
}
136