4 * Structural class representing a column ordering for a query.
6 * Queries often order results on multiple columns. For example, projects might
7 * be ordered by "name, id". This class wraps a list of column orderings and
8 * makes them easier to manage.
10 * To construct an order vector, use @{method:newFromVector}:
12 * $vector = PhabricatorQueryOrderVector::newFromVector(array('name', 'id'));
14 * You can iterate over an order vector normally:
16 * foreach ($vector as $item) {
20 * The items are objects of class @{class:PhabricatorQueryOrderItem}.
22 * This class is primarily internal to the query infrastructure, and most
23 * application code should not need to interact with it directly.
25 final class PhabricatorQueryOrderVector
33 private function __construct() {
37 public static function newFromVector($vector) {
38 if ($vector instanceof PhabricatorQueryOrderVector
) {
39 return (clone $vector);
42 if (!is_array($vector)) {
45 'An order vector can only be constructed from a list of strings or '.
46 'another order vector.'));
52 'An order vector must not be empty.'));
56 foreach ($vector as $key => $scalar) {
57 if (!is_string($scalar)) {
60 'Value with key "%s" in order vector is not a string (it has '.
61 'type "%s"). An order vector must contain only strings.',
66 $item = PhabricatorQueryOrderItem
::newFromScalar($scalar);
68 // Orderings like "id, id, id" or "id, -id" are meaningless and invalid.
69 if (isset($items[$item->getOrderKey()])) {
72 'Order vector "%s" specifies order "%s" twice. Each component '.
73 'of an ordering must be unique.',
74 implode(', ', $vector),
75 $item->getOrderKey()));
78 $items[$item->getOrderKey()] = $item;
81 $obj = new PhabricatorQueryOrderVector();
83 $obj->keys
= array_keys($items);
87 public function appendVector($vector) {
88 $vector = self
::newFromVector($vector);
90 // When combining vectors (like "group by" and "order by" vectors), there
91 // may be redundant columns. We only want to append unique columns which
92 // aren't already present in the vector.
93 foreach ($vector->items
as $key => $item) {
94 if (empty($this->items
[$key])) {
95 $this->items
[$key] = $item;
103 public function getAsString() {
105 foreach ($this->items
as $item) {
106 $scalars[] = $item->getAsScalar();
108 return implode(', ', $scalars);
111 public function containsKey($key) {
112 return isset($this->items
[$key]);
116 /* -( Iterator Interface )------------------------------------------------- */
119 public function rewind() {
124 public function current() {
125 return $this->items
[$this->key()];
129 public function key() {
130 return $this->keys
[$this->cursor
];
134 public function next() {
139 public function valid() {
140 return isset($this->keys
[$this->cursor
]);