-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from nthmedia/feature/graphql-support
Add GraphQL support
- Loading branch information
Showing
10 changed files
with
162 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/assetbundles/entrycoordinatesfield/EntryCoordinatesFieldAsset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* Entry GPS Coordinates plugin for Craft CMS 3.x | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* Entry GPS Coordinates plugin for Craft CMS 3.x | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters