|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\Core\Entity; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ShortUrl |
|
11
|
|
|
* @author |
|
12
|
|
|
* @link |
|
13
|
|
|
* |
|
14
|
|
|
* @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\ShortUrlRepository") |
|
15
|
|
|
* @ORM\Table(name="short_urls") |
|
16
|
|
|
*/ |
|
17
|
|
|
class ShortUrl extends AbstractEntity implements \JsonSerializable |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
* @ORM\Column(name="original_url", type="string", nullable=false, length=1024) |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $originalUrl; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
* @ORM\Column( |
|
27
|
|
|
* name="short_code", |
|
28
|
|
|
* type="string", |
|
29
|
|
|
* nullable=false, |
|
30
|
|
|
* length=10, |
|
31
|
|
|
* unique=true, |
|
32
|
|
|
* options={"collation": "utf8_bin"} |
|
33
|
|
|
* ) |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $shortCode; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var \DateTime |
|
38
|
|
|
* @ORM\Column(name="date_created", type="datetime") |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $dateCreated; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var Collection|Visit[] |
|
43
|
|
|
* @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY") |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $visits; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* ShortUrl constructor. |
|
49
|
|
|
*/ |
|
50
|
11 |
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
11 |
|
$this->setDateCreated(new \DateTime()); |
|
53
|
11 |
|
$this->setVisits(new ArrayCollection()); |
|
54
|
11 |
|
$this->setShortCode(''); |
|
55
|
11 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getOriginalUrl() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->originalUrl; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $originalUrl |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function setOriginalUrl($originalUrl) |
|
70
|
|
|
{ |
|
71
|
4 |
|
$this->originalUrl = (string) $originalUrl; |
|
72
|
4 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getShortCode() |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->shortCode; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $shortCode |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
11 |
|
public function setShortCode($shortCode) |
|
88
|
|
|
{ |
|
89
|
11 |
|
$this->shortCode = $shortCode; |
|
90
|
11 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return \DateTime |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getDateCreated() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->dateCreated; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param \DateTime $dateCreated |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
11 |
|
public function setDateCreated($dateCreated) |
|
106
|
|
|
{ |
|
107
|
11 |
|
$this->dateCreated = $dateCreated; |
|
108
|
11 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Visit[]|Collection |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getVisits() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->visits; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param Visit[]|Collection $visits |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
11 |
|
public function setVisits($visits) |
|
124
|
|
|
{ |
|
125
|
11 |
|
$this->visits = $visits; |
|
126
|
11 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Specify data which should be serialized to JSON |
|
131
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
132
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
133
|
|
|
* which is a value of any type other than a resource. |
|
134
|
|
|
* @since 5.4.0 |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function jsonSerialize() |
|
137
|
|
|
{ |
|
138
|
|
|
return [ |
|
139
|
2 |
|
'shortCode' => $this->shortCode, |
|
140
|
2 |
|
'originalUrl' => $this->originalUrl, |
|
141
|
2 |
|
'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format(\DateTime::ISO8601) : null, |
|
142
|
2 |
|
'visitsCount' => count($this->visits), |
|
143
|
2 |
|
]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|