Passed
Push — master ( dc1961...4e74cd )
by Pauli
02:00
created

Playlist::toAmpacheApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
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
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2014
12
 * @copyright Pauli Järvinen 2017 - 2021
13
 */
14
15
namespace OCA\Music\Db;
16
17
use \OCP\AppFramework\Db\Entity;
18
19
/**
20
 * @method string getName()
21
 * @method setName(string $name)
22
 * @method string getTrackIds()
23
 * @method setTrackIds(string $trackIds)
24
 * @method string getUserId()
25
 * @method setUserId(string $userId)
26
 * @method string getCreated()
27
 * @method setCreated(string $timestamp)
28
 * @method string getUpdated()
29
 * @method setUpdated(string $timestamp)
30
 * @method string getComment()
31
 * @method setComment(string $comment)
32
 */
33
class Playlist extends Entity {
34
	public $name;
35
	public $userId;
36
	public $trackIds;
37
	public $created;
38
	public $updated;
39
	public $comment;
40
41
	/**
42
	 * @return integer
43
	 */
44
	public function getTrackCount() {
45
		return \count($this->getTrackIdsAsArray());
46
	}
47
48
	/**
49
	 * @return int[]
50
	 */
51
	public function getTrackIdsAsArray() {
52
		if (!$this->trackIds || \strlen($this->trackIds) < 3) {
53
			// the list is empty if there is nothing between the leading and trailing '|'
54
			return [];
55
		} else {
56
			$encoded = \substr($this->trackIds, 1, -1); // omit leading and trailing '|'
57
			return \array_map('intval', \explode('|', $encoded));
58
		}
59
	}
60
61
	/**
62
	 * @param int[] $trackIds
63
	 */
64
	public function setTrackIdsFromArray($trackIds) {
65
		// encode to format like "|123|45|667|"
66
		$this->setTrackIds('|' . \implode('|', $trackIds) . '|');
67
	}
68
69
	public function toAPI() {
70
		return [
71
			'name' => $this->getName(),
72
			'trackIds' => $this->getTrackIdsAsArray(),
73
			'id' => $this->getId(),
74
			'created' => $this->getCreated(),
75
			'updated' => $this->getUpdated(),
76
			'comment' => $this->getComment()
77
		];
78
	}
79
80
	public function toAmpacheApi() {
81
		return [
82
			'id' => (string)$this->getId(),
83
			'name' => $this->getName(),
84
			'owner' => $this->getUserId(),
85
			'items' => $this->getTrackCount(),
86
			'type' => 'Private'
87
		];
88
	}
89
}
90