OrderBy.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00016     final class OrderBy extends FieldTable implements MappableObject
00017     {
00018         private $direction  = null;
00019         private $nulls      = null;
00020         
00024         public static function create($field)
00025         {
00026             return new self($field);
00027         }
00028         
00029         public function __construct($field)
00030         {
00031             parent::__construct($field);
00032             
00033             $this->direction = new Ternary(null);
00034             $this->nulls = new Ternary(null);
00035         }
00036         
00037         public function __clone()
00038         {
00039             $this->direction = clone $this->direction;
00040             $this->nulls = clone $this->nulls;
00041         }
00042         
00046         public function setDirection($direction)
00047         {
00048             $this->direction->setValue($direction);
00049             return $this;
00050         }
00051         
00055         public function desc()
00056         {
00057             $this->direction->setFalse();
00058             return $this;
00059         }
00060         
00064         public function asc()
00065         {
00066             $this->direction->setTrue();
00067             return $this;
00068         }
00069         
00070         public function isAsc()
00071         {
00072             return $this->direction->decide(true, false, true);
00073         }
00074         
00078         public function nullsFirst()
00079         {
00080             $this->nulls->setTrue();
00081             return $this;
00082         }
00083         
00087         public function nullsLast()
00088         {
00089             $this->nulls->setFalse();
00090             return $this;
00091         }
00092         
00093         public function isNullsFirst()
00094         {
00095             return $this->nulls->decide(true, false, true);
00096         }
00097         
00101         public function setNullsFirst($nullsFirst)
00102         {
00103             $this->nulls->setValue($nullsFirst);
00104             return $this;
00105         }
00106         
00110         public function invert()
00111         {
00112             return
00113                 $this->isAsc()
00114                     ? $this->desc()
00115                     : $this->asc();
00116         }
00117         
00121         public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
00122         {
00123             $order = self::create($dao->guessAtom($this->field, $query));
00124             
00125             if (!$this->nulls->isNull())
00126                 $order->setNullsFirst($this->nulls->getValue());
00127             
00128             if (!$this->direction->isNull())
00129                 $order->setDirection($this->direction->getValue());
00130             
00131             return $order;
00132         }
00133         
00134         public function toDialectString(Dialect $dialect)
00135         {
00136             if (
00137                 $this->field instanceof SelectQuery
00138                 || $this->field instanceof LogicalObject
00139             )
00140                 $result = '('.$dialect->fieldToString($this->field).')';
00141             else
00142                 $result = parent::toDialectString($dialect);
00143             
00144             $result .=
00145                 $this->direction->decide(' ASC', ' DESC')
00146                 .$this->nulls->decide(' NULLS FIRST', ' NULLS LAST');
00147             
00148             return $result;
00149         }
00150         
00151         public function getFieldName()
00152         {
00153             if ($this->field instanceof DBField)
00154                 return $this->field->getField();
00155             else
00156                 return $this->field;
00157         }
00158     }
00159 ?>