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