Completed
Push — php-7.1 ( 77242e...1bf6d8 )
by Pauli
14:40
created

Playlist::getTrackIdsAsArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @copyright Morris Jobke 2014
11
 */
12
13
namespace OCA\Music\Db;
14
15
use \OCP\AppFramework\Db\Entity;
16
17
/**
18
 * @method string getName()
19
 * @method setName(string $name)
20
 * @method string getTrackIds()
21
 * @method setTrackIds(string $trackIds)
22
 * @method string getUserId()
23
 * @method setUserId(string $userId)
24
 */
25
class Playlist extends Entity {
26
27
	public $name;
28
	public $userId;
29
	public $trackIds;
30
31
	/**
32
	 * @return int[]
33
	 */
34
	public function getTrackIdsAsArray() {
35
		if (!$this->trackIds || strlen($this->trackIds) < 3) {
36
			// the list is empty if there is nothing between the leading and trailing '|'
37
			return [];
38
		} else {
39
			$encoded = substr($this->trackIds, 1, -1); // omit leading and trailing '|'
40
			return array_map('intval', explode('|', $encoded));
41
		}
42
	}
43
44
	/**
45
	 * @param int[] $trackIds
46
	 */
47
	public function setTrackIdsFromArray($trackIds) {
48
		// encode to format like "|123|45|667|"
49
		$this->setTrackIds('|' . implode('|', $trackIds) . '|');
50
	}
51
52
	public function toAPI() {
53
		return array(
54
			'name' => $this->getName(),
55
			'trackIds' => $this->getTrackIdsAsArray(),
56
			'id' => $this->getId(),
57
		);
58
	}
59
60
}
61