1 : <?php
2 : namespace phMagick\Core;
3 :
4 : use phMagick\Core\Path;
5 :
6 : Class Command
7 : {
8 : private $source;
9 : private $destination;
10 : private $cmd = array();
11 : private $binaryBase = null;
12 :
13 : public function __construct($source = null, $destination = null)
14 : {
15 13 : $this->setSource($source);
16 13 : $this->setDestination($destination);
17 13 : }
18 :
19 : /*
20 : *
21 : * Getters and setters
22 : *
23 : */
24 :
25 : public function setBinaryBase(Path $path)
26 : {
27 1 : $this->binaryBase = $path;
28 1 : }
29 :
30 : public function getBinaryBase()
31 : {
32 2 : if (is_null($this->binaryBase)) {
33 2 : $this->binaryBase = new Path();
34 2 : }
35 :
36 2 : return $this->binaryBase;
37 : }
38 :
39 : public function setSource($source)
40 : {
41 13 : $this->source = $this->toPathObject($source);
42 13 : }
43 :
44 : public function getSource ()
45 : {
46 1 : return $this->source;
47 : }
48 :
49 : public function setDestination($path)
50 : {
51 13 : $this->destination = $this->toPathObject($path);
52 13 : }
53 :
54 : public function getDestination()
55 : {
56 1 : return $this->destination;
57 : }
58 :
59 : /*
60 : *
61 : * Logic
62 : *
63 : */
64 :
65 : /**
66 : *
67 : * Set's an imagemagick value
68 : *
69 : * @param string $parameter the parameter name
70 : * @param string $value the parameter value
71 : * param boolean $quotes the parameter value, if present will be surrounded by quotes
72 : */
73 : function set($parameter, $value = '', $quotes = TRUE)
74 : {
75 :
76 10 : if ( strlen($value) > 0 )
77 10 : {
78 8 : if ( TRUE == $quotes )
79 8 : {
80 6 : $value = '"' . $value . '"';
81 6 : }
82 :
83 8 : $value = $value;
84 8 : }
85 :
86 10 : $this->_appendToCmd($parameter , $value);
87 10 : return $this;
88 : }
89 :
90 : /**
91 : * adds a file name to the command list
92 : *
93 : * @param string $file, the filename
94 : */
95 : function file($file)
96 : {
97 1 : return $this->set('', $file, TRUE);
98 : }
99 :
100 : function binary ($name)
101 : {
102 1 : $binary = $this->getBinaryBase();
103 1 : $binary->add($name);
104 :
105 1 : return $this->option($name);
106 : }
107 : /**
108 : * Sets an Parameter
109 : *
110 : * @param string $parameter the parameter name
111 : * @param string $value the parameter value
112 : * @param boolean $quotes the parameter value, if present will be surrounded by quotes
113 : */
114 : function param($parameter, $value = '', $quotes = TRUE)
115 : {
116 5 : return $this->set($parameter, $value, $quotes);
117 : }
118 :
119 : /**
120 : * Set's an option (a parameter without a value) Alias for setParameter($option, '', false)
121 : *
122 : * @param string $option the parameter name
123 : */
124 : function option($option)
125 : {
126 3 : return $this->set($option, '', FALSE);
127 : }
128 :
129 : /**
130 : * returns the shell command, ready to be used in the command line
131 : */
132 : function toString()
133 : {
134 10 : return trim(implode(' ', $this->_cmd));
135 : }
136 :
137 : public function __toString()
138 : {
139 10 : return $this->toString();
140 : }
141 :
142 : /*
143 : *
144 : * Private methods
145 : *
146 : */
147 : protected function toPathObject($path)
148 : {
149 : // if (is_null($path)) {
150 : // return new Path();
151 : // }
152 13 : if ($path instanceof \phMagick\Core\Path) {
153 1 : return $path;
154 : }
155 :
156 13 : return new Path($path);
157 : }
158 :
159 :
160 : protected function _appendToCmd($cmd)
161 : {
162 10 : $args = func_get_args();
163 10 : foreach ( $args as $cmd)
164 : {
165 : // if( is_a($cmd, 'phMagick\Core\Command') )
166 : // {
167 : // $this->_cmd[] = $cmd-();
168 : // }
169 : // elseif (is_string ($cmd) && (strlen($cmd) > 0 ) )
170 : // {
171 10 : $this->_cmd[]= $cmd;
172 : // }
173 10 : }
174 10 : }
|