-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrmDataSet.inc
148 lines (126 loc) · 3.98 KB
/
OrmDataSet.inc
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
<?php
/**
* Copyright Skinit, Inc.
*/
class OrmDataSet implements Iterator, IFilterable
{
protected $relation = null;
protected $filters = array();
/**
* Set the primary relation for the data set.
*
* @param string $relationName
*/
public function __construct( $relationName )
{
$this->setPrimaryRelation( $relationName );
}
/**
* Return a data set for the specified relation. This data set will generate OrmModel instances for the tuples
* in the relation.
*
* @return OrmDataSet
*/
public static function get( $relationName )
{
return new OrmDataSet( $relationName );
}
/**
* Return the name of the relation for the data set. This is the relation for which models will be generated when
* iterating over the data set.
*
* @return string
*/
public function getRelation()
{
return $this->relation;
}
/**
* Set the relation for the data set. This is the relation for which models will be generated when iterating over
* the data set.
*
* $ormDataSet->setPrimaryRelation( "foo" );
* foreach( $ormDataSet as $m )
* assert( $m instanceof FooModel );
*
* @param string $relationName
*/
public function setRelation( $relationName )
{
// ensure we have a model for the relation; otherwise we don't know what to do with it
if( !class_exists( ucfirst($relationName) . "Model" ) )
{
throw new OrmSchemaException( "relation '$relationName' has no known model" );
}
// set the relation
$this->relation = $relationName;
}
/**
* Return the name of the model class used for generated instances when iterating.
*
* @return string
*/
public function getModelClass()
{
return ucfirst( Orm::formatLogicalName( $this->getRelation() ) ) . "Model";
}
/**
* Evaluate the data set. Once evaluated, no further modifications can be made to the data set. This includes
* adding additional filters or beginning a starting over with a new iteration. The data set may only be evaluated
* once. Trying to evaluate a second time results in an exception.
*/
public function evaluate()
{
// grab the appropriate data store by checking the model class
$modelClass = $this->getModelClass();
$dataStoreName = $modelClass::getDataStoreName();
Orm::getDataStore( $dataStoreName );
}
/**************************************************************************
* Iterator implementation
**************************************************************************/
public function current()
{
}
public function key()
{
// the iterator must currently be set to a valid result for the key to be valid
if( $this->valid() )
{
return $this->key;
}
}
public function next()
{
$this->key++;
}
public function rewind()
{
// rewind is called first during iteration, so we evaluate at this step
$this->evaluate();
$this->key = 0;
}
public function valid()
{
return false;
}
/**************************************************************************
* IFilterable implementation
**************************************************************************/
public function filter( $condition_1 )
{
$filter = Filter::createProxy( $this )->addTests( func_get_args() );
$this->filters[] = $filter;
return $filter;
}
public function filterOut( $condition_1 )
{
$filter = Filter::createProxy( $this )->addTests( func_get_args() )->invert();
$this->filters[] = $filter;
return $filter;
}
public function getFilters()
{
return $this->filters;
}
}