Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match and create elements with title or slug "zero" #1339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/fields/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ public function parseField()
return null;
}

$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$specialMatchCase = in_array($match, ['title', 'slug']);

// 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 an empty array; no point bothering further;
// but we need to allow for zero as a string ("0") value if we're matching by title or slug
if (empty($default) && DataHelper::isArrayValueEmpty($value, $specialMatchCase)) {
return [];
}

$source = Hash::get($this->field, 'settings.source');
$branchLimit = Hash::get($this->field, 'settings.branchLimit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
$feedSiteId = Hash::get($this->feed, 'siteId');
$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$create = Hash::get($this->fieldInfo, 'options.create');
$fields = Hash::get($this->fieldInfo, 'fields');
$node = Hash::get($this->fieldInfo, 'node');
Expand All @@ -87,7 +90,8 @@ public function parseField()

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
// but sometimes allow for zeros
if (empty($dataValue) && empty($default) && ($specialMatchCase && !is_numeric($dataValue))) {
continue;
}

Expand Down
9 changes: 6 additions & 3 deletions src/fields/CommerceProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ public function parseField()
return null;
}

$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$specialMatchCase = in_array($match, ['title', 'slug']);

// 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)) {
if (empty($default) && DataHelper::isArrayValueEmpty($value, $specialMatchCase)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
$feedSiteId = Hash::get($this->feed, 'siteId');
$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$node = Hash::get($this->fieldInfo, 'node');

$typeIds = [];
Expand All @@ -90,7 +92,8 @@ public function parseField()

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
// but sometimes allow for zeros
if (empty($dataValue) && empty($default) && ($specialMatchCase && !is_numeric($dataValue))) {
continue;
}

Expand Down
9 changes: 6 additions & 3 deletions src/fields/CommerceVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ public function parseField()
return null;
}

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

// 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)) {
if (empty($default) && DataHelper::isArrayValueEmpty($value, $specialMatchCase)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
$feedSiteId = Hash::get($this->feed, 'siteId');
$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$node = Hash::get($this->fieldInfo, 'node');

$typeIds = [];
Expand All @@ -89,7 +91,8 @@ public function parseField()

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
// but sometimes allow for zeros
if (empty($dataValue) && empty($default) && ($specialMatchCase && !is_numeric($dataValue))) {
continue;
}

Expand Down
9 changes: 6 additions & 3 deletions src/fields/DigitalProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ public function parseField()
return null;
}

$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$specialMatchCase = in_array($match, ['title', 'slug']);

// 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)) {
if (empty($default) && DataHelper::isArrayValueEmpty($value, $specialMatchCase)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
$feedSiteId = Hash::get($this->feed, 'siteId');
$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$node = Hash::get($this->fieldInfo, 'node');

$typeIds = [];
Expand All @@ -91,7 +93,8 @@ public function parseField()

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
// but sometimes allow for zeros
if (empty($dataValue) && empty($default) && ($specialMatchCase && !is_numeric($dataValue))) {
continue;
}

Expand Down
12 changes: 8 additions & 4 deletions src/fields/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ public function parseField()
return null;
}

$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$specialMatchCase = in_array($match, ['title', 'slug']);

// 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 an empty array; no point bothering further;
// but we need to allow for zero as a string ("0") value if we're matching by title or slug
if (empty($default) && DataHelper::isArrayValueEmpty($value, $specialMatchCase)) {
return [];
}

$sources = Hash::get($this->field, 'settings.sources');
$limit = Hash::get($this->field, 'settings.limit');
$targetSiteId = Hash::get($this->field, 'settings.targetSiteId');
$feedSiteId = Hash::get($this->feed, 'siteId');
$match = Hash::get($this->fieldInfo, 'options.match', 'title');
$create = Hash::get($this->fieldInfo, 'options.create');
$fields = Hash::get($this->fieldInfo, 'fields');
$node = Hash::get($this->fieldInfo, 'node');
Expand Down Expand Up @@ -101,7 +104,8 @@ public function parseField()

foreach ($value as $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
// but sometimes allow for zeros
if (empty($dataValue) && empty($default) && ($specialMatchCase && !is_numeric($dataValue))) {
continue;
}

Expand Down
13 changes: 11 additions & 2 deletions src/helpers/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ class DataHelper
* Check if provided value is not set or empty or an array of empties
*
* @param $value
* @param $allowZero bool Whether to treat zero as an empty value or not
* @return bool
*/
public static function isArrayValueEmpty($value)
public static function isArrayValueEmpty($value, $allowZero = false): bool
{
return (!$value || (is_array($value) && empty(array_filter($value))));
return (!$value ||
(is_array($value) && empty(array_filter($value, function($item) use ($allowZero): bool {
if ($allowZero) {
return (!empty($item) || is_numeric($item));
}

return !empty($item);
})))
);
}

/**
Expand Down