From 8a4c5a6e6065384910d3128182c1df02ce815398 Mon Sep 17 00:00:00 2001 From: Niek ten Hoopen Date: Tue, 14 Jan 2020 13:51:48 +0100 Subject: [PATCH] Add GraphQL support --- CHANGELOG.md | 4 ++ composer.json | 2 +- src/EntryGpsCoordinates.php | 4 +- .../EntryCoordinatesFieldAsset.php | 1 + src/fields/EntryCoordinates.php | 37 +++++++++---- src/graphql/EntryCoordinatesFieldResolver.php | 18 +++++++ .../EntryCoordinatesFieldTypeGenerator.php | 51 ++++++++++++++++++ src/models/EntryCoordinatesModel.php | 53 +++++++++++++++++++ src/translations/en/entry-gps-coordinates.php | 1 + .../EntryGpsCoordinatesTwigExtension.php | 20 ++----- 10 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 src/graphql/EntryCoordinatesFieldResolver.php create mode 100644 src/graphql/EntryCoordinatesFieldTypeGenerator.php create mode 100644 src/models/EntryCoordinatesModel.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bf50c2..16a81d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.3.0 - 2020-01-14 +### Added +- GraphQL support + ## 1.2.1 - 2020-01-02 ### Changed diff --git a/composer.json b/composer.json index c7a39bc..08a6f8a 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nthmedia/entry-gps-coordinates", "description": "Entry GPS Coordinates plugin for Craft CMS 3.x", "type": "craft-plugin", - "version": "1.2.1", + "version": "1.3.0", "keywords": [ "gps coordinates", "gps", diff --git a/src/EntryGpsCoordinates.php b/src/EntryGpsCoordinates.php index 058e831..b85264e 100644 --- a/src/EntryGpsCoordinates.php +++ b/src/EntryGpsCoordinates.php @@ -1,4 +1,5 @@ $value]; + } - if ($value === null) + if ($value === null) { $value = []; + } - $value['coordinates'] = array_key_exists('coordinates', $value) ? $value['coordinates'] : ''; - $value['searchQuery'] = array_key_exists('searchQuery', $value) ? $value['searchQuery'] : ''; - - return $value; + return new EntryCoordinatesModel([ + 'coordinates' => array_key_exists('coordinates', $value) ? $value['coordinates'] : null, + 'searchQuery' => array_key_exists('searchQuery', $value) ? $value['searchQuery'] : null, + ]); } /** @@ -347,7 +353,9 @@ public function getSettingsHtml() */ public function getInputHtml($value, ElementInterface $element = null): string { - if (!$element) return ''; + if (!$element) { + return ''; + } // Register our asset bundle Craft::$app->getView()->registerAssetBundle(EntryCoordinatesFieldAsset::class); @@ -380,4 +388,15 @@ public function getInputHtml($value, ElementInterface $element = null): string ] ); } + + public function getContentGqlType() + { + $typeArray = EntryCoordinatesFieldTypeGenerator::generateTypes($this); + + return [ + 'name' => $this->handle, + 'description' => 'Entry Coordinates field', + 'type' => array_shift($typeArray), + ]; + } } diff --git a/src/graphql/EntryCoordinatesFieldResolver.php b/src/graphql/EntryCoordinatesFieldResolver.php new file mode 100644 index 0000000..f64d70a --- /dev/null +++ b/src/graphql/EntryCoordinatesFieldResolver.php @@ -0,0 +1,18 @@ +fieldName; + return $source->$fieldName; + } +} diff --git a/src/graphql/EntryCoordinatesFieldTypeGenerator.php b/src/graphql/EntryCoordinatesFieldTypeGenerator.php new file mode 100644 index 0000000..a487897 --- /dev/null +++ b/src/graphql/EntryCoordinatesFieldTypeGenerator.php @@ -0,0 +1,51 @@ + Type::string(), + 'coordinates' => Type::string(), + 'latitude' => Type::float(), + 'longitude' => Type::float(), + ]; + + $coordinatesProperty = GqlEntityRegistry::getEntity($typeName) + ?: GqlEntityRegistry::createEntity($typeName, new EntryCoordinatesFieldResolver([ + 'name' => $typeName, + 'description' => 'This entity has all the EntryCoordinates properties', + 'fields' => function () use ($props) { + return $props; + } + ])); + + TypeLoader::registerType($typeName, function () use ($coordinatesProperty) { + return $coordinatesProperty; + }); + + return [$coordinatesProperty]; + } + + /** + * @inheritdoc + */ + public static function getName($context = null): string + { + /** @var EntryCoordinates $context */ + return $context->handle . '_EntryCoordinateField'; + } +} diff --git a/src/models/EntryCoordinatesModel.php b/src/models/EntryCoordinatesModel.php new file mode 100644 index 0000000..295d95a --- /dev/null +++ b/src/models/EntryCoordinatesModel.php @@ -0,0 +1,53 @@ + $value) { + if (property_exists($this, $key)) { + $this[$key] = $value; + } + } + parent::__construct($config); + } + + /** + * @param null $value + * @return ?float + */ + public function getLatitude(): ?float + { + if ($this->coordinates !== null && preg_match(self::validPattern, $this->coordinates, $matches)) { + return floatval($matches[1]); + } + + return null; + } + + /** + * @param null $value + * @return ?float + */ + public function getLongitude(): ?float + { + if ($this->coordinates !== null && preg_match(self::validPattern, $this->coordinates, $matches)) { + return floatval($matches[5]); + } + + return null; + } +} diff --git a/src/translations/en/entry-gps-coordinates.php b/src/translations/en/entry-gps-coordinates.php index 89e4dea..cfe3be8 100644 --- a/src/translations/en/entry-gps-coordinates.php +++ b/src/translations/en/entry-gps-coordinates.php @@ -1,4 +1,5 @@ coordinates; } /** @@ -77,11 +73,7 @@ public function coordinates($value = null) */ public function latitude($value = null) { - if (array_key_exists('coordinates', $value) && preg_match('/^([-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)),\s*([-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?))$/', $value['coordinates'], $matches)) { - return $matches[1]; - } - - return $value; + return $value->latitude; } /** @@ -91,10 +83,6 @@ public function latitude($value = null) */ public function longitude($value = null) { - if (array_key_exists('coordinates', $value) && preg_match('/^([-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)),\s*([-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?))$/', $value['coordinates'], $matches)) { - return $matches[5]; - } - - return $value; + return $value->longitude; } }