EnumInteractsWithQueryBuilder.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\ValueObjects; |
| 4 | |
| 5 | use Give\Framework\Support\Facades\Str; |
| 6 | |
| 7 | trait EnumInteractsWithQueryBuilder |
| 8 | { |
| 9 | /** |
| 10 | * @since 2.19.6 |
| 11 | * |
| 12 | * Returns array of meta aliases to be used with attachMeta |
| 13 | * |
| 14 | * [ ['_give_payment_total', 'amount'], etc. ] |
| 15 | * |
| 16 | * @return array |
| 17 | */ |
| 18 | public static function getColumnsForAttachMetaQuery() |
| 19 | { |
| 20 | $columns = []; |
| 21 | |
| 22 | foreach (static::toArray() as $key => $value) { |
| 23 | $keyFormatted = Str::camel($key); |
| 24 | |
| 25 | $columns[] = [$value, $keyFormatted]; |
| 26 | } |
| 27 | |
| 28 | return $columns; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 2.24.0 |
| 33 | * |
| 34 | * Returns array of meta aliases to be used with attachMeta based on the given array of ENUMs |
| 35 | * |
| 36 | * [ ['_give_payment_total', 'amount'], etc. ] |
| 37 | * |
| 38 | * @param array<Enum> $enums An array of Enums. Eg.: [ DonationMetaKeys::AMOUNT(), etc. ] |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public static function getColumnsForAttachMetaQueryFromArray(array $enums): array |
| 43 | { |
| 44 | $columns = []; |
| 45 | |
| 46 | foreach ($enums as $enum) { |
| 47 | $value = $enum->getValue(); |
| 48 | $keyFormatted = $enum->getKeyAsCamelCase(); |
| 49 | |
| 50 | $columns[] = [$value, $keyFormatted]; |
| 51 | } |
| 52 | |
| 53 | return $columns; |
| 54 | } |
| 55 | } |
| 56 |