id = $this->validateId($id); $this->name = $this->validateString($name, 255, 'name'); $this->author = $this->validateString($author, 1000, 'author'); $this->starred = $this->validateBool($starred); $this->published = $this->validateBool($published); $this->dateCreated = gmdate('Y-m-d H:i:s'); $this->dateEdited = gmdate('Y-m-d H:i:s'); $this->fields = $fields ?? []; $this->description = $this->validateString($description, 1000, 'description'); $this->showTitle = $this->validateBool($showTitle); $this->showDescription = $this->validateBool($showDescription); $this->storeEntries = $this->validateBool($storeEntries); } /** * Validates the ID. * * @param int $id * * @return int * @throws ValidationException */ private function validateId(int $id): int { if ($id < 0) { throw new ValidationException("Id must be a positive integer."); } return $id; } /** * Validates a string value. * * @param string $value * @param int $maxLength * @param string $fieldName * * @return string * * @throws ValidationException */ private function validateString(string $value, int $maxLength, string $fieldName): string { if (strlen($value) > $maxLength) { throw new ValidationException( sprintf( /* translators: 1: String value, 2: String max length. */ esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'), esc_html($fieldName), $maxLength ) ); } return $value; } /** * Validates a boolean field. * * @param bool $value * * @return bool */ private function validateBool(bool $value): bool { return $value; } /** * Get the form ID. * * @return int */ public function getId(): int { return $this->id; } /** * Get the form name. * * @return string */ public function getName(): string { return $this->name; } /** * Get the form author. * * @return string */ public function getAuthor(): string { return $this->author; } /** * Get the form starred status. * * @return bool */ public function isStarred(): bool { return $this->starred; } /** * Get the form published status. * * @return bool */ public function isPublished(): bool { return $this->published; } /** * Get the date of form creation. * * @return string */ public function getDateCreated(): string { return $this->dateCreated; } /** * Get the form date edited. * * @return string */ public function getDateEdited(): string { return $this->dateEdited; } /** * Get the form fields. * * @return mixed[]|null */ public function getFields(): ?array { return $this->fields; } /** * Get the form description. * * @return string */ public function getDescription(): string { return $this->description; } /** * Get the show description of the form. * * @return bool The form show description status. */ public function isDescriptionVisible(): bool { return $this->showDescription; } /** * Get the form store entries' status. * @return bool */ public function isStoreEntries(): bool { return $this->storeEntries; } /** * Get the show title of the form. * * @return bool The form show title status. */ public function isTitleVisible(): bool { return $this->showTitle; } /** * Convert the form to an array. * * @return array */ public function toArray(): array { return [ 'id' => $this->getId(), 'name' => $this->getName(), 'author' => $this->getAuthor(), 'starred' => $this->isStarred(), 'published' => $this->isPublished(), 'fields' => $this->getFields(), 'description' => $this->getDescription(), 'showTitle' => $this->isTitleVisible(), 'showDescription' => $this->isDescriptionVisible(), 'storeEntries' => $this->isStoreEntries(), ]; } }