/
LinkDataFieldData.php
58 lines (50 loc) · 1.53 KB
/
LinkDataFieldData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Concrete\Core\Summary\Data\Field;
use Concrete\Core\Editor\LinkAbstractor;
use Concrete\Core\Entity\File\File as FileEntity;
use League\Url\Url;
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Concrete\Core\File\File;
/**
* Represents a link. Useful for linking to pages on your site, because the contents are run through the
* Concrete\Core\Editor\LinkAbstractor class, ensuring that if your site is migrated elsewhere the cached
* contents of the data field will translate to the new environment.
*/
class LinkDataFieldData implements DataFieldDataInterface
{
/**
* @var int
*/
protected $link;
public function __construct(string $link = null)
{
$this->setData($link);
}
/**
* @param string|Url
*/
public function setData($link): void
{
$abstractor = new LinkAbstractor();
$this->link = $abstractor->translateTo((string) $link);
}
public function __toString()
{
$abstractor = new LinkAbstractor();
return $abstractor->translateFrom($this->link);
}
public function jsonSerialize()
{
return [
'class' => self::class,
'link' => (string) $this->link
];
}
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = [])
{
if (isset($data['link'])) {
$this->setData($data['link']);
}
}
}