Completed
Push — master ( 0826f5...8a4d66 )
by Jan
03:23
created

NamedDBElement::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Part-DB Version 0.4+ "nextgen"
6
 * Copyright (C) 2016 - 2019 Jan Böhmer
7
 * https://github.com/jbtronics.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22
 */
23
24
namespace App\Entity;
25
26
use Doctrine\ORM\Mapping as ORM;
27
use Symfony\Component\Validator\Constraints as Assert;
28
use Symfony\Component\Serializer\Annotation\Groups;
29
30
/**
31
 * All subclasses of this class have an attribute "name".
32
 *
33
 * @ORM\MappedSuperclass()
34
 * @ORM\HasLifecycleCallbacks()
35
 */
36
abstract class NamedDBElement extends DBElement
37
{
38
    /**
39
     * @var string The name of this element.
40
     * @ORM\Column(type="string")
41
     * @Assert\NotBlank()
42
     * @Groups({"simple", "extended", "full"})
43
     */
44
    protected $name = '';
45
46
    /**
47
     * @var \DateTime The date when this element was modified the last time.
48
     * @ORM\Column(type="datetimetz", name="last_modified")
49
     * @Groups({"extended", "full"})
50
     */
51
    protected $lastModified;
52
53
    /**
54
     * @var \DateTime The date when this element was created.
55
     * @ORM\Column(type="datetimetz", name="datetime_added")
56
     * @Groups({"extended", "full"})
57
     */
58
    protected $addedDate;
59
60
    /********************************************************************************
61
     *
62
     *   Getters
63
     *
64
     *********************************************************************************/
65
66
    /**
67
     * Get the name.
68
     *
69
     * @return string the name of this element
70
     */
71
    public function getName(): string
72
    {
73
        /*
74
        //Strip HTML from Name, so no XSS injection is possible.
75
        return strip_tags($this->name); */
76
        return $this->name;
77
    }
78
79
    /**
80
     * Returns the last time when the element was modified.
81
     * Returns null if the element was not yet saved to DB yet.
82
     *
83
     * @return \DateTime|null The time of the last edit.
84
     */
85
    public function getLastModified(): ?\DateTime
86
    {
87
        return $this->lastModified;
88
    }
89
90
    /**
91
     * Returns the date/time when the element was created.
92
     * Returns null if the element was not yet saved to DB yet.
93
     *
94
     * @return \DateTime|null The creation time of the part.
95
     */
96
    public function getAddedDate(): ?\DateTime
97
    {
98
        return $this->addedDate;
99
    }
100
101
    /********************************************************************************
102
     *
103
     *   Setters
104
     *
105
     *********************************************************************************/
106
107
    /**
108
     * Change the name of this element.
109
     *
110
     *     Spaces at the begin and at the end of the string will be removed
111
     *          automatically in NamedDBElement::check_values_validity().
112
     *          So you don't have to do this yourself.
113
     *
114
     * @param string $new_name the new name
115
     *
116
     * @return self
117
     */
118
    public function setName(string $new_name): self
119
    {
120
        $this->name = $new_name;
121
122
        return $this;
123
    }
124
125
    /******************************************************************************
126
     *
127
     * Helpers
128
     *
129
     ******************************************************************************/
130
131
    /**
132
     * Helper for updating the timestamp. It is automatically called by doctrine before persisting.
133
     *
134
     * @ORM\PrePersist
135
     * @ORM\PreUpdate
136
     */
137
    public function updatedTimestamps(): void
138
    {
139
        $this->lastModified = new \DateTime('now');
140
        if (null === $this->addedDate) {
141
            $this->addedDate = new \DateTime('now');
142
        }
143
    }
144
145
    public function __toString()
146
    {
147
        return $this->getName();
148
    }
149
}
150