1: <?php
2:
3: namespace Charcoal\Source;
4:
5: use \InvalidArgumentException;
6:
7:
8: use \Charcoal\Source\OrderInterface;
9:
10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class Order implements OrderInterface
21: {
22: const MODE_ASC = 'asc';
23: const MODE_DESC = 'desc';
24: const MODE_RANDOM = 'rand';
25: const MODE_VALUES = 'values';
26: const MODE_CUSTOM = 'custom';
27:
28: 29: 30: 31: 32:
33: protected $property;
34:
35: 36: 37: 38: 39:
40: protected $mode;
41:
42: 43: 44: 45: 46:
47: protected $values;
48:
49: 50: 51: 52: 53:
54: protected $string;
55:
56: 57: 58: 59: 60:
61: protected $active = true;
62:
63: 64: 65: 66:
67: public function setData(array $data)
68: {
69: if (isset($data['property'])) {
70: $this->setProperty($data['property']);
71: }
72:
73: if (isset($data['mode'])) {
74: $this->setMode($data['mode']);
75: }
76:
77: if (isset($data['values'])) {
78: $this->setValues($data['values']);
79: }
80:
81: if (isset($data['string'])) {
82: $this->setString($data['string']);
83:
84: if (!isset($data['mode'])) {
85: $this->setMode(self::MODE_CUSTOM);
86: }
87: }
88:
89: if (isset($data['active'])) {
90: $this->setActive($data['active']);
91: }
92:
93: return $this;
94: }
95:
96: 97: 98: 99: 100:
101: public function setProperty($property)
102: {
103: if (!is_string($property)) {
104: throw new InvalidArgumentException(
105: 'Order Property must be a string.'
106: );
107: }
108: if ($property == '') {
109: throw new InvalidArgumentException(
110: 'Order Property can not be empty.'
111: );
112: }
113:
114: $this->property = $property;
115: return $this;
116: }
117:
118: 119: 120:
121: public function property()
122: {
123: return $this->property;
124: }
125:
126: 127: 128: 129: 130:
131: public function setMode($mode)
132: {
133: if (!is_string($mode)) {
134: throw new InvalidArgumentException(
135: 'Order Mode must be a string.'
136: );
137: }
138:
139: $mode = strtolower($mode);
140: if (!in_array($mode, $this->validModes())) {
141: throw new InvalidArgumentException(
142: sprintf('Invalid Order mode "%s".', $mode)
143: );
144: }
145: $this->mode = $mode;
146: return $this;
147: }
148:
149: 150: 151:
152: public function mode()
153: {
154: return $this->mode;
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
169: public function setValues($values)
170: {
171: if (is_string($values)) {
172: if ($values == '') {
173: throw new InvalidArgumentException(
174: 'String values can not be empty.'
175: );
176: }
177: $values = array_map('trim', explode(',', $values));
178: $this->values = $values;
179: } elseif (is_array($values)) {
180: if (empty($values)) {
181: throw new InvalidArgumentException(
182: 'Array values can not be empty.'
183: );
184: }
185: $this->values = $values;
186: } else {
187: throw new InvalidArgumentException(
188: 'Order Values must be an array, or a comma-delimited string.'
189: );
190: }
191: return $this;
192: }
193:
194: 195: 196:
197: public function values()
198: {
199: return $this->values;
200: }
201:
202: 203: 204: 205: 206:
207: public function setString($sql)
208: {
209: if (!is_string($sql)) {
210: throw new InvalidArgumentException(
211: 'Custom SQL clause should be a string.'
212: );
213: }
214:
215: $this->string = $sql;
216:
217: return $this;
218: }
219:
220: 221: 222:
223: public function string()
224: {
225: return $this->string;
226: }
227:
228: 229: 230: 231:
232: public function setActive($active)
233: {
234: $this->active = !!$active;
235: return $this;
236: }
237:
238: 239: 240:
241: public function active()
242: {
243: return $this->active;
244: }
245:
246: 247: 248: 249: 250:
251: protected function validModes()
252: {
253: $validModes = [
254: self::MODE_DESC,
255: self::MODE_ASC,
256: self::MODE_RANDOM,
257: self::MODE_VALUES,
258: self::MODE_CUSTOM
259: ];
260:
261: return $validModes;
262: }
263: }
264: