Skip to content

Commit

Permalink
refactor: code cleanup / PHPdoc / types / PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
michtio committed Apr 20, 2022
1 parent b95192a commit 8a31e7f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 87 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"name": "Colour Swatches",
"handle": "colour-swatches",
"description": "Adding custom selectable colour palettes, gradients, classes and more to your field types.",
"schemaVersion": "1.4.2",
"developer": "percipiolondon",
"developerUrl": "https://percipio.london",
"documentationUrl": "https://github.com/percipioglobal/craft-colour-swatches/blob/v4/README.md",
Expand Down
31 changes: 26 additions & 5 deletions src/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace percipiolondon\colourswatches;

use Craft;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
Expand All @@ -19,11 +20,14 @@
use yii\base\Event;

/**
* Class Colorswatches.
* Class ColourSwatches.
*
* @author Percipio Global Ltd.
*
* @since 1.0.0
* @property Settings $settings
*
* @method Settings getSettings()
*/
class ColourSwatches extends Plugin
{
Expand All @@ -33,15 +37,23 @@ class ColourSwatches extends Plugin
/**
* @var ColourSwatches
*/
public static $plugin;
public static ColourSwatches $plugin;

// Public Properties
// =========================================================================

/**
* @var string
*/
public string $schemaVersion = '1.4.2';

// Public Methods
// =========================================================================

/**
* {@inheritdoc}
* @inheritdoc
*/
public function init()
public function init(): void
{
parent::init();
self::$plugin = $this;
Expand All @@ -53,9 +65,18 @@ function(RegisterComponentTypesEvent $event) {
$event->types[] = ColourSwatchesField::class;
}
);

Craft::info(
Craft::t(
'colour-swatches',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
}

protected function createSettingsModel(): ?\craft\base\Model
protected function createSettingsModel(): Settings
{
return new Settings();
}
Expand Down
36 changes: 0 additions & 36 deletions src/elements/ColourSwatches.php

This file was deleted.

51 changes: 26 additions & 25 deletions src/fields/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\helpers\Json;
use percipiolondon\colourswatches\assetbundles\colourswatchesfield\ColourSwatchesFieldAsset;
use percipiolondon\colourswatches\ColourSwatches as Plugin;
use percipiolondon\colourswatches\models\ColourSwatches as ColourSwatchesModel;
Expand All @@ -24,6 +25,9 @@
* @author Percipio Global Ltd.
*
* @since 1.0.0
*
* @property-read string|array $contentColumnType
* @property-read null|string $settingsHtml
*/
class ColourSwatches extends Field implements PreviewableFieldInterface
{
Expand All @@ -35,22 +39,22 @@ class ColourSwatches extends Field implements PreviewableFieldInterface
*
* @var array
*/
public $options = [];
public array $options = [];

/** @var bool */
public $useConfigFile = false;
public bool $useConfigFile = false;

/** @var string */
public $palette = null;
/** @var string|null */
public ?string $palette = null;

/** @var int|string */
public $default = null;
/** @var int|string|null */
public string|int|null $default = null;

// Static Methods
// =========================================================================

/**
* {@inheritdoc}
* @inheritdoc
*/
public static function displayName(): string
{
Expand All @@ -61,7 +65,7 @@ public static function displayName(): string
// =========================================================================

/**
* {@inheritdoc}
* @inheritdoc
*/
public function rules(): array
{
Expand All @@ -72,39 +76,37 @@ public function rules(): array
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getContentColumnType()
public function getContentColumnType(): array|string
{
return Schema::TYPE_TEXT;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function normalizeValue($value, ?\craft\base\ElementInterface $element = null)
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed
{
if ($value instanceof ColourSwatchesModel) {
return $value;
}

// Check to see if this is already an array, which happens in some cases (Vizy)
if (is_array($value)) {
$value = json_encode($value);
$value = Json::encode($value);
}

// quick array transform so that we can ensure and `required fields` fire an error
$valueData = (array)json_decode($value);
// if we have actual data return model
if (count($valueData) > 0) {
return new ColourSwatchesModel($value);
}
$valueData = (array)Json::decode($value);

return new ColourSwatchesModel($value);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function serializeValue($value, ?\craft\base\ElementInterface $element = null)
public function serializeValue($value, ?ElementInterface $element = null): mixed
{
$settingsPalette = $this->options;
$saveValue = null;
Expand Down Expand Up @@ -145,7 +147,7 @@ public function serializeValue($value, ?\craft\base\ElementInterface $element =
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getSettingsHtml(): ?string
{
Expand Down Expand Up @@ -185,9 +187,9 @@ public function getSettingsHtml(): ?string
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getInputHtml($value, ?\craft\base\ElementInterface $element = null): string
public function getInputHtml($value, ?ElementInterface $element = null): string
{
// Register our asset bundle
Craft::$app->getView()
Expand Down Expand Up @@ -218,7 +220,7 @@ public function getInputHtml($value, ?\craft\base\ElementInterface $element = nu
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getTableAttributeHtml($value, ElementInterface $element): string
{
Expand Down Expand Up @@ -251,6 +253,5 @@ public function getTableAttributeHtml($value, ElementInterface $element): string
}
}
return '<div class="color small static"><div class="color-preview" style="' . $style . '"></div></div>';
// return print_r($color);
}
}
30 changes: 13 additions & 17 deletions src/models/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,43 @@
namespace percipiolondon\colourswatches\models;

use craft\base\Model;
use craft\helpers\Json;

class ColourSwatches extends Model
{
/**
* @var string
*/
public string $label = '';

public $label = '';
public $color = '';
/**
* @var string
*/
public string $color = '';

/**
* @inheritdoc
*/
public function __construct($value)
{
if ($this->validateJson($value)) {

// typecast our object to an array
$colorData = (array)json_decode($value);
$value = null;
$color = array_filter($colorData);
// if our array has usable data
$colorData = Json::decode($value);
if (!empty($colorData['label'])) {
$this->label = $colorData['label'];
$this->color = $colorData['color'];
}
// else ensure we return a null value (only really needed for old data stores)
else {
$value = null;
}
// else ensure we return a null value
} else {
$value = null;
}
}

// making sure we have json data, returns boolean(true) if this is the case
public function validateJson($value)
public function validateJson($value): bool
{
$json = json_decode($value);
$json = Json::decode($value);
return $json && $value != $json;
}

public function __toString()
public function __toString(): string
{
return $this->label;
}
Expand Down
26 changes: 23 additions & 3 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@

class Settings extends Model
{
public $colors = [];
public $palettes = [];
public $default = null;
/**
* @var array
*/
public array $colors = [];
/**
* @var array
*/
public array $palettes = [];
/**
* @var array|null
*/
public ?array $default = null;

/**
* @inheritdoc
*/
protected function defineRules(): array
{
return [
[['colors', 'palettes'], 'required'],
[['colors', 'palettes'], 'array'],
];
}
}

0 comments on commit 8a31e7f

Please sign in to comment.