Skip to content

Commit

Permalink
differentiate between empty and null values
Browse files Browse the repository at this point in the history
  • Loading branch information
i-just committed Mar 30, 2023
1 parent 318d63e commit a59fe4e
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 69 deletions.
18 changes: 13 additions & 5 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\AssetHelper;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;
use craft\helpers\Db;
use craft\helpers\Json;
Expand Down Expand Up @@ -62,6 +63,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$settings = Hash::get($this->field, 'settings');
$folders = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
Expand Down Expand Up @@ -99,10 +111,6 @@ public function parseField()
$urlsToUpload = [];
$base64ToUpload = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $key => $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -117,7 +125,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
20 changes: 14 additions & 6 deletions src/fields/CalendarEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Craft;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;
use Solspace\Calendar\Elements\Event as EventElement;

Expand Down Expand Up @@ -55,6 +56,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
Expand All @@ -66,7 +78,7 @@ public function parseField()

if (is_array($sources)) {
foreach ($sources as $source) {
list(, $uid) = explode(':', $source);
[, $uid] = explode(':', $source);
$typeIds[] = $uid;
}
} elseif ($sources === '*') {
Expand All @@ -75,10 +87,6 @@ public function parseField()

$foundElements = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -93,7 +101,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
20 changes: 14 additions & 6 deletions src/fields/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use craft\elements\Category as CategoryElement;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;
use craft\helpers\Db;

Expand Down Expand Up @@ -57,6 +58,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$source = Hash::get($this->field, 'settings.source');
$branchLimit = Hash::get($this->field, 'settings.branchLimit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
Expand All @@ -67,15 +79,11 @@ public function parseField()
$node = Hash::get($this->fieldInfo, 'node');

// Get source id's for connecting
list(, $groupUid) = explode(':', $source);
[, $groupUid] = explode(':', $source);
$groupId = Db::idByUid('{{%categorygroups}}', $groupUid);

$foundElements = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -90,7 +98,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/fields/Checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

if ($value === null) {
return null;
}

$preppedData = [];

$options = Hash::get($this->field, 'settings.options');
Expand Down
20 changes: 14 additions & 6 deletions src/fields/CommerceProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use craft\commerce\elements\Product as ProductElement;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;
use craft\helpers\Db;

Expand Down Expand Up @@ -56,6 +57,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
Expand All @@ -67,7 +79,7 @@ public function parseField()

if (is_array($sources)) {
foreach ($sources as $source) {
list(, $uid) = explode(':', $source);
[, $uid] = explode(':', $source);
$typeIds[] = Db::idByUid('{{%commerce_producttypes}}', $uid);
}
} elseif ($sources === '*') {
Expand All @@ -76,10 +88,6 @@ public function parseField()

$foundElements = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -94,7 +102,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
20 changes: 14 additions & 6 deletions src/fields/CommerceVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use craft\commerce\elements\Variant as VariantElement;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;

/**
Expand Down Expand Up @@ -55,6 +56,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
Expand All @@ -66,7 +78,7 @@ public function parseField()

if (is_array($sources)) {
foreach ($sources as $source) {
list(, $uid) = explode(':', $source);
[, $uid] = explode(':', $source);
$typeIds[] = $uid;
}
} elseif ($sources === '*') {
Expand All @@ -75,10 +87,6 @@ public function parseField()

$foundElements = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -93,7 +101,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/fields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function parseField()
{
$value = $this->fetchValue();

if ($value === null) {
return null;
}

$formatting = Hash::get($this->fieldInfo, 'options.match');

$dateValue = DateHelper::parseString($value, $formatting);
Expand Down
4 changes: 4 additions & 0 deletions src/fields/DefaultField.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function parseField()
{
$value = $this->fetchValue();

if ($value === null) {
return null;
}

// Default fields expect strings, if its an array for an odd reason, serialise it
if (is_array($value)) {
if (empty($value)) {
Expand Down
20 changes: 14 additions & 6 deletions src/fields/DigitalProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use craft\digitalproducts\elements\Product as ProductElement;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;

/**
Expand Down Expand Up @@ -57,6 +58,17 @@ public function parseField()
$value = $this->fetchArrayValue();
$default = $this->fetchDefaultArrayValue();

// if the mapped value is not set in the feed
if ($value === null) {
return null;
}

// if value from the feed is empty and default is not set
// return an empty array; no point bothering further
if (empty($default) && DataHelper::isArrayValueEmpty($value)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
Expand All @@ -68,7 +80,7 @@ public function parseField()

if (is_array($sources)) {
foreach ($sources as $source) {
list(, $uid) = explode(':', $source);
[, $uid] = explode(':', $source);
$typeIds[] = $uid;
}
} elseif ($sources === '*') {
Expand All @@ -77,10 +89,6 @@ public function parseField()

$foundElements = [];

if (!$value) {
return $foundElements;
}

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand All @@ -95,7 +103,7 @@ public function parseField()

// special provision for falling back on default BaseRelationField value
// https://github.com/craftcms/feed-me/issues/1195
if (empty($dataValue)) {
if (trim($dataValue) === '') {
$foundElements = $default;
break;
}
Expand Down
18 changes: 10 additions & 8 deletions src/fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function parseField()
{
$value = $this->fetchValue();

if ($value === null) {
return null;
}

$value = (string) $value;

$options = Hash::get($this->field, 'settings.options');
$match = Hash::get($this->fieldInfo, 'options.match', 'value');

Expand All @@ -56,14 +62,10 @@ public function parseField()
}
}

return null;
}
if (empty($value)) {
return $value;
}

/**
* @inheritDoc
*/
public function fetchValue()
{
return (string) parent::fetchValue();
return null;
}
}
Loading

0 comments on commit a59fe4e

Please sign in to comment.