-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPackage.php
137 lines (119 loc) · 3.21 KB
/
Package.php
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
<?php
/**
* Package is an entity representing a LaTeX package with its parameters.
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Adam Kučera <adam.kucera@wrent.cz>
*/
/**
* Class representing a LaTeX package.
*/
class Package {
/**
* Name of the LaTeX package.
* @var string
*/
protected $name;
/**
* Array of the package parameters.
* @var array of strings
*/
protected $parameters;
/**
* Array of commands called after inserting the package.
* @var array of strings
*/
protected $commands;
/**
* Creates an Package object.
* @param string $name Name of the package
*/
public function __construct($name) {
$this->name = $name;
$this->parameters = array();
$this->commands = array();
}
/**
* Adds new parameter to the package and prevents duplicates.
* @param string $name Name of the parameter
*/
public function addParameter($name) {
if (!in_array($name, $this->parameters)) {
$this->parameters[] = $name;
}
}
/**
* Adds new command to the package and prevents duplicates.
* @param string $command Command.
*/
public function addCommand($command) {
if (!in_array($command, $this->commands)) {
$this->commands[] = $command;
}
}
/**
* Print this package ready to use
*
* @return string
*/
public function printUsePackage() {
$data = '\\usepackage';
$data .= $this->printParameters();
$data .= '{';
$data .= helper_plugin_latexit::escape($this->getName());
$data .= "}\n";
$data .= $this->printCommands();
return $data;
}
/**
* Prints all parameters, so they can be used in LaTeX \usepackage command
* @return String List of parameters in right format.
*/
public function printParameters() {
if(!$this->countParameters()) return '';
$parameters = $this->parameters;
$parameters = array_map(array('helper_plugin_latexit', 'escape'), $parameters);
$parameters = join(', ', $parameters);
return '['.$parameters.']';
}
/**
* Prints all commands, each on new line.
* @return String Text of commands.
*/
public function printCommands() {
if(!count($this->commands)) return '';
$commands = '';
foreach ($this->commands as $c) {
$commands .= $c."\n";
}
return $commands;
}
/**
* Returns the name of the package.
* @return string Name of the package.
*/
public function getName() {
return $this->name;
}
/**
* Check the number of parameters a command has
*
* @return bool
*/
protected function countParameters() {
return count($this->parameters);
}
/**
* Custom comparator to sort Packages
*
* @param Package $a
* @param Package $b
* @return int
*/
static function cmpPackages($a, $b) {
if($a->countParameters() == $b->countParameters()) {
return 0;
}
return ($a->countParameters() > $b->countParameters()) ? -1 : +1;
}
}