00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class OrderBy extends FieldTable implements MappableObject
00017 {
00018 private $direction = null;
00019
00023 public static function create($field)
00024 {
00025 return new self($field);
00026 }
00027
00028 public function __construct($field)
00029 {
00030 parent::__construct($field);
00031
00032 $this->direction = new Ternary(null);
00033 }
00034
00035 public function __clone()
00036 {
00037 $this->direction = clone $this->direction;
00038 }
00039
00043 public function desc()
00044 {
00045 $this->direction->setFalse();
00046 return $this;
00047 }
00048
00052 public function asc()
00053 {
00054 $this->direction->setTrue();
00055 return $this;
00056 }
00057
00058 public function isAsc()
00059 {
00060 return $this->direction->decide(true, false, true);
00061 }
00062
00066 public function invert()
00067 {
00068 return
00069 $this->isAsc()
00070 ? $this->desc()
00071 : $this->asc();
00072 }
00073
00077 public function toMapped(StorableDAO $dao, JoinCapableQuery $query)
00078 {
00079 $order = self::create($dao->guessAtom($this->field, $query));
00080
00081 if ($this->direction->isNull())
00082 return $order;
00083
00084 return $order->{$this->direction->decide('asc', 'desc')}();
00085 }
00086
00087 public function toDialectString(Dialect $dialect)
00088 {
00089 if (
00090 $this->field instanceof SelectQuery
00091 || $this->field instanceof LogicalObject
00092 )
00093 return
00094 '('.$dialect->fieldToString($this->field).')'
00095 .$this->direction->decide(' ASC', ' DESC');
00096 else
00097 return
00098 parent::toDialectString($dialect)
00099 .$this->direction->decide(' ASC', ' DESC');
00100 }
00101 }
00102 ?>