Skip to content

Commit

Permalink
Feature: Make CSV support optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Penny authored and chrispenny committed Jun 2, 2022
1 parent e284d8b commit 6e37f67
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"license": "BSD-3-Clause",
"require": {
"php": ">=7.4",
"php": "^7.4 || ^8.0",
"silverstripe/framework": "^4.7",
"symbiote/silverstripe-queuedjobs": "^4"
},
Expand Down
12 changes: 11 additions & 1 deletion src/Formatters/OaiDcFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,17 @@ protected function addMetadataElement(
return;
}

// CSV values are supported (and expected). Parse the field value as a CSV and create one element per value
// If this field has been marked as *not* supporting CSV, then we just add one node with the value as is
if (!OaiRecord::fieldSupportsCsv($property)) {
$element = $document->createElement($elementName);
$element->nodeValue = $oaiRecord->{$property};

$appendTo->appendChild($element);

return;
}

// If CSV values are marked as supported. Parse the field value as a CSV and create one element per value
foreach (str_getcsv($oaiRecord->{$property}) as $content) {
$element = $document->createElement($elementName);
$element->nodeValue = $content;
Expand Down
37 changes: 34 additions & 3 deletions src/Models/OaiRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
use Terraformers\OpenArchive\Models\Relationships\OaiRecordOaiSet;

/**
* All MANAGED_FIELDS support CSV format:
* CSV SUPPORT:
* All MANAGED_FIELDS support CSV format *by default*. Be sure to enclose any values that contain commas:
* Separator = ",", Enclosure = '"', Escape = "\\"
*
* Be sure to enclose any values that contain commas
* Why CSV? Because OAI spec does not restrict the number of fields that can be provided for any given record. EG, you
* can technically provide any number or Titles, or Descriptions, etc
*
* Why CSV? Because OAI spec does not restrict the number of fields that can be provided for any given record
* DISABLE CSV:
* You can disable CSV formats for any field you like by updating the `csv_fields` configuration (set the desired field
* to false rather than true to indicate that it is not CSV)
*
* Why disable CSV? Well, because it can be a bit of a pain to have to format all of your data for CSV support when/if
* you know you'll never need it
*
* @property string $Contributors
* @property string $Coverages
Expand Down Expand Up @@ -116,6 +123,23 @@ class OaiRecord extends DataObject

private static string $default_sort = 'ID ASC';

private static array $csv_fields = [
self::FIELD_CONTRIBUTORS => true,
self::FIELD_COVERAGES => true,
self::FIELD_CREATORS => true,
self::FIELD_DESCRIPTIONS => true,
self::FIELD_FORMATS => true,
self::FIELD_IDENTIFIER => true,
self::FIELD_LANGUAGES => true,
self::FIELD_PUBLISHERS => true,
self::FIELD_RELATIONS => true,
self::FIELD_RIGHTS => true,
self::FIELD_SOURCES => true,
self::FIELD_SUBJECTS => true,
self::FIELD_TITLES => true,
self::FIELD_TYPES => true,
];

public function addSet(string $title): void
{
$this->OaiSets()->add(OaiSet::findOrCreate($title));
Expand Down Expand Up @@ -143,4 +167,11 @@ protected function removeFilteredFromList(ManyManyThroughList $list, array $filt
}
}

public static function fieldSupportsCsv(string $fieldName): bool
{
$csvConfig = static::config()->get('csv_fields');

return $csvConfig[$fieldName] ?? false;
}

}
7 changes: 7 additions & 0 deletions tests/Models/OaiRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ public function testRemoveSet(): void
$this->assertCount(0, $record->OaiSets());
}

public function testFieldSupportsCsv(): void
{
foreach (OaiRecord::MANAGED_FIELDS as $fieldName) {
$this->assertTrue(OaiRecord::fieldSupportsCsv($fieldName));
}
}

}

0 comments on commit 6e37f67

Please sign in to comment.