Add new fileToArray and arrayToFile methods to Module\FileTag

This commit is contained in:
Hypolite Petovan 2019-05-27 17:16:46 -04:00
parent 96402e306a
commit 6de3449cae
1 changed files with 61 additions and 38 deletions

View File

@ -64,19 +64,18 @@ class FileTag
} }
/** /**
* @brief Get file tags from list * Get file tags from array
* *
* ex. given music,video return <music><video> or [music][video] * ex. given [music,video] return <music><video> or [music][video]
* @param string $list A comma delimited list of tags. *
* @param array $array A list of tags.
* @param string $type Optional file type. * @param string $type Optional file type.
* *
* @return string A list of file tags. * @return string A list of file tags.
*/ */
public static function listToFile($list, $type = 'file') public static function arrayToFile(array $array, string $type = 'file')
{ {
$tag_list = ''; $tag_list = '';
if (strlen($list)) {
$list_array = explode(",", $list);
if ($type == 'file') { if ($type == 'file') {
$lbracket = '['; $lbracket = '[';
$rbracket = ']'; $rbracket = ']';
@ -85,16 +84,62 @@ class FileTag
$rbracket = '>'; $rbracket = '>';
} }
foreach ($list_array as $item) { foreach ($array as $item) {
if (strlen($item)) { if (strlen($item)) {
$tag_list .= $lbracket . self::encode(trim($item)) . $rbracket; $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
} }
} }
}
return $tag_list; return $tag_list;
} }
/**
* Get tag list from file tags
*
* ex. given <music><video>[friends], return [music,video] or [friends]
*
* @param string $file File tags
* @param string $type Optional file type.
*
* @return array List of tag names.
*/
public static function fileToArray(string $file, string $type = 'file')
{
$matches = [];
$return = [];
if ($type == 'file') {
$cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
} else {
$cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
}
if ($cnt) {
foreach ($matches as $match) {
$return[] = self::decode($match[1]);
}
}
return $return;
}
/**
* @brief Get file tags from list
*
* ex. given music,video return <music><video> or [music][video]
* @param string $list A comma delimited list of tags.
* @param string $type Optional file type.
*
* @return string A list of file tags.
* @deprecated since 2019.06 use arrayToFile() instead
*/
public static function listToFile($list, $type = 'file')
{
$list_array = explode(',', $list);
return self::arrayToFile($list_array, $type);
}
/** /**
* @brief Get list from file tags * @brief Get list from file tags
* *
@ -103,29 +148,11 @@ class FileTag
* @param string $type Optional file type. * @param string $type Optional file type.
* *
* @return string Comma delimited list of tag names. * @return string Comma delimited list of tag names.
* @deprecated since 2019.06 use fileToArray() instead
*/ */
public static function fileToList($file, $type = 'file') public static function fileToList($file, $type = 'file')
{ {
$matches = false; return implode(',', self::fileToArray($file, $type));
$list = '';
if ($type == 'file') {
$cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
} else {
$cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
}
if ($cnt) {
foreach ($matches as $mtch) {
if (strlen($list)) {
$list .= ',';
}
$list .= self::decode($mtch[1]);
}
}
return $list;
} }
/** /**
@ -164,21 +191,17 @@ class FileTag
// check for new tags to be added as filetags in pconfig // check for new tags to be added as filetags in pconfig
$new_tags = []; $new_tags = [];
$check_new_tags = explode(",", self::fileToList($file_new, $type)); foreach (self::fileToArray($file_new, $type) as $tag) {
foreach ($check_new_tags as $tag) {
if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) { if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
$new_tags[] = $tag; $new_tags[] = $tag;
} }
} }
$filetags_updated .= self::listToFile(implode(",", $new_tags), $type); $filetags_updated .= self::arrayToFile($new_tags, $type);
// check for deleted tags to be removed from filetags in pconfig // check for deleted tags to be removed from filetags in pconfig
$deleted_tags = []; $deleted_tags = [];
$check_deleted_tags = explode(",", self::fileToList($file_old, $type)); foreach (self::fileToArray($file_old, $type) as $tag) {
foreach ($check_deleted_tags as $tag) {
if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) { if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
$deleted_tags[] = $tag; $deleted_tags[] = $tag;
} }