From 3db47a1580ce675eb1baafded7bee48ae0627d07 Mon Sep 17 00:00:00 2001 From: Kath Young Date: Thu, 7 Sep 2023 14:39:44 +0930 Subject: [PATCH] Move date handling to connection --- src/Eloquent/Builder.php | 15 +++++ src/Eloquent/Connection.php | 107 +++++++++++++++++++++++++++++++++++- src/Eloquent/ZohoModel.php | 74 ------------------------- src/ZohoClient.php | 17 ++++++ 4 files changed, 136 insertions(+), 77 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 59312bf..93f2fa4 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -88,4 +88,19 @@ public function getConnection() { return $this->query->getConnection(); } + + /** + * Get the hydrated models without eager loading. + * + * @param array|string $columns + * @return \Illuminate\Database\Eloquent\Model[]|static[] + */ + public function getModels($columns = ['*']) + { + $data = $this->query->get($columns)->all(); + + return $this->model->hydrate( + $data + )->all(); + } } diff --git a/src/Eloquent/Connection.php b/src/Eloquent/Connection.php index a9f6a9f..0ba374c 100644 --- a/src/Eloquent/Connection.php +++ b/src/Eloquent/Connection.php @@ -9,6 +9,9 @@ use Portable\EloquentZoho\Exceptions\ConfigurationException; use Portable\EloquentZoho\ZohoClient; use Illuminate\Http\Client\Response; +use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; +use Portable\EloquentZoho\Casts\ZohoInteger; use Portable\EloquentZoho\Exceptions\NotConnectedException; use Portable\EloquentZoho\TokenStorage; @@ -19,6 +22,10 @@ class Connection extends DatabaseConnection */ protected ?ZohoClient $client = null; + protected Collection $views; + protected Collection $folders; + protected array $schemas; + protected string $folderName; public function __construct(protected array $zConfig) @@ -30,12 +37,17 @@ public function __construct(protected array $zConfig) } } + $this->views = collect(); + $this->folders = collect(); + $this->schemas = []; + $this->folderName = $zConfig['prefix']; $this->useDefaultPostProcessor(); $this->useDefaultQueryGrammar(); $this->useDefaultSchemaGrammar(); } + public function setAuthToken(?string $token): void { $this->getClient()->setAuthToken($token); @@ -94,11 +106,35 @@ public function hasTable(string $table): bool public function zohoSelect(string $fromTable, string $query): array { - $data = $this->getClient()->exportTable($fromTable, $query); + $tableSchema = $this->getSchema($fromTable); + $dateColumns = $tableSchema['columnList'] + ->where('dataTypeName', 'Date') + ->pluck('dateFormat', 'columnName') + ->toArray(); + $numberColumns = $tableSchema['columnList'] + ->whereIn('dataTypeName', ['Auto Number','Number']) + ->pluck('columnName')->toArray(); + + $response = $this->getClient()->exportTable($fromTable, $query)['response']; + $data = $response['result']['rows']; + $rows = []; - foreach ($data['response']['result']['rows'] as $row) { - $rows[] = array_combine($data['response']['result']['column_order'], $row); + foreach ($data as $row) { + $row = array_combine($response['result']['column_order'], $row); + foreach ($dateColumns as $dateColumn => $dateFormat) { + if (isset($row[$dateColumn]) && strlen($row[$dateColumn])) { + $row[$dateColumn] = Carbon::createFromFormat($dateFormat, $row[$dateColumn])->format('Y-m-d H:i:s'); + } + } + + foreach ($numberColumns as $numberColumn) { + if (isset($row[$numberColumn]) && strlen($row[$numberColumn])) { + $row[$numberColumn] = (int) Str::replace(',', '', $row[$numberColumn]); + } + } + + $rows[] = $row; } return $rows; @@ -218,4 +254,69 @@ protected function getClient(): ZohoClient return $this->client; } + + public function getFolderId() + { + return $this->getFolders()->where('folderName', $this->folderName)->first(); + } + + public function getFolders() + { + if (count($this->folders)==0) { + $response = $this->getClient()->getFolderList(); + if ($response->successful()) { + $this->folders = collect($response->json()['response']['result']); + } + } + return $this->folders; + } + + public function getViews() + { + if (count($this->views)==0) { + $response = $this->getClient()->getViewList(); + if ($response->successful()) { + $this->views = collect($response->json()['response']['result']); + } + } + return $this->views; + } + + public function getSchema($name) + { + if (isset($this->schemas[$name])) { + return $this->schemas[$name]; + } else { + $response = $this->getClient()->getViewInfo($name); + if ($response->successful()) { + $schema = $response->json()['response']['result']['viewInfo']; + foreach ($schema['columnList'] as $key => $item) { + if (isset($item['dateFormat'])) { + $item['dateFormat'] = $this->parseZohoDateFormat($item['dateFormat']); + } + $schema['columnList'][$key] = $item; + } + + $schema['columnList'] = collect($schema['columnList']); + + $this->schemas[$name] = $schema; + } else { + throw new \Exception("Unable to get schema"); + } + } + return $schema; + } + + protected function parseZohoDateFormat($format) + { + $format = str_replace('dd', 'd', $format); + $format = str_replace('MMM', 'M', $format); + $format = str_replace('yyyy', 'Y', $format); + + $format = str_replace('HH', 'H', $format); + $format = str_replace('mm', 'i', $format); + $format = str_replace('ss', 's', $format); + + return $format; + } } diff --git a/src/Eloquent/ZohoModel.php b/src/Eloquent/ZohoModel.php index 718c7f1..8f083fe 100644 --- a/src/Eloquent/ZohoModel.php +++ b/src/Eloquent/ZohoModel.php @@ -2,12 +2,7 @@ namespace Portable\EloquentZoho\Eloquent; -use Carbon\CarbonInterface; -use DateTimeInterface; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\Date; -use Portable\EloquentZoho\Casts\ZohoInteger; abstract class ZohoModel extends Model { @@ -19,12 +14,6 @@ abstract class ZohoModel extends Model abstract public static function arrayFromLocal(Model $local): array; - public function __construct(array $attributes = []) - { - parent::__construct($attributes); - $this->casts = array_merge($this->casts, ['id' => ZohoInteger::class]); - } - /** * Get the current connection name for the model. * @@ -34,67 +23,4 @@ public function getConnectionName() { return $this->connection ?? config('zoho.connection', 'zoho'); } - - /** - * Create a new model instance that is existing. - * - * @param array $attributes - * @param string|null $connection - * @return static - */ - public function newFromBuilder($attributes = [], $connection = null) - { - $model = $this->newInstance($attributes, true); - - $model->setConnection($connection ?: $this->getConnectionName()); - - $model->fireModelEvent('retrieved', false); - - return $model; - } - - /** - * Return a timestamp as DateTime object. - * Overriden because Zoho is weird - * - * @param mixed $value - * @return \Illuminate\Support\Carbon - */ - protected function asDateTime($value) - { - // If this value is already a Carbon instance, we shall just return it as is. - // This prevents us having to re-instantiate a Carbon instance when we know - // it already is one, which wouldn't be fulfilled by the DateTime check. - if ($value instanceof CarbonInterface) { - return Date::instance($value); - } - - // If the value is already a DateTime instance, we will just skip the rest of - // these checks since they will be a waste of time, and hinder performance - // when checking the field. We will just return the DateTime right away. - if ($value instanceof DateTimeInterface) { - return Date::parse( - $value->format('Y-m-d H:i:s.u'), - $value->getTimezone() - ); - } - - // If this value is an integer, we will assume it is a UNIX timestamp's value - // and format a Carbon object from this timestamp. This allows flexibility - // when defining your date fields as they might be UNIX timestamps here. - if (is_numeric($value)) { - return Date::createFromTimestamp($value); - } - - // If the value is in simply year, month, day format, we will instantiate the - // Carbon instances from that format. Again, this provides for simple date - // fields on the database, while still supporting Carbonized conversion. - if ($this->isStandardDateFormat($value)) { - return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->startOfDay()); - } - - $format = 'd M, Y H:i:s'; - - return Date::createFromFormat($format, $value); - } } diff --git a/src/ZohoClient.php b/src/ZohoClient.php index 759f7db..7f75cd3 100644 --- a/src/ZohoClient.php +++ b/src/ZohoClient.php @@ -29,6 +29,23 @@ public function __construct( ]); } + public function getFolderList(): Response + { + $response = $this->post('?ZOHO_ACTION=FOLDERLIST'); + return $response; + } + + public function getViewList(): Response + { + $response = $this->post('?ZOHO_ACTION=VIEWLIST'); + return $response; + } + + public function getViewInfo(string $table): Response + { + $response = $this->post($table . '?ZOHO_ACTION=VIEWMETADATA'); + return $response; + } /* * Determine if the service has enough configuration data to run correctly */