| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * TransactionGroupTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2022 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-16 19:29:53 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | namespace FireflyIII\Transformers\V2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							|  |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use FireflyIII\Support\Http\Api\ConvertsExchangeRates; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use FireflyIII\Support\Http\Api\ExchangeRateConverter; | 
					
						
							| 
									
										
										
										
											2022-07-03 08:33:01 +02:00
										 |  |  | use FireflyIII\Support\NullArrayObject; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							|  |  |  | use stdClass; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionGroupTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class TransactionGroupTransformer extends AbstractTransformer | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |     private ExchangeRateConverter $converter; | 
					
						
							|  |  |  |     private array                 $currencies = []; | 
					
						
							|  |  |  |     private TransactionCurrency   $default; | 
					
						
							|  |  |  |     private array                 $meta; | 
					
						
							|  |  |  |     private array                 $notes; | 
					
						
							|  |  |  |     private array                 $tags; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |      * @inheritDoc | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function collectMetaData(Collection $objects): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // start with currencies:
 | 
					
						
							|  |  |  |         $currencies = []; | 
					
						
							|  |  |  |         $journals   = []; | 
					
						
							|  |  |  |         /** @var array $object */ | 
					
						
							|  |  |  |         foreach ($objects as $object) { | 
					
						
							|  |  |  |             foreach ($object['sums'] as $sum) { | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |                 $id              = (int)$sum['currency_id']; | 
					
						
							|  |  |  |                 $currencies[$id] = $currencies[$id] ?? TransactionCurrency::find($sum['currency_id']); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             /** @var array $transaction */ | 
					
						
							|  |  |  |             foreach ($object['transactions'] as $transaction) { | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |                 $id            = (int)$transaction['transaction_journal_id']; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 $journals[$id] = []; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->currencies = $currencies; | 
					
						
							|  |  |  |         $this->default    = app('amount')->getDefaultCurrency(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // grab meta for all journals:
 | 
					
						
							|  |  |  |         $meta = TransactionJournalMeta::whereIn('transaction_journal_id', array_keys($journals))->get(); | 
					
						
							|  |  |  |         /** @var TransactionJournalMeta $entry */ | 
					
						
							|  |  |  |         foreach ($meta as $entry) { | 
					
						
							|  |  |  |             $id                            = (int)$entry->transaction_journal_id; | 
					
						
							|  |  |  |             $this->meta[$id][$entry->name] = $entry->data; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // grab all notes for all journals:
 | 
					
						
							|  |  |  |         $notes = Note::whereNoteableType(TransactionJournal::class)->whereIn('noteable_id', array_keys($journals))->get(); | 
					
						
							|  |  |  |         /** @var Note $note */ | 
					
						
							|  |  |  |         foreach ($notes as $note) { | 
					
						
							|  |  |  |             $id               = (int)$note->noteable_id; | 
					
						
							|  |  |  |             $this->notes[$id] = $note; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // grab all tags for all journals:
 | 
					
						
							|  |  |  |         $tags = DB::table('tag_transaction_journal') | 
					
						
							|  |  |  |                   ->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id') | 
					
						
							|  |  |  |                   ->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($journals)) | 
					
						
							|  |  |  |                   ->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag']); | 
					
						
							|  |  |  |         /** @var stdClass $tag */ | 
					
						
							|  |  |  |         foreach ($tags as $tag) { | 
					
						
							|  |  |  |             $id                = (int)$tag->transaction_journal_id; | 
					
						
							|  |  |  |             $this->tags[$id][] = $tag->tag; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // create converter
 | 
					
						
							|  |  |  |         $this->converter = new ExchangeRateConverter(); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param array $group | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(array $group): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $first = reset($group['transactions']); | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             'id'           => (string)$group['id'], | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'created_at'   => $first['created_at']->toAtomString(), | 
					
						
							|  |  |  |             'updated_at'   => $first['updated_at']->toAtomString(), | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             'user'         => (string)$first['user_id'], | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'user_group'   => (string)$first['user_group_id'], | 
					
						
							| 
									
										
										
										
											2022-07-05 19:12:22 +02:00
										 |  |  |             'group_title'  => $group['title'] ?? null, | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'transactions' => $this->transformTransactions($group['transactions'] ?? []), | 
					
						
							|  |  |  |             'links'        => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2022-07-03 08:33:01 +02:00
										 |  |  |                     'uri' => sprintf('/transactions/%d', $group['id']), | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param array $transactions | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function transformTransactions(array $transactions): array | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $return = []; | 
					
						
							|  |  |  |         /** @var array $transaction */ | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $return[] = $this->transformTransaction($transaction); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array $transaction | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |     private function transformTransaction(array $transaction): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |         $transaction = new NullArrayObject($transaction); | 
					
						
							|  |  |  |         $type        = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL); | 
					
						
							|  |  |  |         $journalId   = (int)$transaction['transaction_journal_id']; | 
					
						
							|  |  |  |         $meta        = new NullArrayObject($this->meta[$journalId] ?? []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * Convert and use amount: | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         $amount              = app('steam')->positive((string)($transaction['amount'] ?? '0')); | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |         $currencyId          = (int)$transaction['currency_id']; | 
					
						
							|  |  |  |         $nativeAmount        = $this->converter->convert($this->default, $this->currencies[$currencyId], $transaction['date'], $amount); | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |         $foreignAmount       = null; | 
					
						
							| 
									
										
										
										
											2022-07-17 08:51:53 +02:00
										 |  |  |         $nativeForeignAmount = null; | 
					
						
							| 
									
										
										
										
											2022-07-03 08:33:01 +02:00
										 |  |  |         if (null !== $transaction['foreign_amount']) { | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             $foreignCurrencyId   = (int)$transaction['foreign_currency_id']; | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |             $foreignAmount       = app('steam')->positive($transaction['foreign_amount']); | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             $nativeForeignAmount = $this->converter->convert($this->default, $this->currencies[$foreignCurrencyId], $transaction['date'], $foreignAmount); | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             'user'                            => (string)$transaction['user_id'], | 
					
						
							|  |  |  |             'user_group'                      => (string)$transaction['user_group_id'], | 
					
						
							|  |  |  |             'transaction_journal_id'          => (string)$transaction['transaction_journal_id'], | 
					
						
							|  |  |  |             'type'                            => strtolower($type), | 
					
						
							|  |  |  |             'date'                            => $transaction['date']->toAtomString(), | 
					
						
							|  |  |  |             'order'                           => $transaction['order'], | 
					
						
							|  |  |  |             'amount'                          => $amount, | 
					
						
							|  |  |  |             'native_amount'                   => $nativeAmount, | 
					
						
							|  |  |  |             'foreign_amount'                  => $foreignAmount, | 
					
						
							|  |  |  |             'native_foreign_amount'           => $nativeForeignAmount, | 
					
						
							|  |  |  |             'currency_id'                     => (string)$transaction['currency_id'], | 
					
						
							|  |  |  |             'currency_code'                   => $transaction['currency_code'], | 
					
						
							|  |  |  |             'currency_name'                   => $transaction['currency_name'], | 
					
						
							|  |  |  |             'currency_symbol'                 => $transaction['currency_symbol'], | 
					
						
							|  |  |  |             'currency_decimal_places'         => (int)$transaction['currency_decimal_places'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // converted to native currency
 | 
					
						
							| 
									
										
										
										
											2023-09-17 09:45:34 +02:00
										 |  |  |             'native_currency_id'              => (string)$this->default->id, | 
					
						
							|  |  |  |             'native_currency_code'            => $this->default->code, | 
					
						
							|  |  |  |             'native_currency_name'            => $this->default->name, | 
					
						
							|  |  |  |             'native_currency_symbol'          => $this->default->symbol, | 
					
						
							|  |  |  |             'native_currency_decimal_places'  => (int)$this->default->decimal_places, | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             // foreign currency amount:
 | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'foreign_currency_id'             => $this->stringFromArray($transaction, 'foreign_currency_id', null), | 
					
						
							|  |  |  |             'foreign_currency_code'           => $transaction['foreign_currency_code'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |             'foreign_currency_name'           => $transaction['foreign_currency_name'], | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  |             'foreign_currency_symbol'         => $transaction['foreign_currency_symbol'], | 
					
						
							|  |  |  |             'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'], | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |             // foreign converted to native:
 | 
					
						
							|  |  |  |             'description'                     => $transaction['description'], | 
					
						
							|  |  |  |             'source_id'                       => (string)$transaction['source_account_id'], | 
					
						
							|  |  |  |             'source_name'                     => $transaction['source_account_name'], | 
					
						
							|  |  |  |             'source_iban'                     => $transaction['source_account_iban'], | 
					
						
							|  |  |  |             'source_type'                     => $transaction['source_account_type'], | 
					
						
							|  |  |  |             'destination_id'                  => (string)$transaction['destination_account_id'], | 
					
						
							|  |  |  |             'destination_name'                => $transaction['destination_account_name'], | 
					
						
							|  |  |  |             'destination_iban'                => $transaction['destination_account_iban'], | 
					
						
							|  |  |  |             'destination_type'                => $transaction['destination_account_type'], | 
					
						
							|  |  |  |             'budget_id'                       => $this->stringFromArray($transaction, 'budget_id', null), | 
					
						
							|  |  |  |             'budget_name'                     => $transaction['budget_name'], | 
					
						
							|  |  |  |             'category_id'                     => $this->stringFromArray($transaction, 'category_id', null), | 
					
						
							|  |  |  |             'category_name'                   => $transaction['category_name'], | 
					
						
							|  |  |  |             'bill_id'                         => $this->stringFromArray($transaction, 'bill_id', null), | 
					
						
							|  |  |  |             'bill_name'                       => $transaction['bill_name'], | 
					
						
							|  |  |  |             'reconciled'                      => $transaction['reconciled'], | 
					
						
							|  |  |  |             'notes'                           => $this->notes[$journalId] ?? null, | 
					
						
							|  |  |  |             'tags'                            => $this->tags[$journalId] ?? [], | 
					
						
							|  |  |  |             'internal_reference'              => $meta['internal_reference'], | 
					
						
							|  |  |  |             'external_id'                     => $meta['external_id'], | 
					
						
							|  |  |  |             'original_source'                 => $meta['original_source'], | 
					
						
							|  |  |  |             'recurrence_id'                   => $meta['recurrence_id'], | 
					
						
							|  |  |  |             'recurrence_total'                => $meta['recurrence_total'], | 
					
						
							|  |  |  |             'recurrence_count'                => $meta['recurrence_count'], | 
					
						
							|  |  |  |             'bunq_payment_id'                 => $meta['bunq_payment_id'], | 
					
						
							|  |  |  |             'external_url'                    => $meta['external_url'], | 
					
						
							|  |  |  |             'import_hash_v2'                  => $meta['import_hash_v2'], | 
					
						
							|  |  |  |             'sepa_cc'                         => $meta['sepa_cc'], | 
					
						
							|  |  |  |             'sepa_ct_op'                      => $meta['sepa_ct_op'], | 
					
						
							|  |  |  |             'sepa_ct_id'                      => $meta['sepa_ct_id'], | 
					
						
							|  |  |  |             'sepa_db'                         => $meta['sepa_db'], | 
					
						
							|  |  |  |             'sepa_country'                    => $meta['sepa_country'], | 
					
						
							|  |  |  |             'sepa_ep'                         => $meta['sepa_ep'], | 
					
						
							|  |  |  |             'sepa_ci'                         => $meta['sepa_ci'], | 
					
						
							|  |  |  |             'sepa_batch_id'                   => $meta['sepa_batch_id'], | 
					
						
							|  |  |  |             'interest_date'                   => $this->date($meta['interest_date']), | 
					
						
							|  |  |  |             'book_date'                       => $this->date($meta['book_date']), | 
					
						
							|  |  |  |             'process_date'                    => $this->date($meta['process_date']), | 
					
						
							|  |  |  |             'due_date'                        => $this->date($meta['due_date']), | 
					
						
							|  |  |  |             'payment_date'                    => $this->date($meta['payment_date']), | 
					
						
							|  |  |  |             'invoice_date'                    => $this->date($meta['invoice_date']), | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // location data
 | 
					
						
							|  |  |  |             //            'longitude'     => $longitude,
 | 
					
						
							|  |  |  |             //            'latitude'      => $latitude,
 | 
					
						
							|  |  |  |             //            'zoom_level'    => $zoomLevel,
 | 
					
						
							|  |  |  |             //
 | 
					
						
							|  |  |  |             //            'has_attachments' => $this->hasAttachments((int) $row['transaction_journal_id']),
 | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * TODO also in the old transformer. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-08-08 15:03:58 +02:00
										 |  |  |      * Used to extract a value from the given array, and fall back on a sensible default or NULL | 
					
						
							|  |  |  |      * if it can't be helped. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param NullArrayObject $array | 
					
						
							|  |  |  |      * @param string          $key | 
					
						
							|  |  |  |      * @param string|null     $default | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string|null | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function stringFromArray(NullArrayObject $array, string $key, ?string $default): ?string | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-06 18:23:32 +02:00
										 |  |  |         //app('log')->debug(sprintf('%s: %s', $key, var_export($array[$key], true)));
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (null === $array[$key] && null === $default) { | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2023-01-07 05:53:48 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (0 === $array[$key]) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('0' === $array[$key]) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $array[$key]) { | 
					
						
							|  |  |  |             return (string)$array[$key]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $default) { | 
					
						
							|  |  |  |             return $default; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string|null $string | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return Carbon|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function date(?string $string): ?Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (null === $string) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-06 18:23:32 +02:00
										 |  |  |         //        app('log')->debug(sprintf('Now in date("%s")', $string));
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (10 === strlen($string)) { | 
					
						
							|  |  |  |             return Carbon::createFromFormat('Y-m-d', $string, config('app.timezone')); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-12 18:21:29 +02:00
										 |  |  |         if (25 === strlen($string)) { | 
					
						
							|  |  |  |             return Carbon::parse($string, config('app.timezone')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (19 === strlen($string) && str_contains($string, 'T')) { | 
					
						
							|  |  |  |             return Carbon::createFromFormat('Y-m-d\TH:i:s', substr($string, 0, 19), config('app.timezone')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // 2022-01-01 01:01:01
 | 
					
						
							|  |  |  |         return Carbon::createFromFormat('Y-m-d H:i:s', substr($string, 0, 19), config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-25 14:23:52 +02:00
										 |  |  | } |