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

Mixin::toYaml()   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 4
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\VOArray\Mixin as VOArrayMixin;
6
7
/**
8
 * Class SringMixin
9
 *
10
 * @package BestServedCold\PhalueObjects\Format
11
 */
12
trait Mixin
13
{
14
    use VOArrayMixin;
15
16
    /**
17
     * @param  Yaml $yaml
18
     * @return static
19
     */
20
    public static function fromVOYaml(Yaml $yaml)
21
    {
22
        return static::fromArray($yaml->toArray());
0 ignored issues
show
Bug introduced by
The method fromArray() does not exist on BestServedCold\PhalueObjects\Format\String\Mixin. Did you maybe mean fromVOArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23
    }
24
25
    /**
26
     * @param  $yaml
27
     * @return static
28
     */
29
    public static function fromYaml($yaml)
30
    {
31
        return static::fromVOYaml(Yaml::fromString($yaml));
32
    }
33
34
    /**
35
     * @return Yaml
36
     */
37
    public function toVOYaml()
38
    {
39
        return Yaml::fromArray($this->toArray());
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function toYaml()
46
    {
47
        return self::toVOYaml()->getValue();
48
    }
49
50
    /**
51
     * @param  Xml    $xml
52
     * @return static
53
     */
54
    public function fromVOXml(Xml $xml)
55
    {
56
        return static::fromArray($xml->toArray());
0 ignored issues
show
Bug introduced by
The method fromArray() does not exist on BestServedCold\PhalueObjects\Format\String\Mixin. Did you maybe mean fromVOArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
    }
58
59
    /**
60
     * @param  $xml
61
     * @return static
62
     */
63
    public function fromXml($xml)
64
    {
65
        return self::fromVOXml(Xml::fromString($xml));
66
    }
67
68
    /**
69
     * @return Xml
70
     */
71
    public function toVOXml()
72
    {
73
        return Xml::fromArray($this->toArray());
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function toXml()
80
    {
81
        return $this->toVOXml()->getValue();
82
    }
83
84
    /**
85
     * @param  Json  $json
86
     * @return static
87
     */
88
    public static function fromVOJson(Json $json)
89
    {
90
        return static::fromArray($json->toArray());
0 ignored issues
show
Bug introduced by
The method fromArray() does not exist on BestServedCold\PhalueObjects\Format\String\Mixin. Did you maybe mean fromVOArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
    }
92
93
    /**
94
     * @param  string $json
95
     * @return static
96
     */
97
    public static function fromJson($json)
98
    {
99
        return self::fromVOJson(Json::fromString($json));
100
    }
101
102
    /**
103
     * @return static
0 ignored issues
show
Documentation introduced by
Should the return type not be Json?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
104
     */
105
    public function toVOJson()
106
    {
107
        return Json::fromArray($this->toArray());
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function toJson()
114
    {
115
        return $this->toVOJson()->getValue();
116
    }
117
}
118