Skip to content

Commit

Permalink
Merge pull request #7 from nthmedia/feature/graphql-support
Browse files Browse the repository at this point in the history
Add GraphQL support
  • Loading branch information
niektenhoopen authored Jan 14, 2020
2 parents d716772 + 8a4c5a6 commit 0ee4803
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions src/EntryGpsCoordinates.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
Expand All @@ -12,14 +13,12 @@

use nthmedia\entrygpscoordinates\fields\EntryCoordinates as EntryCoordinatesField;
use nthmedia\entrygpscoordinates\twigextensions\EntryGpsCoordinatesTwigExtension;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\services\Fields;
use craft\events\RegisterComponentTypesEvent;

use yii\base\Event;

/**
Expand Down Expand Up @@ -132,5 +131,4 @@ function (PluginEvent $event) {

// Protected Methods
// =========================================================================

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
Expand Down
37 changes: 28 additions & 9 deletions src/fields/EntryCoordinates.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
Expand All @@ -12,12 +13,14 @@

use nthmedia\entrygpscoordinates\EntryGpsCoordinates;
use nthmedia\entrygpscoordinates\assetbundles\entrycoordinatesfield\EntryCoordinatesFieldAsset;

use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use nthmedia\entrygpscoordinates\graphql\EntryCoordinatesFieldTypeGenerator;
use nthmedia\entrygpscoordinates\models\EntryCoordinatesModel;
use yii\db\Schema;
use craft\helpers\Json;
use GraphQL\Type\Definition\Type;

/**
* EntryCoordinates Field
Expand Down Expand Up @@ -112,19 +115,22 @@ public function getContentColumnType(): string
*/
public function normalizeValue($value, ElementInterface $element = null)
{
if (is_string($value))
if (is_string($value)) {
$value = Json::decodeIfJson($value);
}

if (is_string($value))
if (is_string($value)) {
$value = ['coordinates' => $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,
]);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
];
}
}
18 changes: 18 additions & 0 deletions src/graphql/EntryCoordinatesFieldResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace nthmedia\entrygpscoordinates\graphql;

use craft\gql\base\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;

class EntryCoordinatesFieldResolver extends ObjectType
{
/**
* @inheritdoc
*/
protected function resolve($source, $arguments, $context, ResolveInfo $resolveInfo)
{
$fieldName = $resolveInfo->fieldName;
return $source->$fieldName;
}
}
51 changes: 51 additions & 0 deletions src/graphql/EntryCoordinatesFieldTypeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace nthmedia\entrygpscoordinates\graphql;

use craft\gql\base\GeneratorInterface;
use craft\gql\GqlEntityRegistry;
use craft\gql\TypeLoader;
use GraphQL\Type\Definition\Type;
use nthmedia\entrygpscoordinates\fields\EntryCoordinates;

class EntryCoordinatesFieldTypeGenerator implements GeneratorInterface
{
/**
* @inheritdoc
*/
public static function generateTypes($context = null): array
{
/** @var EntryCoordinates $context */
$typeName = self::getName($context);
$props = [
'searchQuery' => 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';
}
}
53 changes: 53 additions & 0 deletions src/models/EntryCoordinatesModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace nthmedia\entrygpscoordinates\models;

use yii\base\Model as BaseModel;

class EntryCoordinatesModel extends BaseModel
{

protected const validPattern = '/^([-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)),\s*([-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?))$/';

/** @var ?string */
public $coordinates = null;

/** @var ?string */
public $searchQuery = null;

public function __construct($attributes = [], array $config = [])
{
foreach ($attributes as $key => $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;
}
}
1 change: 1 addition & 0 deletions src/translations/en/entry-gps-coordinates.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
Expand Down
20 changes: 4 additions & 16 deletions src/twigextensions/EntryGpsCoordinatesTwigExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
Expand All @@ -11,7 +12,6 @@
namespace nthmedia\entrygpscoordinates\twigextensions;

use nthmedia\entrygpscoordinates\EntryGpsCoordinates;

use Craft;

/**
Expand Down Expand Up @@ -63,11 +63,7 @@ public function getFunctions()
*/
public function coordinates($value = null)
{
if (array_key_exists('coordinates', $value)) {
return $value['coordinates'];
}

return $value;
return $value->coordinates;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit 0ee4803

Please sign in to comment.