-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for creating attributes with values of type eztags
- Loading branch information
gggeek
committed
Nov 14, 2016
1 parent
fa3295a
commit 4abae71
Showing
20 changed files
with
271 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\API\Collection; | ||
|
||
/** | ||
* @todo add phpdoc to suggest typehinting | ||
*/ | ||
class TagCollection extends AbstractCollection | ||
{ | ||
protected $allowedClass = 'Netgen\TagsBundle\API\Repository\Values\Tags\Tag'; | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\ComplexField; | ||
|
||
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface; | ||
use Kaliop\eZMigrationBundle\Core\Matcher\TagMatcher; | ||
|
||
class EzTags extends AbstractComplexField implements ComplexFieldInterface | ||
{ | ||
protected $tagMatcher; | ||
|
||
public function __construct(TagMatcher $tagMatcher) | ||
{ | ||
$this->tagMatcher = $tagMatcher; | ||
} | ||
|
||
/** | ||
* Creates a value object to use as the field value when setting an eztags field type. | ||
* | ||
* @param array $fieldValue | ||
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration | ||
* @return \Netgen\TagsBundle\Core\FieldType\Tags\Value | ||
* @throws \Exception | ||
*/ | ||
public function createValue($fieldValue, array $context = array()) | ||
{ | ||
$tags = array(); | ||
foreach($fieldValue as $def) | ||
{ | ||
if (!is_array($def) || count($def) != 1) { | ||
throw new \Exception('Definition of EzTags field is incorrect: each element of the tags array must be an array with one element'); | ||
} | ||
$identifier = reset($def); | ||
$type = key($def); | ||
|
||
|
||
foreach($this->tagMatcher->match(array($type => $identifier)) as $id => $tag) { | ||
$tags[$id] = $tag; | ||
} | ||
} | ||
|
||
return new \Netgen\TagsBundle\Core\FieldType\Tags\Value(array_values($tags)); | ||
} | ||
} |
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
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
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
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
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,19 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\Matcher; | ||
|
||
use eZ\Publish\API\Repository\Repository; | ||
|
||
abstract class RepositoryMatcher extends AbstractMatcher | ||
{ | ||
protected $repository; | ||
|
||
/** | ||
* @param Repository $repository | ||
* @todo inject the services needed, not the whole repository | ||
*/ | ||
public function __construct(Repository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
} |
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
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,145 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\Matcher; | ||
|
||
use Kaliop\eZMigrationBundle\API\Collection\TagCollection; | ||
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface; | ||
|
||
/** | ||
* @todo when matching by keyword, allow to pick the desired language | ||
*/ | ||
class TagMatcher extends AbstractMatcher implements KeyMatcherInterface | ||
{ | ||
use FlexibleKeyMatcherTrait; | ||
|
||
const MATCH_TAG_ID = 'tag_id'; | ||
const MATCH_TAG_REMOTE_ID = 'tag_remote_id'; | ||
const MATCH_TAG_KEYWORD = 'tag_keyword'; | ||
|
||
protected $allowedConditions = array( | ||
self::MATCH_TAG_ID, self::MATCH_TAG_REMOTE_ID, self::MATCH_TAG_KEYWORD, | ||
// aliases | ||
'id', 'remote_id', 'keyword' | ||
); | ||
protected $returns = 'Tag'; | ||
|
||
protected $translationHelper; | ||
protected $tagService; | ||
|
||
/** | ||
* @param $translationHelper | ||
* @param \Netgen\TagsBundle\API\Repository\TagsService $tagService | ||
*/ | ||
public function __construct($translationHelper, $tagService=null) | ||
{ | ||
$this->translationHelper = $translationHelper; | ||
$this->tagService = $tagService; | ||
} | ||
|
||
/** | ||
* @param array $conditions key: condition, value: int / string / int[] / string[] | ||
* @return TagCollection | ||
*/ | ||
public function match(array $conditions) | ||
{ | ||
return $this->matchTag($conditions); | ||
} | ||
|
||
/** | ||
* @param array $conditions key: condition, value: int / string / int[] / string[] | ||
* @return TagCollection | ||
* @throws \Exception | ||
*/ | ||
public function matchTag(array $conditions) | ||
{ | ||
if ($this->tagService == null) { | ||
throw new \Exception('Netgen TAG Bundle is required to use tag matching'); | ||
} | ||
|
||
$this->validateConditions($conditions); | ||
|
||
foreach ($conditions as $key => $values) { | ||
|
||
if (!is_array($values)) { | ||
$values = array($values); | ||
} | ||
|
||
switch ($key) { | ||
case 'id': | ||
case self::MATCH_TAG_ID: | ||
return new TagCollection($this->findTagsByIds($values)); | ||
|
||
case 'remote_id': | ||
case self::MATCH_TAG_REMOTE_ID: | ||
return new TagCollection($this->findTagsByRemoteIds($values)); | ||
|
||
case 'keyword': | ||
case self::MATCH_TAG_KEYWORD: | ||
return new TagCollection($this->findTagsByKeywords($values)); | ||
} | ||
} | ||
} | ||
|
||
protected function getConditionsFromKey($key) | ||
{ | ||
if (is_int($key) || ctype_digit($key)) { | ||
return array(self::MATCH_TAG_ID => $key); | ||
} | ||
|
||
throw new \Exception("Tag matcher can not uniquely identify the type of key used to match: ".key); | ||
} | ||
|
||
/** | ||
* @param int[] $tagIds | ||
* @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag[] | ||
*/ | ||
protected function findTagsByIds(array $tagIds) | ||
{ | ||
$tags = []; | ||
|
||
foreach ($tagIds as $tagId) { | ||
// return unique contents | ||
$tag = $this->tagService->loadTag($tagId); | ||
$tags[$tag->id] = $tag; | ||
} | ||
|
||
return $tags; | ||
} | ||
|
||
/** | ||
* @param string[] $tagRemoteIds | ||
* @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag[] | ||
*/ | ||
protected function findTagsByRemoteIds(array $tagRemoteIds) | ||
{ | ||
$tags = []; | ||
|
||
foreach ($tagRemoteIds as $tagRemoteId) { | ||
// return unique contents | ||
$tag = $this->tagService->loadTagByRemoteId($tagRemoteId); | ||
$tags[$tag->id] = $tag; | ||
} | ||
|
||
return $tags; | ||
} | ||
|
||
/** | ||
* @param string[] $tagKeywords | ||
* @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag[] | ||
*/ | ||
protected function findTagsByKeywords(array $tagKeywords) | ||
{ | ||
$tags = []; | ||
|
||
$availableLanguages = $this->translationHelper->getAvailableLanguages(); | ||
|
||
foreach ($tagKeywords as $tagKeyword) { | ||
// return unique contents | ||
foreach($this->tagService->loadTagsByKeyword($tagKeyword, $availableLanguages[0]) as $tag) { | ||
$tags[$tag->id] = $tag; | ||
} | ||
} | ||
|
||
return $tags; | ||
} | ||
} |
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
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
Oops, something went wrong.