Completed
Push — 2.0 ( b99ec1...00bbc5 )
by David
10s
created

PatchType::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Mouf\Utils\Patcher;
5
6
7
use Mouf\MoufManager;
8
9
class PatchType implements \JsonSerializable
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
    /**
16
     * @var string
17
     */
18
    private $description;
19
20
    /**
21
     * @Important
22
     * @param string $name The name of the patch type. Should not contain special characters or spaces. Note: the default type is an empty string.
23
     * @param string $description The description of the patch type
24
     * @throws PatchException
25
     */
26
    public function __construct(string $name, string $description)
27
    {
28
        if (!preg_match('/^[a-z_\-0-9]*$/i', $name)) {
29
            throw new PatchException('A patch name can only contain alphanumeric characters and underscore. Name passed: "'.$name.'"');
30
        }
31
32
        $this->name = $name;
33
        $this->description = $description;
34
    }
35
36
    /**
37
     * The name of the patch type. Should not contain special characters or spaces.
38
     *
39
     * @return string
40
     */
41
    public function getName(): string
42
    {
43
        return $this->name;
44
    }
45
46
    /**
47
     * The description of the patch type.
48
     *
49
     * @return string
50
     */
51
    public function getDescription(): string
52
    {
53
        return $this->description;
54
    }
55
56
    /**
57
     * Specify data which should be serialized to JSON
58
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
59
     * @return mixed data which can be serialized by <b>json_encode</b>,
60
     * which is a value of any type other than a resource.
61
     * @since 5.4.0
62
     */
63
    public function jsonSerialize()
64
    {
65
        return [
66
            'name' => $this->name,
67
            'description' => $this->description,
68
            'instanceName' => MoufManager::getMoufManager()->findInstanceName($this)
69
        ];
70
    }
71
}
72