Skip to content

Commit bd919a2

Browse files
author
Vítězslav Dvořák
committed
Fix: getExternalID now supports array of Relation objects as external-ids
1 parent 6f5c24e commit bd919a2

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/AbraFlexi/RO.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,37 +2254,42 @@ public function getPrevRecordID($conditions = [])
22542254
}
22552255

22562256
/**
2257-
* Vrací hodnotu daného externího ID.
2257+
* Returns the value of the given external ID.
22582258
*
2259-
* @param string $want Namespace Selector. If empty,you obtain the first one.
2259+
* @param string $want Namespace selector. If empty, returns the first one.
22602260
*
2261-
* @return array|string one id or array if multiplete
2261+
* @return array|string One ID or array if multiple
22622262
*/
22632263
public function getExternalID($want = null)
22642264
{
22652265
$extid = null;
22662266
$ids = $this->getExternalIDs();
22672267

2268+
// If $ids is an array of Relation objects, extract their 'value' property
2269+
if (is_array($ids) && isset($ids[0]) && is_object($ids[0]) && property_exists($ids[0], 'value')) {
2270+
$values = array_map(function($relation) {
2271+
return $relation->value;
2272+
}, $ids);
2273+
} elseif (is_object($ids) && property_exists($ids, 'value')) {
2274+
$values = is_array($ids->value) ? $ids->value : [$ids->value];
2275+
} else {
2276+
$values = is_array($ids) ? $ids : [$ids];
2277+
}
2278+
22682279
if (null === $want) {
2269-
if (!empty($ids)) {
2270-
$extid = \is_object($ids) ? (\is_array($ids->value) ? current($ids->value) : $ids) : current($ids);
2280+
if (!empty($values)) {
2281+
$extid = current($values);
22712282
}
22722283
} else {
2273-
if (null !== $ids && \is_array($ids->value)) {
2274-
foreach ($ids->value as $id) {
2275-
if (strstr($id, 'ext:'.$want)) {
2276-
if (null === $extid) {
2277-
$extid = str_replace('ext:'.$want.':', '', $id);
2284+
foreach ($values as $id) {
2285+
if (strstr($id, 'ext:'.$want)) {
2286+
if (null === $extid) {
2287+
$extid = str_replace('ext:'.$want.':', '', $id);
2288+
} else {
2289+
if (is_array($extid)) {
2290+
$extid[] = str_replace('ext:'.$want.':', '', $id);
22782291
} else {
2279-
if (\is_array($extid)) {
2280-
$extid[] = str_replace('ext:'.$want.':', '', $id);
2281-
} else {
2282-
$extid = [$extid, str_replace(
2283-
'ext:'.$want.':',
2284-
'',
2285-
$id,
2286-
)];
2287-
}
2292+
$extid = [$extid, str_replace('ext:'.$want.':', '', $id)];
22882293
}
22892294
}
22902295
}

0 commit comments

Comments
 (0)