PEAR2_Net_RouterOS  1.0.0b1
A MikroTik RouterOS client
PEAR2/Net/RouterOS/Query.php
Go to the documentation of this file.
00001 <?php
00002 
00022 namespace PEAR2\Net\RouterOS;
00023 
00033 class Query
00034 {
00035 
00039     const ACTION_EXIST = '';
00040     
00044     const ACTION_NOT_EXIST = '-';
00045     
00049     const ACTION_EQUALS = '=';
00050     
00054     const ACTION_LESS_THAN = '<';
00055     
00059     const ACTION_GREATHER_THAN = '>';
00060 
00066     protected $words = array();
00067 
00072     private function __construct()
00073     {
00074         
00075     }
00076 
00084     protected static function sanitizeAction($action)
00085     {
00086         $action = (string) $action;
00087         switch ($action) {
00088         case Query::ACTION_EXIST:
00089         case Query::ACTION_NOT_EXIST:
00090         case Query::ACTION_EQUALS:
00091         case Query::ACTION_LESS_THAN:
00092         case Query::ACTION_GREATHER_THAN:
00093             return $action;
00094         default:
00095             throw new UnexpectedValueException(
00096                 'Unknown action specified', 208, null, $action
00097             );
00098         }
00099     }
00100 
00112     public static function where(
00113         $name, $value = null, $action = self::ACTION_EXIST
00114     ) {
00115         $query = new self;
00116         return $query->addWhere($name, $value, $action);
00117     }
00118 
00124     public function not()
00125     {
00126         $this->words[] = array('#!', null);
00127         return $this;
00128     }
00129 
00141     public function orWhere($name, $value = null, $action = self::ACTION_EXIST)
00142     {
00143         $this->addWhere($name, $value, $action)->words[] = array('#|', null);
00144         return $this;
00145     }
00146 
00158     public function andWhere($name, $value = null, $action = self::ACTION_EXIST)
00159     {
00160         $this->addWhere($name, $value, $action)->words[] = array('#&', null);
00161         return $this;
00162     }
00163 
00171     public function send(Communicator $com)
00172     {
00173         if (!$com->getTransmitter()->isAcceptingData()) {
00174             throw new SocketException(
00175                 'Transmitter is invalid. Sending aborted.', 209
00176             );
00177         }
00178         $bytes = 0;
00179         foreach ($this->words as $queryWord) {
00180             list($predicate, $value) = $queryWord;
00181             $prefix = '?' . $predicate;
00182             if (null === $value) {
00183                 $bytes += $com->sendWord($prefix);
00184             } else {
00185                 $prefix .= '=';
00186                 if (is_string($value)) {
00187                     $bytes += $com->sendWord($prefix . $value);
00188                 } else {
00189                     $bytes += $com->sendWordFromStream($prefix, $value);
00190                 }
00191             }
00192         }
00193         return $bytes;
00194     }
00195 
00207     protected function addWhere($name, $value, $action)
00208     {
00209         $this->words[] = array(
00210             self::sanitizeAction($action)
00211             . Message::sanitizeArgumentName($name),
00212             (null === $value ? null : Message::sanitizeArgumentValue($value))
00213         );
00214         return $this;
00215     }
00216 
00217 }