Spaces:
No application file
No application file
File size: 4,000 Bytes
d2897cd |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
<?php
namespace Mautic\ConfigBundle\Event;
use Mautic\CoreBundle\Event\CommonEvent;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\ParameterBag;
class ConfigEvent extends CommonEvent
{
/**
* @var mixed[]
*/
private array $preserve = [];
/**
* @var mixed[]
*/
private array $errors = [];
/**
* @var mixed[]
*/
private array $fieldErrors = [];
/**
* Data got from build form before update.
*/
private ?array $originalNormData = null;
/**
* Data got from build form after update.
*
* @var array
*/
private $normData;
/**
* @param mixed[]|null $config
*/
public function __construct(
private ?array $config,
private ParameterBag $post
) {
}
/**
* Returns the config array.
*
* @param string $key
*
* @return array
*/
public function getConfig($key = null)
{
if ($key) {
return $this->config[$key] ?? [];
}
return $this->config;
}
/**
* Sets the config array.
*
* @param string $key
*/
public function setConfig(array $config, $key = null): void
{
if ($key) {
$this->config[$key] = $config;
} else {
$this->config = $config;
}
}
public function getPost(): ParameterBag
{
return $this->post;
}
/**
* Set fields such as passwords that will not overwrite existing values
* if the current is empty.
*
* @param array|string $fields
*/
public function unsetIfEmpty($fields): void
{
if (!is_array($fields)) {
$fields = [$fields];
}
$this->preserve = array_merge($this->preserve, $fields);
}
/**
* Return array of fields to unset if empty so that existing values are not
* overwritten if empty.
*
* @return array
*/
public function getPreservedFields()
{
return $this->preserve;
}
/**
* Set error message.
*
* @param string $message (untranslated)
* @param array $messageVars for translation
* @param string|null $key
* @param string|null $field
*
* @return ConfigEvent
*/
public function setError($message, $messageVars = [], $key = null, $field = null)
{
if (!empty($key) && !empty($field)) {
if (!isset($this->errors[$key])) {
$this->fieldErrors[$key] = [];
}
$this->fieldErrors[$key][$field] = [
$message,
$messageVars,
];
return $this;
}
$this->errors[$message] = $messageVars;
return $this;
}
/**
* Get error messages.
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* @return array
*/
public function getFieldErrors()
{
return $this->fieldErrors;
}
public function getFileContent(UploadedFile $file): string
{
$tmpFile = $file->getRealPath();
$content = trim(file_get_contents($tmpFile));
@unlink($tmpFile);
return $content;
}
public function encodeFileContents($content): string
{
return base64_encode($content);
}
/**
* @return array
*/
public function getOriginalNormData()
{
return $this->originalNormData;
}
/**
* @return ConfigEvent
*/
public function setOriginalNormData(array $normData)
{
$this->originalNormData = $normData;
return $this;
}
/**
* @return array
*/
public function getNormData()
{
return $this->normData;
}
/**
* @param array $normData
*/
public function setNormData($normData): void
{
$this->normData = $normData;
}
}
|