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 /* $Id: OrderBy.class.php 4687 2007-12-09 18:57:18Z voxus $ */
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 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4