| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\Form; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ValidationException; |
| 6 |
use IvyForms\Services\Translations\BackendStrings; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class FormMetadata |
| 10 |
* Groups form metadata (author, dates, status flags) |
| 11 |
* |
| 12 |
* @package IvyForms\ValueObjects\Form |
| 13 |
*/ |
| 14 |
final class FormMetadata |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public string $author; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var bool |
| 23 |
*/ |
| 24 |
public bool $starred; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
public bool $published; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public string $dateCreated; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public string $dateEdited; |
| 40 |
|
| 41 |
/** |
| 42 |
* FormMetadata constructor. |
| 43 |
* |
| 44 |
* @param string $author |
| 45 |
* @param bool $starred |
| 46 |
* @param bool $published |
| 47 |
* @param string|null $dateCreated |
| 48 |
* @param string|null $dateEdited |
| 49 |
* @throws ValidationException |
| 50 |
*/ |
| 51 |
public function __construct( |
| 52 |
string $author, |
| 53 |
bool $starred, |
| 54 |
bool $published, |
| 55 |
?string $dateCreated = null, |
| 56 |
?string $dateEdited = null |
| 57 |
) { |
| 58 |
$now = gmdate('Y-m-d H:i:s'); |
| 59 |
|
| 60 |
$this->author = $this->validateString($author, 1000, 'author'); |
| 61 |
$this->starred = $starred; |
| 62 |
$this->published = $published; |
| 63 |
$this->dateCreated = $dateCreated ?? $now; |
| 64 |
$this->dateEdited = $dateEdited ?? $now; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Validates a string value. |
| 69 |
* |
| 70 |
* @param string $value |
| 71 |
* @param int $maxLength |
| 72 |
* @param string $fieldName |
| 73 |
* |
| 74 |
* @return string |
| 75 |
* |
| 76 |
* @throws ValidationException |
| 77 |
*/ |
| 78 |
private function validateString(string $value, int $maxLength, string $fieldName): string |
| 79 |
{ |
| 80 |
if (strlen($value) > $maxLength) { |
| 81 |
throw new ValidationException( |
| 82 |
sprintf( |
| 83 |
/* translators: 1: String value, 2: String max length. */ |
| 84 |
esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), |
| 85 |
esc_html($fieldName), |
| 86 |
$maxLength |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
return $value; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
public function getAuthor(): string |
| 97 |
{ |
| 98 |
return $this->author; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
public function isStarred(): bool |
| 105 |
{ |
| 106 |
return $this->starred; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
public function isPublished(): bool |
| 113 |
{ |
| 114 |
return $this->published; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function getDateCreated(): string |
| 121 |
{ |
| 122 |
return $this->dateCreated; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public function getDateEdited(): string |
| 129 |
{ |
| 130 |
return $this->dateEdited; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Convert to array |
| 135 |
* |
| 136 |
* @return array<string, mixed> |
| 137 |
*/ |
| 138 |
public function toArray(): array |
| 139 |
{ |
| 140 |
return [ |
| 141 |
'author' => $this->author, |
| 142 |
'starred' => $this->starred, |
| 143 |
'published' => $this->published, |
| 144 |
'dateCreated' => $this->dateCreated, |
| 145 |
'dateEdited' => $this->dateEdited, |
| 146 |
]; |
| 147 |
} |
| 148 |
} |
| 149 |
|