-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6aeb22
commit 8dc26fd
Showing
4 changed files
with
144 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codappix\ResponsiveImages\Service; | ||
|
||
use Codappix\ResponsiveImages\Configuration\ConfigurationManager; | ||
use Codappix\ResponsiveImages\Sizes\Breakpoint; | ||
use Codappix\ResponsiveImages\Sizes\Rootline; | ||
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData; | ||
|
||
final class ResponsiveImageService | ||
{ | ||
private array $files = []; | ||
|
||
private array $calculatedFiles = []; | ||
|
||
private array $contentElementSizes = []; | ||
|
||
public function __construct( | ||
private readonly ConfigurationManager $configurationManager | ||
) { | ||
} | ||
|
||
public function getCalculatedFiles( | ||
array $files, | ||
ContentBlockData|array $data, | ||
string $fieldName | ||
): array { | ||
$this->files = $files; | ||
|
||
if ( | ||
$data instanceof ContentBlockData | ||
) { | ||
assert(is_array($data->_raw)); | ||
$data = $data->_raw; | ||
} | ||
|
||
$this->contentElementSizes = (new Rootline($data, $fieldName))->getFinalSizes(); | ||
|
||
$this->calculateFileDimensions(); | ||
|
||
return $this->calculatedFiles; | ||
} | ||
|
||
private function calculateFileDimensions(): void | ||
{ | ||
foreach ($this->files as $file) { | ||
$calculatedFile = [ | ||
'media' => $file, | ||
'sizes' => $this->calculateFileDimensionForBreakpoints(), | ||
]; | ||
|
||
$this->calculatedFiles[] = $calculatedFile; | ||
} | ||
} | ||
|
||
private function calculateFileDimensionForBreakpoints(): array | ||
{ | ||
$fileDimensions = []; | ||
|
||
foreach ($this->getBreakpoints() as $breakpoint) { | ||
if (isset($this->contentElementSizes[$breakpoint->getIdentifier()]) === false) { | ||
continue; | ||
} | ||
|
||
$fileDimensions[$breakpoint->getIdentifier()] = [ | ||
'breakpoint' => $breakpoint, | ||
'size' => $this->contentElementSizes[$breakpoint->getIdentifier()], | ||
]; | ||
} | ||
|
||
return $fileDimensions; | ||
} | ||
|
||
/** | ||
* @return Breakpoint[] | ||
*/ | ||
private function getBreakpoints(): array | ||
{ | ||
$breakpoints = []; | ||
|
||
$breakpointsByPath = $this->configurationManager->getByPath('breakpoints'); | ||
|
||
if (is_iterable($breakpointsByPath)) { | ||
foreach ($breakpointsByPath as $breakpointIdentifier => $breakpointData) { | ||
$breakpoints[$breakpointIdentifier] = new Breakpoint($breakpointIdentifier, $breakpointData); | ||
} | ||
} | ||
|
||
return $breakpoints; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters