mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 13:03:20 +00:00

- Squashed Pull request #1730 - Squashed Pull request #1741 - Squashed Pull request #1742 - Squash merge branch 4.0 - Squashed Pull request #1744 - Squashed Pull request #1746 - Squashed merge branch 4.0 - Squashed merge branch 4.0 - Squashed merge branch 4.0 - Squashed merge branch 4.0 - Squashed Pull request #1758 - Avoid using imagine/imagine alias as it is causing install issues - Squashed merge branch 4.0 - Squashed Pull request #1763 - Squashed merge branch 4.0 - Squash of 6 commits - Squashed merge branch 4.0 - This is a combination of 2 commits. - Squashed Pull request #1775 - Squashed Pull request #1777 - Squashed Pull request #1779 - Squashed Pull request #1780 - Squashed Pull request #1782 - Adds a Pull request template - Squased Pull request #1783 - Squash Pull request #1786 - Squashed Pull request #1796 - Squashed merge branch 4.0 - Squash Pull request #1791 - Squashed merge branch 4.0 - Squashed Pull request #1808 - Squashed Pull request #1811 - Squashed Pull request #1809
139 lines
3.1 KiB
PHP
139 lines
3.1 KiB
PHP
<?php
|
|
/*
|
|
* This file is part of Phraseanet
|
|
*
|
|
* (c) 2005-2016 Alchemy
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Alchemy\Phrasea\Databox\Subdef;
|
|
|
|
use Alchemy\Phrasea\Hydration\Hydrator;
|
|
use Assert\Assertion;
|
|
|
|
class MediaSubdefRepository
|
|
{
|
|
/**
|
|
* @var MediaSubdefDataRepository
|
|
*/
|
|
private $repository;
|
|
|
|
/**
|
|
* @var \media_subdef[]
|
|
*/
|
|
private $idMap = [];
|
|
|
|
/**
|
|
* @var callable
|
|
*/
|
|
private $subdefFactory;
|
|
|
|
/**
|
|
* @var Hydrator
|
|
*/
|
|
private $hydrator;
|
|
|
|
public function __construct(MediaSubdefDataRepository $repository, callable $subdefFactory, Hydrator $hydrator = null)
|
|
{
|
|
$this->repository = $repository;
|
|
$this->subdefFactory = $subdefFactory;
|
|
$this->hydrator = $hydrator ?: new MediaSubdefHydrator();
|
|
}
|
|
|
|
/**
|
|
* @param int $recordId
|
|
* @param string $name
|
|
* @return \media_subdef|null
|
|
*/
|
|
public function findOneByRecordIdAndName($recordId, $name)
|
|
{
|
|
$subdefs = $this->repository->findByRecordIdsAndNames([$recordId], [$name]);
|
|
|
|
if (!$subdefs) {
|
|
return null;
|
|
}
|
|
|
|
$instances = $this->hydrateAll($subdefs);
|
|
|
|
return reset($instances);
|
|
}
|
|
|
|
/**
|
|
* @param int[] $recordIds
|
|
* @param string[] $names
|
|
* @return \media_subdef[]
|
|
*/
|
|
public function findByRecordIdsAndNames(array $recordIds, array $names = null)
|
|
{
|
|
if (!$recordIds) {
|
|
return [];
|
|
}
|
|
|
|
$data = $this->repository->findByRecordIdsAndNames($recordIds, $names);
|
|
|
|
return $this->hydrateAll($data);
|
|
}
|
|
|
|
/**
|
|
* @param \media_subdef|\media_subdef[] $subdefs
|
|
*/
|
|
public function save($subdefs)
|
|
{
|
|
if (!is_array($subdefs) || !$subdefs instanceof \Traversable) {
|
|
$subdefs = [$subdefs];
|
|
} elseif ($subdefs instanceof \Traversable) {
|
|
$subdefs = iterator_to_array($subdefs);
|
|
}
|
|
Assertion::allIsInstanceOf($subdefs, \media_subdef::class);
|
|
|
|
$data = array_map([$this->hydrator, 'extract'], $subdefs);
|
|
|
|
$this->repository->save($data);
|
|
}
|
|
|
|
public function clear()
|
|
{
|
|
$this->idMap = [];
|
|
}
|
|
|
|
/**
|
|
* @param string $index
|
|
* @param array $data
|
|
* @return \media_subdef
|
|
*/
|
|
private function hydrate($index, array $data)
|
|
{
|
|
if (isset($this->idMap[$index])) {
|
|
$this->hydrator->hydrate($this->idMap[$index], $data);
|
|
|
|
return $this->idMap[$index];
|
|
}
|
|
|
|
$factory = $this->subdefFactory;
|
|
|
|
$instance = $factory($data);
|
|
Assertion::isInstanceOf($instance, \media_subdef::class);
|
|
|
|
$this->idMap[$index] = $instance;
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return \media_subdef[]
|
|
*/
|
|
private function hydrateAll(array $data)
|
|
{
|
|
$instances = [];
|
|
|
|
foreach ($data as $item) {
|
|
$instances[] = $this->hydrate(json_encode([$item['record_id'], $item['name']]), $item);
|
|
}
|
|
|
|
return $instances;
|
|
}
|
|
}
|