]> git.mxchange.org Git - friendica.git/blob - src/Model/FileTag.php
Merge pull request #13172 from annando/parent-view
[friendica.git] / src / Model / FileTag.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 /**
25  * This class handles FileTag related functions
26  *
27  * post categories and "save to file" use the same item.file table for storage.
28  * We will differentiate the different uses by wrapping categories in angle brackets
29  * and save to file categories in square brackets.
30  * To do this we need to escape these characters if they appear in our tag.
31  */
32 class FileTag
33 {
34         /**
35          * URL encode <, >, left and right brackets
36          *
37          * @param string $s String to be URL encoded.
38          * @return string   The URL encoded string.
39          */
40         private static function encode(string $s): string
41         {
42                 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
43         }
44
45         /**
46          * URL decode <, >, left and right brackets
47          *
48          * @param string $s The URL encoded string to be decoded
49          * @return string   The decoded string.
50          */
51         private static function decode(string $s): string
52         {
53                 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
54         }
55
56         /**
57          * Get file tags from array
58          *
59          * ex. given [music,video] return <music><video> or [music][video]
60          *
61          * @param array  $array A list of tags.
62          * @param string $type  Optional file type.
63          * @return string       A list of file tags.
64          */
65         public static function arrayToFile(array $array, string $type = 'file'): string
66         {
67                 $tag_list = '';
68                 if ($type == 'file') {
69                         $lbracket = '[';
70                         $rbracket = ']';
71                 } else {
72                         $lbracket = '<';
73                         $rbracket = '>';
74                 }
75
76                 foreach ($array as $item) {
77                         if (strlen($item)) {
78                                 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
79                         }
80                 }
81
82                 return $tag_list;
83         }
84
85         /**
86          * Get tag list from file tags
87          *
88          * ex. given <music><video>[friends], return [music,video] or [friends]
89          *
90          * @param string $file File tags
91          * @param string $type Optional file type.
92          * @return array        List of tag names.
93          */
94         public static function fileToArray(string $file, string $type = 'file'): array
95         {
96                 $matches = [];
97                 $return = [];
98
99                 if ($type == 'file') {
100                         $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
101                 } else {
102                         $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
103                 }
104
105                 if ($cnt) {
106                         foreach ($matches as $match) {
107                                 $return[] = self::decode($match[1]);
108                         }
109                 }
110
111                 return $return;
112         }
113 }