PEAR2_Net_RouterOS
1.0.0b1
A MikroTik RouterOS client
|
00001 <?php 00002 00022 namespace PEAR2\Net\RouterOS; 00023 00033 class Response extends Message 00034 { 00035 00039 const TYPE_FINAL = '!done'; 00040 00044 const TYPE_DATA = '!re'; 00045 00049 const TYPE_ERROR = '!trap'; 00050 00055 const TYPE_FATAL = '!fatal'; 00056 00060 protected $unrecognizedWords = array(); 00061 00065 private $_type = null; 00066 00078 public function __construct(Communicator $com, $asStream = false) 00079 { 00080 if (!$com->getTransmitter()->isDataAwaiting()) { 00081 throw new SocketException( 00082 'No data awaiting. Receiving aborted.', 206 00083 ); 00084 } 00085 $this->setType($com->getNextWord()); 00086 if ($asStream) { 00087 for ( 00088 $word = $com->getNextWordAsStream(), fseek($word, 0, SEEK_END); 00089 ftell($word) !== 0; 00090 $word = $com->getNextWordAsStream(), fseek( 00091 $word, 0, SEEK_END 00092 ) 00093 ) { 00094 rewind($word); 00095 $ind = fread($word, 1); 00096 if ('=' === $ind || '.' === $ind) { 00097 $prefix = stream_get_line($word, null, '='); 00098 } 00099 if ('=' === $ind) { 00100 $value = fopen('php://temp', 'r+b'); 00101 $bytesCopied = ftell($word); 00102 while (!feof($word)) { 00103 $bytesCopied += stream_copy_to_stream( 00104 $word, $value, 0xFFFFF, $bytesCopied 00105 ); 00106 } 00107 rewind($value); 00108 $this->setArgument($prefix, $value); 00109 continue; 00110 } 00111 if ('.' === $ind && 'tag' === $prefix) { 00112 $this->setTag(stream_get_contents($word, -1, -1)); 00113 continue; 00114 } 00115 rewind($word); 00116 $this->unrecognizedWords[] = $word; 00117 } 00118 } else { 00119 for ($word = $com->getNextWord(); '' !== $word; 00120 $word = $com->getNextWord() 00121 ) { 00122 if (preg_match('/^=([^=]+)=(.*)$/sm', $word, $matches)) { 00123 $this->setArgument($matches[1], $matches[2]); 00124 } elseif (preg_match('/^\.tag=(.*)$/sm', $word, $matches)) { 00125 $this->setTag($matches[1]); 00126 } else { 00127 $this->unrecognizedWords[] = $word; 00128 } 00129 } 00130 } 00131 } 00132 00143 protected function setType($type) 00144 { 00145 switch ($type) { 00146 case self::TYPE_FINAL: 00147 case self::TYPE_DATA: 00148 case self::TYPE_ERROR: 00149 case self::TYPE_FATAL: 00150 $this->_type = $type; 00151 return $this; 00152 default: 00153 throw new UnexpectedValueException( 00154 'Unrecognized response type.', 207, null, $type 00155 ); 00156 } 00157 } 00158 00165 public function getType() 00166 { 00167 return $this->_type; 00168 } 00169 00175 public function getUnrecognizedWords() 00176 { 00177 return $this->unrecognizedWords; 00178 } 00179 00180 }