1<?php
2
3/**
4 * Package is an entity representing a LaTeX package with its parameters.
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Adam Kučera <adam.kucera@wrent.cz>
8 */
9
10/**
11 * Class representing a LaTeX package.
12 */
13class Package {
14
15    /**
16     * Name of the LaTeX package.
17     * @var string
18     */
19    protected $name;
20    /**
21     * Array of the package parameters.
22     * @var array of strings
23     */
24    protected $parameters;
25    /**
26     * Array of commands called after inserting the package.
27     * @var array of strings
28     */
29    protected $commands;
30
31    /**
32     * Creates an Package object.
33     * @param string $name Name of the package
34     */
35
36    public function __construct($name) {
37        $this->name = $name;
38        $this->parameters = array();
39        $this->commands = array();
40    }
41
42    /**
43     * Adds new parameter to the package and prevents duplicates.
44     * @param string $name Name of the parameter
45     */
46
47    public function addParameter($name) {
48        if (!in_array($name, $this->parameters)) {
49            $this->parameters[] = $name;
50        }
51    }
52
53    /**
54     * Adds new command to the package and prevents duplicates.
55     * @param string $command Command.
56     */
57    public function addCommand($command) {
58        if (!in_array($command, $this->commands)) {
59            $this->commands[] = $command;
60        }
61    }
62
63    /**
64     * Print this package ready to use
65     *
66     * @return string
67     */
68    public function printUsePackage() {
69        $data  = '\\usepackage';
70        $data .= $this->printParameters();
71        $data .= '{';
72        $data .= helper_plugin_latexit::escape($this->getName());
73        $data .= "}\n";
74        $data .= $this->printCommands();
75
76        return $data;
77    }
78
79    /**
80     * Prints all parameters, so they can be used in LaTeX \usepackage command
81     * @return String List of parameters in right format.
82     */
83    public function printParameters() {
84        if(!$this->countParameters()) return '';
85
86        $parameters = $this->parameters;
87        $parameters = array_map(array('helper_plugin_latexit', 'escape'), $parameters);
88        $parameters = join(', ', $parameters);
89        return '['.$parameters.']';
90    }
91
92    /**
93     * Prints all commands, each on new line.
94     * @return String Text of commands.
95     */
96    public function printCommands() {
97        if(!count($this->commands)) return '';
98
99        $commands = '';
100        foreach ($this->commands as $c) {
101            $commands .= $c."\n";
102        }
103        return $commands;
104    }
105
106    /**
107     * Returns the name of the package.
108     * @return string Name of the package.
109     */
110    public function getName() {
111        return $this->name;
112    }
113
114    /**
115     * Check the number of parameters a command has
116     *
117     * @return bool
118     */
119    protected function countParameters() {
120        return count($this->parameters);
121    }
122
123    /**
124     * Custom comparator to sort Packages
125     *
126     * @param Package $a
127     * @param Package $b
128     * @return int
129     */
130    static function cmpPackages($a, $b) {
131        if($a->countParameters() == $b->countParameters()) {
132            return 0;
133        }
134        return ($a->countParameters() > $b->countParameters()) ? -1 : +1;
135    }
136
137}
138