/
ImageStyle.php
89 lines (81 loc) · 2.99 KB
/
ImageStyle.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Concrete\Core\StyleCustomizer\Style;
use Concrete\Core\File\File;
use Concrete\Core\StyleCustomizer\Normalizer\ImageVariable;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollection;
use Concrete\Core\StyleCustomizer\Normalizer\VariableInterface;
use Concrete\Core\StyleCustomizer\Style\Value\ImageValue;
use Concrete\Core\StyleCustomizer\Style\Value\ValueInterface;
use Concrete\Core\Utility\Service\Validation\Numbers;
class ImageStyle extends Style
{
public function createImageVariable($variable, $url)
{
return new ImageVariable($variable, $url);
}
public function createValueFromVariableCollection(NormalizedVariableCollection $collection): ?ValueInterface
{
$variable = $collection->getVariable($this->getVariableToInspect());
$value = new ImageValue();
if ($variable instanceof ImageVariable) {
$numbers = new Numbers();
if ($numbers->integer($variable->getFileID())) {
// This is file ID.
$value->setImageFileID($variable->getFileID());
} else {
// is it a URL?
if ($variable->getUrl()) {
$value->setImageURL($variable->getUrl());
}
}
return $value;
} else {
// We've parsed the file and can only determine a text path, because it's coming from a text variable in
// .less or .scss
$value->setImageURL($variable->getValue());
}
return $value;
}
public function createValueFromRequestDataCollection(array $styles): ?ValueInterface
{
foreach ($styles as $style) {
if (isset($style['variable']) && $style['variable'] == $this->getVariable()) {
$value = new ImageValue();
if (!empty($style['value']['imageURL'])) {
$value->setImageURL($style['value']['imageURL']);
}
if (!empty($style['value']['imageFileID'])) {
$value->setImageFileID($style['value']['imageFileID']);
}
return $value;
}
}
return null;
}
/**
* @param ImageValue $value
* @return VariableInterface|null
*/
public function createVariableFromValue(ValueInterface $value): ?VariableInterface
{
$url = null;
$fID = null;
if ($value->getImageFileID()) {
// An editor/admin has set the background image using the customizer, so it takes precedence
$file = File::getByID($value->getImageFileID());
if ($file) {
$fID = $file->getFileID();
}
}
if (!$fID) {
if ($value->getImageURL()) {
$url = $value->getImageURL();
}
}
$variable = $this->createImageVariable($this->getVariableToInspect(), $url);
if ($fID) {
$variable->setFileID($fID);
}
return $variable;
}
}