Completed
Push — master ( 59b92d...81b834 )
by Dev
04:01
created

PageTrait::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Cocur\Slugify\Slugify;
7
use Gedmo\Mapping\Annotation as Gedmo;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
trait PageTrait
11
{
12
    /**
13
     * @Gedmo\Translatable
14
     * @ORM\Column(type="string", length=150)
15
     * @Assert\NotBlank
16
     */
17
    protected $slug;
18
19
    /**
20
     * @Gedmo\Translatable
21
     * @ORM\Column(type="string", length=200, nullable=true)
22
     */
23
    protected $title;
24
25
    /**
26
     * @Gedmo\Translatable
27
     * @ORM\Column(type="text", nullable=true)
28
     */
29
    protected $mainContent;
30
31
    /**
32
     * @ORM\Column(type="datetime")
33
     */
34
    protected $createdAt;
35
36
    /**
37
     * @ORM\Column(type="datetime")
38
     */
39
    protected $updatedAt;
40
41
    /**
42
     * @ORM\Column(type="integer")
43
     * @ORM\Version
44
     */
45
    protected $version;
46
47
    public function __toString()
48
    {
49
        return trim($this->slug.' ');
50
    }
51
52
    public function __constructPage()
53
    {
54
        $this->updatedAt = null !== $this->updatedAt ? $this->updatedAt : new \DateTime();
55
        $this->createdAt = null !== $this->createdAt ? $this->createdAt : new \DateTime();
56
    }
57
58
    public function getSlug(): ?string
59
    {
60
        return $this->slug;
61
    }
62
63
    public function getRealSlug(): ?string
64
    {
65
        if ('homepage' == $this->slug) {
66
            return '';
67
        }
68
69
        return $this->slug;
70
    }
71
72
    public function setSlug($slug, $set = false): self
73
    {
74
        if (true === $set) {
75
            $this->slug = $slug;
76
        } elseif (null === $slug) { // work around for disabled input in sonata admin
77
            if (null === $this->slug) {
78
                throw new \ErrorException('slug cant be null');
79
            }
80
        } else {
81
            $slg = new Slugify(['regexp' => '/[^A-Za-z0-9\/]+/']);
82
            $slug = $slg->slugify($slug);
83
            $slug = trim($slug, '/');
84
            $this->slug = $slug; //$this->setSlug(trim($slug, '/'), true);
85
        }
86
87
        return $this;
88
    }
89
90
    public function getTitle(): ?string
91
    {
92
        return $this->title;
93
    }
94
95
    public function setTitle(?string $title): self
96
    {
97
        $this->title = $title;
98
99
        return $this;
100
    }
101
102
    public function getMainContent(): ?string
103
    {
104
        return $this->mainContent;
105
    }
106
107
    public function setMainContent(?string $mainContent): self
108
    {
109
        $this->mainContent = $mainContent;
110
111
        return $this;
112
    }
113
114
    public function getCreatedAt(): ?\DateTimeInterface
115
    {
116
        return $this->createdAt;
117
    }
118
119
    public function setCreatedAt(\DateTimeInterface $createdAt): self
120
    {
121
        $this->createdAt = $createdAt;
122
123
        return $this;
124
    }
125
126
    public function getUpdatedAt(): ?\DateTimeInterface
127
    {
128
        return $this->updatedAt;
129
    }
130
131
    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
132
    {
133
        $this->updatedAt = $updatedAt;
134
135
        return $this;
136
    }
137
}
138