| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Donations\Models; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use Give\Donations\Factories\DonationNoteFactory; |
| 7 |
use Give\Donations\ValueObjects\DonationNoteType; |
| 8 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 9 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 10 |
use Give\Framework\Models\Contracts\ModelCrud; |
| 11 |
use Give\Framework\Models\Contracts\ModelHasFactory; |
| 12 |
use Give\Framework\Models\Model; |
| 13 |
use Give\Framework\Models\ModelQueryBuilder; |
| 14 |
use Give\Framework\Models\ValueObjects\Relationship; |
| 15 |
use Give\Framework\Support\Facades\DateTime\Temporal; |
| 16 |
|
| 17 |
/** |
| 18 |
* @since 2.21.0 |
| 19 |
* |
| 20 |
* @property int $id |
| 21 |
* @property int $donationId |
| 22 |
* @property string $content |
| 23 |
* @property DonationNoteType $type |
| 24 |
* @property DateTime $createdAt |
| 25 |
* @property Donation $donation |
| 26 |
*/ |
| 27 |
class DonationNote extends Model implements ModelCrud, ModelHasFactory |
| 28 |
{ |
| 29 |
/** |
| 30 |
* @inheritdoc |
| 31 |
*/ |
| 32 |
protected $properties = [ |
| 33 |
'id' => 'int', |
| 34 |
'donationId' => 'int', |
| 35 |
'content' => 'string', |
| 36 |
'type' => DonationNoteType::class, |
| 37 |
'createdAt' => DateTime::class, |
| 38 |
]; |
| 39 |
|
| 40 |
/** |
| 41 |
* @inheritdoc |
| 42 |
*/ |
| 43 |
protected $relationships = [ |
| 44 |
'donation' => Relationship::BELONGS_TO, |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* @since 2.21.0 |
| 49 |
* |
| 50 |
* @return DonationNote|null |
| 51 |
*/ |
| 52 |
public static function find($id) |
| 53 |
{ |
| 54 |
return give()->donations->notes->getById($id); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* @since 2.21.0 |
| 60 |
* |
| 61 |
* @return $this |
| 62 |
* @throws Exception|InvalidArgumentException |
| 63 |
*/ |
| 64 |
public static function create(array $attributes): DonationNote |
| 65 |
{ |
| 66 |
$donationNote = new static($attributes); |
| 67 |
|
| 68 |
give()->donations->notes->insert($donationNote); |
| 69 |
|
| 70 |
return $donationNote; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @since 2.21.0 |
| 75 |
* |
| 76 |
* @return void |
| 77 |
* @throws Exception|InvalidArgumentException |
| 78 |
*/ |
| 79 |
public function save() |
| 80 |
{ |
| 81 |
if (!$this->id) { |
| 82 |
give()->donations->notes->insert($this); |
| 83 |
} else{ |
| 84 |
give()->donations->notes->update($this); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @since 2.21.0 |
| 90 |
* |
| 91 |
* @throws Exception|InvalidArgumentException |
| 92 |
*/ |
| 93 |
public function delete(): bool |
| 94 |
{ |
| 95 |
return give()->donations->notes->delete($this); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @since 2.21.0 |
| 100 |
* |
| 101 |
* @return ModelQueryBuilder<DonationNote> |
| 102 |
*/ |
| 103 |
public static function query(): ModelQueryBuilder |
| 104 |
{ |
| 105 |
return give()->donations->notes->prepareQuery(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @since 2.21.0 |
| 110 |
* |
| 111 |
* @return ModelQueryBuilder<Donation> |
| 112 |
*/ |
| 113 |
public function donation(): ModelQueryBuilder |
| 114 |
{ |
| 115 |
return give()->donations->queryById($this->donationId); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @since 2.21.0 |
| 120 |
* |
| 121 |
* @param object $object |
| 122 |
*/ |
| 123 |
public static function fromQueryBuilderObject($object): DonationNote |
| 124 |
{ |
| 125 |
return new DonationNote([ |
| 126 |
'id' => (int)$object->id, |
| 127 |
'type' => $object->type ? new DonationNoteType($object->type) : DonationNoteType::ADMIN(), |
| 128 |
'donationId' => (int)$object->donationId, |
| 129 |
'content' => (string)$object->content, |
| 130 |
'createdAt' => Temporal::toDateTime($object->createdAt), |
| 131 |
]); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @since 2.21.0 |
| 136 |
*/ |
| 137 |
public static function factory(): DonationNoteFactory |
| 138 |
{ |
| 139 |
return new DonationNoteFactory(static::class); |
| 140 |
} |
| 141 |
} |
| 142 |
|