Completed
Push — master ( 81df27...45026c )
by Andras
14s
created

Helper::toISO8601()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments;
13
14
use \DateTime;
15
use \DateTimeZone;
16
17
class Helper {
18
	const APP_ID = 'richdocuments';
19
20
	/**
21
	 * @param string $fileId
22
	 * @return array
23
	 * @throws \Exception
24
	 */
25
	public static function parseFileId($fileId) {
26
		$arr = explode('_', $fileId);
27
		if (count($arr) === 1) {
28
			$fileId = $arr[0];
29
			$instanceId = '';
30
			$version = '0';
31
		} else if (count($arr) === 2) {
32
			list($fileId, $instanceId) = $arr;
33
			$version = '0';
34
		} else if (count($arr) === 3) {
35
			list($fileId, $instanceId, $version) = $arr;
36
		} else {
37
			throw new \Exception('$fileId has not the expected format');
38
		}
39
40
		return [
41
			$fileId,
42
			$instanceId,
43
			$version,
44
		];
45
	}
46
47
	/**
48
	 * WOPI helper function to convert to ISO 8601 round-trip format.
49
	 * @param integer $time Must be seconds since unix epoch
50
	 */
51
	public static function toISO8601($time)
52
	{
53
		// TODO: Be more precise and don't ignore milli, micro seconds ?
54
		$datetime = DateTime::createFromFormat('U', $time, new DateTimeZone('UTC'));
55
		if ($datetime)
56
			return $datetime->format('Y-m-d\TH:i:s.u\Z');
57
58
		return false;
59
	}
60
61
	public static function getNewFileName($view, $path, $prepend = ' '){
62
		$fileNum = 1;
63
64
		while ($view->file_exists($path)){
65
			$fileNum += 1;
66
			$path = preg_replace('/(\.|' . $prepend . '\(\d+\)\.)([^.]*)$/', $prepend . '(' . $fileNum . ').$2', $path);
67
		};
68
69
		return $path;
70
	}
71
}
72