Files
ezmigrationbundle/Core/Matcher/QueryBasedMatcher.php
T
2017-05-03 22:08:51 +01:00

115 lines
4.5 KiB
PHP

<?php
namespace Kaliop\eZMigrationBundle\Core\Matcher;
use eZ\Publish\API\Repository\Values\Content\Query;
/**
*/
abstract class QueryBasedMatcher extends RepositoryMatcher
{
const MATCH_CONTENT_ID = 'content_id';
const MATCH_LOCATION_ID = 'location_id';
const MATCH_CONTENT_REMOTE_ID = 'content_remote_id';
const MATCH_LOCATION_REMOTE_ID = 'location_remote_id';
const MATCH_PARENT_LOCATION_ID = 'parent_location_id';
const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id';
const MATCH_CONTENT_TYPE_ID = 'contenttype_id';
const MATCH_CONTENT_TYPE_IDENTIFIER = 'contenttype_identifier';
const MATCH_SECTION_ID = 'section_id';
//const MATCH_SECTION_IDENTIFIER = 'section_identifier';
const MATCH_VISIBILITY = 'visibility';
const MATCH_SUBTREE = 'subtree';
/**
* @param $key
* @param $values
* @return mixed should it be \eZ\Publish\API\Repository\Values\Content\Query\CriterionInterface ?
* @throws \Exception for unsupported keys
*/
protected function getQueryCriterion($key, $values)
{
if (!is_array($values)) {
$values = array($values);
}
switch ($key) {
case self::MATCH_CONTENT_ID:
return new Query\Criterion\ContentId($values);
case self::MATCH_LOCATION_ID:
return new Query\Criterion\LocationId($values);
case self::MATCH_CONTENT_REMOTE_ID:
return new Query\Criterion\RemoteId(reset($values));
case self::MATCH_LOCATION_REMOTE_ID:
return new Query\Criterion\LocationRemoteId($values);
case self::MATCH_PARENT_LOCATION_ID:
return new Query\Criterion\ParentLocationId($values);
case self::MATCH_PARENT_LOCATION_REMOTE_ID:
$locationIds = [];
foreach ($values as $remoteParentLocationId) {
$location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteParentLocationId);
// unique locations
$locationIds[$location->id] = $location->id;
}
return new Query\Criterion\ParentLocationId($locationIds);
case 'content_type_id':
case self::MATCH_CONTENT_TYPE_ID:
return new Query\Criterion\ContentTypeId($values);
case 'content_type_identifier':
case self::MATCH_CONTENT_TYPE_IDENTIFIER:
return new Query\Criterion\ContentTypeIdentifier($values);
case self::MATCH_SECTION_ID:
return new Query\Criterion\SectionId($values);
//case MATCH_SECTION_IDENTIFIER = 'section_identifier':
// return new Query\Criterion\SectionId();
case self::MATCH_SUBTREE:
return new Query\Criterion\Subtree($values);
case self::MATCH_VISIBILITY:
/// @todo error/warning if there is more than 1 value...
$value = reset($values);
if ($value) {
return new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE);
} else {
return new Query\Criterion\Visibility(Query\Criterion\Visibility::HIDDEN);
}
case self::MATCH_AND:
$subCriteria = array();
foreach($values as $subCriterion) {
$value = reset($subCriterion);
$subCriteria[] = $this->getQueryCriterion(key($subCriterion), $value);
}
return new Query\Criterion\LogicalAnd($subCriteria);
case self::MATCH_OR:
$subCriteria = array();
foreach($values as $subCriterion) {
$value = reset($subCriterion);
$subCriteria[] = $this->getQueryCriterion(key($subCriterion), $value);
}
return new Query\Criterion\LogicalOr($subCriteria);
case self::MATCH_NOT:
/// @todo throw if more than one sub-criteria found
$value = reset($values);
$subCriterion = $this->getQueryCriterion(key($values), $value);
return new Query\Criterion\LogicalNot($subCriterion);
default:
throw new \Exception($this->returns . " can not be matched because matching condition '$key' is not supported. Supported conditions are: " .
implode(', ', $this->allowedConditions));
}
}
}