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

feat: accept (and convert if necessary) Collections and arrays #27

Merged
merged 1 commit into from
Jan 17, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Compress Changelog

## Unreleased
### Fixed
- Accept (and convert if necessary) `Illuminate\Support\Collection` and `array` in `craft.compress.zip` and `Compress::$plugin->compress->getArchiveModelForQuery()`

## 4.0.1 - 2022-06-17
### Added
- Added "Default Volume Subdirectory" settings to control where assets are stored.
Expand Down
20 changes: 9 additions & 11 deletions src/services/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use craft\models\Volume;
use Illuminate\Support\Collection;
use venveo\compress\Compress as Plugin;
use venveo\compress\errors\CompressException;
use venveo\compress\events\CompressEvent;
Expand Down Expand Up @@ -52,22 +53,19 @@ class Compress extends Component
*/
const EVENT_AFTER_CONFIGURE_ARCHIVE = 'EVENT_AFTER_CONFIGURE_ARCHIVE';

/**
* @param AssetQuery $query
* @param bool $lazy
* @param null $filename
* @return ArchiveModel|null
*/
public function getArchiveModelForQuery($query, $lazy = false, $filename = null): ?ArchiveModel
public function getArchiveModelForQuery(
AssetQuery|Collection|array $query,
bool $lazy = false,
null|string $filename = null,
): null|ArchiveModel
{
// Get the assets and create a unique hash to represent them
if ($query instanceof AssetQuery) {
$assets = $query->all();
} elseif ($query instanceof \ArrayAccess) {
$assets = $query;
} elseif ($query instanceof Collection) {
$assets = $query->toArray();
} else {
Craft::error('Unexpected input provided for asset query', __METHOD__);
return null;
$assets = $query;
}

$hash = $this->getHashForAssets($assets, $filename);
Expand Down