Completed
Pull Request — master (#4)
by
unknown
14:30 queued 10:00
created

PageTrait::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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