/
Result.php
executable file
·583 lines (525 loc) · 12.7 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
/**
*
* @package Soup
* @subpackage Result
* @link https://github.com/canerdogan/Soup
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 1.0
* @version 1.0
* @author Can Erdogan <can@canerdogan.net>
*/
class Soup_Result extends Soup_Result_Abstract implements Countable, IteratorAggregate
{
/**
* Propertyler burada tutuluyor.
*
* @var array $_data
*/
protected $_data = array();
/**
* Save'de kullanılacak olan verilerin saklanacağı array
*
* @var array $_values
*/
protected $_values = array();
/**
* Bulunan toplam satır sayısı
*
* @var integer
*/
protected $_foundRows;
/**
* Tanımlanmış relationların saklanacağı depo
*
* @var array
*/
protected $_relations = array();
/**
* Gelen kayıt sayısı
*
* @var integer $_count
*/
protected $_count;
/**
* @var integer $_index this index is used for creating object identifiers
*/
private static $_index = 1;
/**
* @var integer $_oid object identifier, each Record object has a unique object identifier
*/
private $_oid;
/**
* @var Soup_Record $_parent
*/
private $_parent;
/**
* @var string
*/
private $_where;
/**
* the current instance counter used to generate unique ids for php objects. Contains the next identifier.
*
* @return integer
*/
public static function _index()
{
return self::$_index;
}
/**
* @see $_oid;
*
* @return integer the object identifier
*/
public function getOid()
{
return $this->_oid;
}
/**
* Uniq class id
*
* @return integer the object identifier
*/
public function oid()
{
return $this->_oid;
}
/**
* Construct
*/
public function __construct($table = NULL)
{
if (isset ($table) && $table instanceof Soup_Table) {
$this->_table = $table;
} else {
$class = Soup_Inflector::classify(Soup_Inflector::tableize($table));
if (class_exists($class)) {
$newClass = new $class ();
$this->_table = $newClass->getTable();
$exists = FALSE;
}
}
$this->_oid = self::$_index;
self::$_index++;
}
/**
* Parent class ayarlanıyor.
*
* @param Soup_Result $parent
*/
public function setParent(Soup_Result $parent)
{
$this->_parent = $parent;
}
/**
* Parent class geri döndürülüyor.
*
* @return Soup_Result $_parent
*/
public function getParent()
{
return $this->_parent;
}
/**
* Parent class var mı?
*
* @return boolean
*/
public function hasParent()
{
return isset($this->_parent);
}
/**
* @param string $cond
* @param string $args
*/
public function addWhere($cond, $rel)
{
$this->_where[$rel][] = $cond;
}
/**
* @return array
*/
public function getWhere($rel)
{
return $this->_where[$rel];
}
/**
* @param array $where
*/
public function setWhere($where, $rel)
{
$this->_where[$rel] = $where;
}
/**
* @param unknown_type $data
* @param Soup_Result $parent
*/
public function populate($data, $parent = NULL)
{
// print_r($data);
$this->_count = 0;
foreach ($data as $key => $value) {
if (is_array($value)) {
$this->_data [$key] = new Soup_Result ((is_numeric($key) ? $this->getTable() : $key));
$this->_data [$key]->setParent($this);
$this->_data [$key]->populate($value, $this);
$this->_count++;
} else {
if ($key == 'foundRows') {
$this->_foundRows = $value;
} else {
$this->_data [$key] = $value;
}
}
}
return $this;
}
/**
* Get
*
* @param string $fieldName
*
* @see Soup_Access::get()
*/
public function get($fieldName)
{
// print "\n Get: " . $fieldName . "\n";
if (!isset ($this->_data [$fieldName]) AND !array_key_exists($fieldName, $this->_data)) {
if ($this->getTable()->hasRelation($fieldName)) {
$foreignKey = $this->getTable()->getForeignKey($fieldName);
// print $fieldName . "\n";
// print_r($foreignKey);
// exit();
$newResult = new Soup_Result ($foreignKey['class']);
if (!$this->{$foreignKey['local']}) {
throw new Soup_Result_Exception('This relation field is not set.');
}
if ($foreignKey['type'] == Soup_Table::RELATION_ONE) {
$freshResult = $newResult->findOneBy($foreignKey['foreign'], $this->{$foreignKey['local']});
} else {
$freshResult = $newResult->findBy($foreignKey['foreign'], $this->{$foreignKey['local']});
}
$this->addWhere($foreignKey['foreign'] . ' = ?', $this->{$foreignKey['local']}, $foreignKey['alias']);
// print $fieldName;
// print $freshResult;
if (!$freshResult) {
// print 'pipi';
$className = Soup_Inflector::classify($fieldName);
$freshResult = new $className;
// $freshResult->set($foreignKey['foreign'], $this->{$foreignKey['local']});
// $freshResult->getTable()
// return $freshResult;
}
// throw new Soup_Result_Exception('This relation field is not set.');
$this->set($fieldName, $freshResult);
return end($this->_data);
} elseif ($this->getTable()->hasField($fieldName)) {
// return NULL;
} elseif (is_numeric($fieldName) OR is_null($fieldName)) {
// $this->add ( new Soup_Result ( $this->getTable () ) );
// return end ( $this->_data );
} else {
// Zend_Debug::dump($this->_data);
throw new Soup_Result_Exception ('Can\'t found field or relation such "' . $fieldName . '"');
}
// if (isset ( $fieldName )) {
// $this->set ( $fieldName, new Soup_Result () );
// } else {
// $this->add ( new Soup_Result () );
// }
// return end ( $this->_data );
} else {
return $this->_data [$fieldName];
}
}
/**
*
* @param string $condition
* @param string $arg
*
* @return Soup_Result
*/
public function where($condition, $arg = NULL)
{
if ($this->hasParent()) {
$foreignKey = $this->getTable()->getForeignKey($this->getParent()->getTable()->getName());
// return $where = $this->getParent()->getWhere();
if ($foreignKey['type'] == Soup_Table::RELATION_MANY) {
return $this;
} else {
$query = Soup_Query::select()
->from($this->getTable()->getTableName())
->where($condition, $arg);
// print_r($this->_where);
$where = $this->getWhere($this->getTable()->getName());
// print_r($where);
if (!is_null($where)) {
foreach ($where as $cond) {
$query->andWhere($cond, $this->getParent()->{$foreignKey['foreign']});
}
}
$freshResult = $query->execute();
}
} else {
$query = Soup_Query::select()
->from($this->getTable()->getTableName())
->where($condition, $arg);
$where = $this->getWhere($this->getTable()->getTableName());
if (!is_null($where)) {
foreach ($where as $cond) {
$query->andWhere($cond);
}
}
// print $query;
$freshResult = $query->execute();
}
// if(is_null($freshResult))
// $freshResult = new Soup_Result($this->getTable()->getTableName());
return $freshResult;
}
/**
*
* @param string $order
*
* @return Soup_Result
*/
public function orderBy($order)
{
if ($this->hasParent()) {
$foreignKey = $this->getTable()->getForeignKey($this->getParent()->getTable()->getName());
if ($foreignKey['type'] == Soup_Table::RELATION_MANY) {
return $this;
} else {
$where = $this->getParent()->getWhere($this->getTable()->getTableName());
return Soup_Query::select()
->from($this->getTable()->getTableName())
->where($where[0], $this->getParent()->{$foreignKey['foreign']})
->orderBy($order)
->execute();
}
} else {
$freshResult = Soup_Query::select()
->from($this->getTable()->getTableName())
->orderBy($order);
$where = $this->getWhere($this->getTable()->getTableName());
if (!is_null($where)) {
$i = 0;
foreach ($where as $cond) {
if ($i == 0) {
$freshResult->where($cond);
} else {
$freshResult->andWhere($cond);
}
$i++;
}
}
return $freshResult->execute();
}
}
/**
* @return Soup_Result
*/
public function last()
{
return end($this->_data);
}
/**
* @return Soup_Result
*/
public function first()
{
reset($this->_data);
return current($this->_data);
}
/**
* Set data
*
* @param integer $fieldName
* @param string $result
*
* @return void
* @see Soup_Access::set()
*/
public function set($fieldName, $result)
{
// print "\n Set: " . $fieldName . " : " . $result . "\n";
if (!is_object($result)) {
$this->_values [$fieldName] = $result;
} else {
$result->setParent($this);
$this->_relations [$fieldName] = $result;
}
// if (! $result instanceof Soup_Result || ! $result instanceof Soup_Record) {
// $this->_values [$fieldName] = $result;
// } else {
// $result->setParent ( $this );
// $this->_relations [$fieldName] = $result;
// }
$this->_data [$fieldName] = $result;
}
/**
* Adds a record to collection
*
* @param Soup_Record $record Record to be added
* @param string $key Optional key for the record
*
* @return boolean
*/
public function add($record, $key = NULL)
{
print $key . ' ' . $record;
foreach ($this->_data as $val) {
if ($val === $record) {
return FALSE;
}
}
if (isset ($key)) {
if (isset ($this->_data [$key])) {
return FALSE;
}
$this->_data [$key] = $record;
return TRUE;
}
$this->_data [] = $record;
return TRUE;
}
/**
* @see Soup_Access::remove()
*/
public function remove($key)
{
$removed = $this->_data [$key];
unset ($this->_data [$key]);
unset ($this->_values [$key]);
return $removed;
}
/**
* Whether or not this collection contains a specified element
*
* @param mixed $key the key of the element
*
* @return boolean
*/
public function contains($key)
{
return isset ($this->_data [$key]);
}
/**
* Save
*
* @todo preSave, afterSave eventleri eklenecek
* @todo validationlar eklenecek
* @return integer $primaryKey
*/
public function save()
{
// print_r($this->_data);
try {
$calledTable = Array();
foreach ($this->_data as $key => $value) {
// print gettype($value);
if ($value instanceof Soup_Result) {
$value->save();
} else if ($value instanceof Soup_Record) {
$foreignKey = $this->getTable()->getForeignKey($value->getTable()->getName());
$value->set($foreignKey['foreign'], $this->{$foreignKey['local']});
$value->save();
}
}
if (count($this->_values) > 0) {
Soup_Query::update($this->getTable()->getTableName(), $this->_values)->where(
$this->getTable()->getPrimaryKey() . " = ?", $this->_data[$this->getTable()->getPrimaryKey()]
) ->execute();
}
// Soup_Query::insert ( $this->getTable ()->getTableName (), $this->_values )->execute ();
// Soup_Query::update($this->getTable ()->getTableName (), $this->_values)->where(, )->execute();
return TRUE;
} catch (Soup_Query_Exception $e) {
throw new Soup_Record_Exception($e->getMessage(), $e->getCode());
}
}
/**
*
* @todo bu kısımda tek bir data varsa o silinecek birden fazla data tutuluyorsa içerisinde hepsi birden silinecek. Relationlar eklendikten sonra test edilecek.
*/
public function delete()
{
// if($this->count()>0)
// return 'Toplu siliş';
// else
// return 'Tekli Siliş';
return Soup_Query::delete($this->getTable()->getTableName())->where(
$this->getTable()->getPrimaryKey() . ' = ?', $this->_data[$this->getTable()->getPrimaryKey()]
) ->execute();
}
/**
* serialize
* this method is automatically called when an instance of Doctrine_Record is serialized
*
* @return string
* @todo Bu kısım hatalı tekrar yazılacak
*/
// public function serialize()
// {
// $str = serialize($this->_data);
//
// return $str;
// }
/**
* this method is automatically called everytime an instance is unserialized
*
* @param string $serialized Soup_Record as serialized string
*
* @return void
* @todo Bu kısım hatalı tekrar yazılacak
*/
// public function unserialize($serialized)
// {
// $this->_data = unserialize($serialized);
// }
//
/**
* @see Countable::count()
*/
public function count()
{
return $this->_count;
}
/**
* Toplam satır sayısını geri döndürür
*
* @return integer
*/
public function foundRows()
{
return $this->_foundRows;
}
/**
* @see IteratorAggregate::getIterator()
*/
public function getIterator()
{
return new ArrayIterator ($this->_data);
}
public function __toString()
{
return 'Soup_Result';
}
public function toArray()
{
if (is_array($this->_data)) {
foreach ($this->_data as $key => $data) {
if ($data instanceof Soup_Result) {
$result[] = $data->toArray();
} else {
$result[$key] = $data;
}
}
return $result;
} else {
return $this->_data;
}
}
}
?>