From 14d73346c564753b6cc82f8769c0f508990f549d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20Muller?= Date: Thu, 20 Jun 2019 12:37:00 +0200 Subject: [PATCH] :sparkles: Add richText caster method --- composer.json | 1 + .../Contentful/Helpers/Caster.php | 23 ++++++++++++++++ .../Contentful/ServiceProvider.php | 27 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/composer.json b/composer.json index 8ef251f..63e3949 100755 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "require": { "php": ">=7.1.3", "ext-json": "*", + "contentful/contentful": "^4.1", "erusev/parsedown": "^1.7", "guzzlehttp/guzzle": "^6.3", "illuminate/support": "~5.6|~5.7|~5.8", diff --git a/src/Distilleries/Contentful/Helpers/Caster.php b/src/Distilleries/Contentful/Helpers/Caster.php index ce4d8c0..93228fb 100755 --- a/src/Distilleries/Contentful/Helpers/Caster.php +++ b/src/Distilleries/Contentful/Helpers/Caster.php @@ -4,6 +4,7 @@ use Exception; use Illuminate\Support\Carbon; +use Contentful\RichText\Renderer; use Illuminate\Support\Collection; use Distilleries\Contentful\Models\Location; @@ -78,6 +79,28 @@ public static function fromJson($json): ?array return $data; } + /** + * Return rendered HTML rich-text data. + * + * @param mixed $object + * @return string|null + */ + public static function richText($object): ?string + { + $html = ''; + + if (! empty($object)) { + try { + $node = app('contentful.rich-text.parser')->parse($object); + $html = (new Renderer)->render($node); + } catch (Exception $e) { + dd($e->getMessage()); + } + } + + return $html; + } + /** * Transform markdown content to an HTML string. * diff --git a/src/Distilleries/Contentful/ServiceProvider.php b/src/Distilleries/Contentful/ServiceProvider.php index db419d3..146cce9 100644 --- a/src/Distilleries/Contentful/ServiceProvider.php +++ b/src/Distilleries/Contentful/ServiceProvider.php @@ -27,6 +27,7 @@ public function provides(): array 'command.contentful.sync-locales', 'command.contentful.import-clean', 'command.contentful.import-publish', + 'contentful.rich-text.parser' ]; } @@ -58,6 +59,8 @@ public function register() $this->app->bind(Api\SyncApi::class, Api\Sync\Api::class); $this->app->bind(Api\UploadApi::class, Api\Upload\Api::class); + $this->registerContentfulRelated(); + if ($this->app->runningInConsole()) { $this->registerCommands(); } @@ -108,4 +111,28 @@ private function registerCommands() $this->commands('command.contentful.import-clean'); $this->commands('command.contentful.import-publish'); } + + /** + * Bind utilities in IoC. + * + * @return void + */ + private function registerContentfulRelated() + { + $this->app->singleton('contentful.rich-text.parser', function () { + $spaceId = config('contentful.space_id'); + $environment = config('contentful.environment'); + + $client = new \Contentful\Delivery\Client(config('contentful.tokens.delivery.live'), $spaceId, $environment); + $linkResolver = new \Contentful\Delivery\LinkResolver( + $client, + new \Contentful\Delivery\ResourcePool\Extended( + $client, + new \Cache\Adapter\Void\VoidCachePool + ) + ); + + return new \Contentful\RichText\Parser($linkResolver); + }); + } }