-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_mapIndex.php
More file actions
140 lines (110 loc) · 3.52 KB
/
_mapIndex.php
File metadata and controls
140 lines (110 loc) · 3.52 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/orm/blob/master/LICENSE
*/
namespace Quid\Orm;
use Quid\Main;
// _mapIndex
// trait that grants common methods for indexed collections (cols, cells, rows)
trait _mapIndex
{
// trait
use Main\Map\_sequential;
// isTable
// retourne vrai si la collection contient au moins un élément de cette table
final public function isTable($value):bool
{
return $this->some(fn($row) => (is_object($value) && $value === $row->table()) || (is_string($value) && $value === $row->table()->name()));
}
// sameTable
// retourne vrai si toutes les entrées dans l'objet ont la même table
final public function sameTable():bool
{
$return = false;
$table = $this->table();
if(!empty($table))
$return = $this->every(fn($row) => $row->sameTable($table));
return $return;
}
// table
// retourne la table du premier objet
final public function table():?Table
{
$return = null;
$first = $this->first();
if(!empty($first))
$return = $first->table();
return $return;
}
// add
// ajoute une ou plusieurs objects dans la collection
final public function add(...$values):self
{
$this->checkAllowed('add');
$class = $this->mapIs;
$values = $this->prepareValues(...$values);
$data =& $this->arr();
foreach ($values as $value)
{
if(!is_a($value,$class,true))
static::throw('requires',$class);
if(in_array($value,$data,true))
static::throw('alreadyIn');
$data[] = $value;
}
return $this;
}
// filterByTable
// retourne un objet collections avec toutes les entrées étant dans la table fourni en argument
// l'objet retourné est dans la bonne classe de collection pour la table
final public function filterByTable($table):?Map
{
$return = null;
if(is_string($table))
{
$first = $this->table();
if(!empty($first))
{
$db = $first->db();
if($db->hasTable($table))
$table = $db->table($table);
}
}
if($table instanceof Table)
{
$type = static::$collectionType;
$classe = $table->classe()->$type() ?: static::throw('noClass');
$return = new $classe();
foreach ($this->arr() as $value)
{
if($value->sameTable($table))
$return->add($value);
}
}
return $return;
}
// groupByTable
// retourne un tableau multidimensionnel avec toutes les entrées séparés par le nom de table
// les objets retournés retournés sont dans les bonnes classes pour les tables
final public function groupByTable():array
{
$return = [];
foreach ($this->arr() as $value)
{
$table = $value->table();
$tableName = $table->name();
if(!array_key_exists($tableName,$return))
{
$type = static::$collectionType;
$classe = $table->classe()->$type() ?: static::throw('noClass');
$return[$tableName] = new $classe();
}
$return[$tableName]->add($value);
}
return $return;
}
}
?>