1
0
mirror of synced 2024-11-30 09:16:06 +03:00
bitrix-module/intaro.retailcrm/lib/repository/filerepository.php
Сергей Чазов a4c06ed8ee
New ICML generator
2021-05-31 16:33:02 +03:00

68 lines
1.7 KiB
PHP

<?php
namespace Intaro\RetailCrm\Repository;
use CFile;
/**
* Class FileRepository
* @package Intaro\RetailCrm\Repository
*/
class FileRepository
{
/**
* @var string
*/
private $defaultServerName;
/**
* FileRepository constructor.
* @param string $defaultServerName
*/
public function __construct(string $defaultServerName)
{
$this->defaultServerName = $defaultServerName;
}
/**
* @param int|null $fileId
* @return string
*/
public function getImageUrl(?int $fileId): string
{
if (!$fileId) {
return '';
}
$pathImage = CFile::GetPath($fileId);
$validation = '/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i';
if ((bool) preg_match($validation, $pathImage) === false) {
return $this->defaultServerName . $pathImage;
}
return $pathImage;
}
/**
* @param array $product
* @param string $pictureProp
* @return string
*/
public function getProductPicture(array $product, string $pictureProp = ''): string
{
$picture = '';
$pictureId = $product['PROPERTY_' . $pictureProp . '_VALUE'] ?? null;
if (isset($product['DETAIL_PICTURE'])) {
$picture = $this->getImageUrl($product['DETAIL_PICTURE']);
} elseif (isset($product['PREVIEW_PICTURE'])) {
$picture = $this->getImageUrl($product['PREVIEW_PICTURE']);
} elseif ($pictureId !== null) {
$picture = $this->getImageUrl($pictureId);
}
return $picture ?? '';
}
}