OrderChain.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Konstantin V. Arkhipov                          *
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 
00015     final class OrderChain implements DialectString, MappableObject
00016     {
00017         private $chain = array();
00018         
00022         public static function create()
00023         {
00024             return new self;
00025         }
00026         
00030         public function add($order)
00031         {
00032             $this->chain[] = $this->makeOrder($order);
00033             
00034             return $this;
00035         }
00036         
00040         public function prepend($order)
00041         {
00042             if ($this->chain)
00043                 array_unshift($this->chain, $this->makeOrder($order));
00044             else
00045                 $this->chain[] = $this->makeOrder($order);
00046             
00047             return $this;
00048         }
00049         
00053         public function getLast()
00054         {
00055             return end($this->chain);
00056         }
00057         
00058         public function getList()
00059         {
00060             return $this->chain;
00061         }
00062         
00063         public function getCount()
00064         {
00065             return count($this->chain);
00066         }
00067         
00071         public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
00072         {
00073             $chain = new self;
00074             
00075             foreach ($this->chain as $order)
00076                 $chain->add($order->toMapped($dao, $query));
00077             
00078             return $chain;
00079         }
00080         
00081         public function toDialectString(Dialect $dialect)
00082         {
00083             if (!$this->chain)
00084                 return null;
00085             
00086             $out = null;
00087             
00088             foreach ($this->chain as $order)
00089                 $out .= $order->toDialectString($dialect).', ';
00090             
00091             return rtrim($out, ', ');
00092         }
00093         
00097         private function makeOrder($object)
00098         {
00099             if ($object instanceof OrderBy)
00100                 return $object;
00101             elseif ($object instanceof DialectString)
00102                 return new OrderBy($object);
00103             
00104             return
00105                 new OrderBy(
00106                     new DBField($object)
00107                 );
00108         }
00109     }
00110 ?>