getAttributes() as $attribute) { if (! $attribute instanceof AttributeDTO) { continue; } $resolved[] = new AttributeDTO( $attribute->getName(), trim($attribute->getValue()), $attribute->getTaxonomy() ); } if ( $productDTO->getBarcode() !== null && $this->isIsbn($productDTO->getBarcode()->getValue()) && ! $this->hasAttribute($resolved, 'isbn') ) { $resolved[] = new AttributeDTO('isbn', $productDTO->getBarcode()->getValue()); } return $resolved; } private function hasAttribute(array $attributes, string $name): bool { foreach ($attributes as $attribute) { if ($attribute instanceof AttributeDTO && $attribute->getName() === $name) { return true; } } return false; } private function isIsbn(string $value): bool { $normalized = strtoupper((string) preg_replace('/[^0-9X]/i', '', $value)); return preg_match('/^[0-9]{9}[0-9X]$/', $normalized) === 1 || preg_match('/^97[89][0-9]{10}$/', $normalized) === 1; } }