Skip to content

Commit

Permalink
Add attachment data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg committed Aug 12, 2024
1 parent 0e41930 commit b044fc7
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Data/AttachmentDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace DataKit\Plugin\Data;

use DataKit\DataViews\Data\CsvDataSource;
use DataKit\DataViews\Data\Exception\DataSourceNotFoundException;

/**
* Data source (factory) backed by a WordPress media attachment.
*
* @since $ver$
*/
final class AttachmentDataSource {
/**
* Disabled constructor.
*
* Use any of the named constructors.
*
* @since $ver$
*/
private function __construct() {
}

/**
* Returns a {@see CsvDataSource} for the specified attachment ID.
*
* @since $ver$
*
* @param int $attachment_id The attachment ID.
*
* @return CsvDataSource THe data source.
* @throws DataSourceNotFoundException When the attachment could not be found.
*/
public static function csv(
int $attachment_id,
string $separator = ',',
string $enclosure = '"',
string $escape = '\\'
): CsvDataSource {
$path = get_attached_file( $attachment_id );
if ( ! $path ) {
throw new DataSourceNotFoundException( sprintf( 'The attachment ID (%d) is not found.', $attachment_id ) );
}

return new CsvDataSource( $path, $separator, $enclosure, $escape );
}
}

0 comments on commit b044fc7

Please sign in to comment.