| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\ValueObjects\Form; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Options controlling which sections are included in a form export. |
| 12 |
*/ |
| 13 |
final class FormExportOptions |
| 14 |
{ |
| 15 |
private bool $includeStyles; |
| 16 |
private bool $includeEntries; |
| 17 |
|
| 18 |
private function __construct(bool $includeStyles, bool $includeEntries) |
| 19 |
{ |
| 20 |
$this->includeStyles = $includeStyles; |
| 21 |
$this->includeEntries = $includeEntries; |
| 22 |
} |
| 23 |
|
| 24 |
public static function withStyles(): self |
| 25 |
{ |
| 26 |
return new self(true, false); |
| 27 |
} |
| 28 |
|
| 29 |
public static function withoutStyles(): self |
| 30 |
{ |
| 31 |
return new self(false, false); |
| 32 |
} |
| 33 |
|
| 34 |
public static function withEntries(): self |
| 35 |
{ |
| 36 |
return new self(true, true); |
| 37 |
} |
| 38 |
|
| 39 |
public static function withoutEntries(): self |
| 40 |
{ |
| 41 |
return new self(true, false); |
| 42 |
} |
| 43 |
|
| 44 |
public function includesStyles(): bool |
| 45 |
{ |
| 46 |
return $this->includeStyles; |
| 47 |
} |
| 48 |
|
| 49 |
public function includesEntries(): bool |
| 50 |
{ |
| 51 |
return $this->includeEntries; |
| 52 |
} |
| 53 |
|
| 54 |
public function withIncludeEntries(bool $includeEntries): self |
| 55 |
{ |
| 56 |
return new self($this->includeStyles, $includeEntries); |
| 57 |
} |
| 58 |
|
| 59 |
public function withoutEntriesIncluded(): self |
| 60 |
{ |
| 61 |
return new self($this->includeStyles, false); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param mixed $includeStylesParam Request value for includeStyles. |
| 66 |
* @param mixed $includeEntriesParam Request value for includeEntries. |
| 67 |
*/ |
| 68 |
public static function fromRequestParams($includeStylesParam, $includeEntriesParam): self |
| 69 |
{ |
| 70 |
$options = self::fromIncludeStylesParam($includeStylesParam); |
| 71 |
|
| 72 |
if (filter_var($includeEntriesParam, FILTER_VALIDATE_BOOLEAN)) { |
| 73 |
return $options->withIncludeEntries(true); |
| 74 |
} |
| 75 |
|
| 76 |
return $options; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param mixed $includeStylesParam Request value for includeStyles. |
| 81 |
*/ |
| 82 |
public static function fromIncludeStylesParam($includeStylesParam): self |
| 83 |
{ |
| 84 |
if ($includeStylesParam === null) { |
| 85 |
return self::withStyles(); |
| 86 |
} |
| 87 |
|
| 88 |
if (filter_var($includeStylesParam, FILTER_VALIDATE_BOOLEAN)) { |
| 89 |
return self::withStyles(); |
| 90 |
} |
| 91 |
|
| 92 |
return self::withoutStyles(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param array<string, mixed> $data |
| 97 |
*/ |
| 98 |
public static function fromArray(array $data): self |
| 99 |
{ |
| 100 |
$includeStyles = filter_var($data['includeStyles'] ?? true, FILTER_VALIDATE_BOOLEAN); |
| 101 |
$includeEntries = filter_var($data['includeEntries'] ?? false, FILTER_VALIDATE_BOOLEAN); |
| 102 |
|
| 103 |
return new self($includeStyles, $includeEntries); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @return array{includeStyles: bool, includeEntries: bool} |
| 108 |
*/ |
| 109 |
public function toArray(): array |
| 110 |
{ |
| 111 |
return [ |
| 112 |
'includeStyles' => $this->includeStyles, |
| 113 |
'includeEntries' => $this->includeEntries, |
| 114 |
]; |
| 115 |
} |
| 116 |
} |
| 117 |
|