| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\Models; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use Give\DonationForms\Actions\ConvertQueryDataToDonationForm; |
| 7 |
use Give\DonationForms\Factories\DonationFormFactory; |
| 8 |
use Give\DonationForms\Properties\FormSettings; |
| 9 |
use Give\DonationForms\Repositories\DonationFormRepository; |
| 10 |
use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 11 |
use Give\Framework\Blocks\BlockCollection; |
| 12 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 13 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 14 |
use Give\Framework\FieldsAPI\DonationForm as Form; |
| 15 |
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException; |
| 16 |
use Give\Framework\Models\Contracts\ModelCrud; |
| 17 |
use Give\Framework\Models\Contracts\ModelHasFactory; |
| 18 |
use Give\Framework\Models\Model; |
| 19 |
use Give\Framework\Models\ModelQueryBuilder; |
| 20 |
|
| 21 |
/** |
| 22 |
* @since 3.0.0 |
| 23 |
* |
| 24 |
* @property int $id |
| 25 |
* @property string $title |
| 26 |
* @property DateTime $createdAt |
| 27 |
* @property DateTime $updatedAt |
| 28 |
* @property DonationFormStatus $status |
| 29 |
* @property FormSettings $settings |
| 30 |
* @property BlockCollection $blocks |
| 31 |
*/ |
| 32 |
class DonationForm extends Model implements ModelCrud, ModelHasFactory |
| 33 |
{ |
| 34 |
/** |
| 35 |
* @inheritdoc |
| 36 |
*/ |
| 37 |
protected $properties = [ |
| 38 |
'id' => 'int', |
| 39 |
'title' => 'string', |
| 40 |
'createdAt' => DateTime::class, |
| 41 |
'updatedAt' => DateTime::class, |
| 42 |
'status' => DonationFormStatus::class, |
| 43 |
'settings' => FormSettings::class, |
| 44 |
'blocks' => BlockCollection::class, |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* @since 3.0.0 |
| 49 |
* @return DonationFormFactory |
| 50 |
*/ |
| 51 |
public static function factory(): DonationFormFactory |
| 52 |
{ |
| 53 |
return new DonationFormFactory(static::class); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Find donation form by ID |
| 58 |
* |
| 59 |
* @since 3.0.0 |
| 60 |
* |
| 61 |
* @param int $id |
| 62 |
* |
| 63 |
* @return DonationForm|null |
| 64 |
*/ |
| 65 |
public static function find($id) |
| 66 |
{ |
| 67 |
return give(DonationFormRepository::class)->getById($id); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @since 3.0.0 |
| 72 |
* |
| 73 |
* @param array $attributes |
| 74 |
* |
| 75 |
* @return DonationForm |
| 76 |
* @throws Exception |
| 77 |
*/ |
| 78 |
public static function create(array $attributes): DonationForm |
| 79 |
{ |
| 80 |
$donationForm = new static($attributes); |
| 81 |
|
| 82 |
give(DonationFormRepository::class)->insert($donationForm); |
| 83 |
|
| 84 |
return $donationForm; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @since 3.0.0 |
| 89 |
* |
| 90 |
* @return void |
| 91 |
* |
| 92 |
* @throws Exception|InvalidArgumentException |
| 93 |
*/ |
| 94 |
public function save() |
| 95 |
{ |
| 96 |
if (!$this->id) { |
| 97 |
give(DonationFormRepository::class)->insert($this); |
| 98 |
} else { |
| 99 |
give(DonationFormRepository::class)->update($this); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @since 3.0.0 |
| 105 |
* |
| 106 |
* @throws Exception |
| 107 |
*/ |
| 108 |
public function delete(): bool |
| 109 |
{ |
| 110 |
return give(DonationFormRepository::class)->delete($this); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @since 3.0.0 |
| 115 |
* |
| 116 |
* @return ModelQueryBuilder<DonationForm> |
| 117 |
*/ |
| 118 |
public static function query(): ModelQueryBuilder |
| 119 |
{ |
| 120 |
return give(DonationFormRepository::class)->prepareQuery(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @since 3.0.0 |
| 125 |
* |
| 126 |
* @param object $object |
| 127 |
*/ |
| 128 |
public static function fromQueryBuilderObject($object): DonationForm |
| 129 |
{ |
| 130 |
return (new ConvertQueryDataToDonationForm())($object); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* |
| 135 |
* @since 3.0.0 |
| 136 |
* @throws NameCollisionException |
| 137 |
*/ |
| 138 |
public function schema(): Form |
| 139 |
{ |
| 140 |
return give(DonationFormRepository::class)->getFormSchemaFromBlocks($this->id, $this->blocks); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @since 4.7.0 |
| 145 |
*/ |
| 146 |
public function getColorSettings(): array |
| 147 |
{ |
| 148 |
return give(DonationFormRepository::class)->getColorSettings($this); |
| 149 |
} |
| 150 |
} |
| 151 |
|