-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPhpTabs.php
More file actions
150 lines (125 loc) · 3.41 KB
/
PhpTabs.php
File metadata and controls
150 lines (125 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
/*
* This file is part of the PhpTabs package.
*
* Copyright (c) landrok at github.com/landrok
*
* For the full copyright and license information, please see
* <https://github.com/stdtabs/phptabs/blob/master/LICENSE>.
*/
namespace PhpTabs;
use Exception;
use PhpTabs\Component\FileInput;
use PhpTabs\Component\Importer;
use PhpTabs\Component\InputStream;
use PhpTabs\Component\Reader;
use PhpTabs\Component\Tablature;
final class PhpTabs
{
/**
* @var \PhpTabs\Component\Tablature A tablature container
*/
private $tablature;
/**
* @param string $pathname A complete pathname
*/
public function __construct(?string $pathname = null)
{
// Create an emty tabs
if (is_null($pathname)) {
$this->setTablature(new Tablature());
// It's a pathname
} else {
$file = new FileInput($pathname);
$this->fromString(
$file->getInputStream()
->getStream($file->getInputStream()->getSize()),
$file->getExtension()
);
}
}
/**
* Instanciate from string
*/
public function fromString(string $string, ?string $extension = null): self
{
$reader = new Reader(
new InputStream($string),
$extension
);
return $this->setTablature($reader->getTablature());
}
/**
* Get the tablature instance
*/
public function getTablature(): Tablature
{
return $this->tablature;
}
/**
* Set the tablature instance
*/
public function setTablature(Tablature $tablature): self
{
$this->tablature = $tablature;
return $this;
}
/**
* Import a tablature from an array
*
* @param array $data A set of data that has been exported
*/
public function fromArray(array $data): self
{
$importer = new Importer($data);
$tabulature = new Tablature();
$tabulature->setSong($importer->getSong());
$this->setTablature($tabulature);
return $this;
}
/**
* Get PhpTabs version
*/
public function getVersion(): string
{
$filename = dirname(__DIR__, 2) . '/composer.json';
IOFactory::checkFile($filename);
$composer = json_decode(
file_get_contents($filename)
);
if (json_last_error() === JSON_ERROR_NONE) {
if (isset($composer->version) && is_string($composer->version)) {
return $composer->version;
}
}
return 'Undefined';
}
/**
* Overloads with $tablature methods
*
* @return mixed
*/
public function __call(string $name, array $arguments = [])
{
if (count($arguments) > 2) {
$message = sprintf(
'%s method does not support %d arguments',
$name,
count($arguments)
);
throw new Exception($message);
}
if (method_exists($this->tablature, $name)) {
return $this->tablature->$name(...$arguments);
}
if (method_exists($this->tablature->getSong(), $name)) {
return $this->tablature->getSong()->$name(...$arguments);
}
$message = sprintf(
'%s method does not exist',
$name
);
throw new Exception($message);
}
}