PEAR2_Net_RouterOS  1.0.0b1
A MikroTik RouterOS client
PEAR2/Net/RouterOS/Request.php
Go to the documentation of this file.
00001 <?php
00002 
00022 namespace PEAR2\Net\RouterOS;
00023 
00033 class Request extends Message
00034 {
00035 
00039     private $_command;
00040 
00044     private $_query = null;
00045 
00055     public function __construct($command)
00056     {
00057         $this->setCommand($command);
00058     }
00059 
00073     public function setCommand($command)
00074     {
00075         $command = (string) $command;
00076         if (strpos($command, '/') !== 0) {
00077             throw new InvalidArgumentException(
00078                 'Commands must be absolute.', 202
00079             );
00080         }
00081         if (substr_count($command, '/') === 1) {
00082             //Command line syntax convertion
00083             $cmdParts = preg_split('#[\s/]+#sm', $command);
00084             $cmdRes = array($cmdParts[0]);
00085             for ($i = 1, $n = count($cmdParts); $i < $n; $i++) {
00086                 if ('..' === $cmdParts[$i]) {
00087                     $delIndex = count($cmdRes) - 1;
00088                     if ($delIndex < 1) {
00089                         throw new InvalidArgumentException(
00090                             'Unable to resolve command', 203
00091                         );
00092                     }
00093                     unset($cmdRes[$delIndex]);
00094                     $cmdRes = array_values($cmdRes);
00095                 } else {
00096                     $cmdRes[] = $cmdParts[$i];
00097                 }
00098             }
00099             $command = implode('/', $cmdRes);
00100         }
00101         if (!preg_match('#^/\S+$#sm', $command)) {
00102             throw new InvalidArgumentException(
00103                 'Invalid command supplied.', 204
00104             );
00105         }
00106         $this->_command = $command;
00107         return $this;
00108     }
00109 
00118     public function getCommand()
00119     {
00120         return $this->_command;
00121     }
00122 
00132     public function setQuery(Query $query = null)
00133     {
00134         $this->_query = $query;
00135         return $this;
00136     }
00137 
00144     public function getQuery()
00145     {
00146         return $this->_query;
00147     }
00148 
00160     public function setTag($tag)
00161     {
00162         return parent::setTag($tag);
00163     }
00164 
00175     public function setArgument($name, $value = null)
00176     {
00177         return parent::setArgument($name, $value);
00178     }
00179 
00185     public function removeAllArguments()
00186     {
00187         return parent::removeAllArguments();
00188     }
00189 
00199     public function send(Communicator $com)
00200     {
00201         if (!$com->getTransmitter()->isAcceptingData()) {
00202             throw new SocketException(
00203                 'Transmitter is invalid. Sending aborted.', 205
00204             );
00205         }
00206         $bytes = 0;
00207         $bytes += $com->sendWord($this->getCommand());
00208         if (null !== ($tag = $this->getTag())) {
00209             $bytes += $com->sendWord('.tag=' . $tag);
00210         }
00211         foreach ($this->getAllArguments() as $name => $value) {
00212             $prefix = '=' . $name . '=';
00213             if (is_string($value)) {
00214                 $bytes += $com->sendWord($prefix . $value);
00215             } else {
00216                 $bytes += $com->sendWordFromStream($prefix, $value);
00217             }
00218         }
00219         $query = $this->getQuery();
00220         if ($query instanceof Query) {
00221             $bytes += $query->send($com);
00222         }
00223         $bytes += $com->sendWord('');
00224         return $bytes;
00225     }
00226 
00227 }