http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 16 LOC: 297 Statements: 43

Source file Statements Methods Total coverage
Meta.php 95.3% 100.0% 96.6%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://www.opensource.org/licenses/bsd-license.php
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to xyster@devweblog.org so we can send you a copy immediately.
14
 *
15
 * @category  Xyster
16
 * @package   Xyster_Orm
17
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
18
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
19
 * @version   $Id: Meta.php 69 2007-08-16 20:50:03Z doublecompile $
20
 */
21
/**
22
 * @see Xyster_Orm_Entity_Field
23
 */
24 1
require_once 'Xyster/Orm/Entity/Field.php';
25
/**
26
 * A helper for meta information about entities
27
 *
28
 * @category  Xyster
29
 * @package   Xyster_Orm
30
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
31
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
32
 */
33
class Xyster_Orm_Entity_Meta
34
{
35
    protected $_class;
36
    /**
37
     * The mapper factory
38
     *
39
     * @var Xyster_Orm_Mapper_Factory_Interface
40
     */
41
    protected $_mapFactory;
42
    protected $_fields = array();
43
    protected $_members = array();
44
    protected $_primary = array();
45
    protected $_relations = array();
46
47
    /**
48
     * Creates a new Entity Metadata helper
49
     *
50
     * @param Xyster_Orm_Mapper $map
51
     */
52
    public function __construct( Xyster_Orm_Mapper_Interface $map )
53
    {
54 214
        $this->_class = $map->getEntityName();
55 214
        $this->_mapFactory = $map->getFactory();
56
57 214
        foreach( $map->getFields() as $name => $values ) {
58 214
            $translated = $map->translateField($name);
59 214
            $field = new Xyster_Orm_Entity_Field($translated, $values);
60 214
            $this->_fields[$translated] = $field;
61 214
            if ( $field->isPrimary() ) {
62 214
                $this->_primary[] = $translated;
63 214
            }
64 214
        }
65
    }
66
67
    /**
68
     * Gets the class name of the entity
69
     *
70
     * @return string The class name
71
     */
72
    public function getEntityName()
73
    {
74 214
        return $this->_class;
75
    }
76
77
    /**
78
     * Gets the names of the fields for the entity
79
     *
80
     * @return array An array of field names
81
     */
82
    public function getFieldNames()
83
    {
84 113
        return array_keys($this->_fields);
85
    }
86
87
    /**
88
     * Gets the fields for the entity
89
     *
90
     * @return array An array of {@link Xyster_Orm_Entity_Field} objects
91
     */
92
    public function getFields()
93
    {
94 8
        return $this->_fields;
95
    }
96
97
    /**
98
     * Gets the mapper factory
99
     *
100
     * @return Xyster_Orm_Mapper_Factory_Interface
101
     */
102
    public function getMapperFactory()
103
    {
104 194
        return $this->_mapFactory;
105
    }
106
107
    /**
108
     * Gets all available class members (fields, relations, and methods)
109
     *
110
     * @return array
111
     */
112
    public function getMembers()
113
    {
114 33
        if ( !$this->_members ) {
115 33
            $this->_members = array_merge($this->_members, array_keys($this->_fields));
116 33
            $this->_members = array_merge($this->_members, $this->getRelationNames());
117 33
            $this->_members = array_merge($this->_members, get_class_methods($this->_class));
118 33
        }
119 33
        return $this->_members;
120
    }
121
122
    /**
123
     * Gets an array containing the field name or names for the primary key
124
     *
125
     * @return array  The field names
126
     */
127
    public function getPrimary()
128
    {
129 202
        return $this->_primary;
130
    }
131
132
    /**
133
     * Gets the relationship by name
134
     *
135
     * @param string $name The name of the relationship
136
     * @return Xyster_Orm_Relation
137
     * @throws Xyster_Orm_Relation_Exception if the relationship name is invalid
138
     */
139
    public function getRelation( $name )
140
    {
141 41
        if ( !$this->isRelation($name) ) {
142 1
            require_once 'Xyster/Orm/Relation/Exception.php';
143 1
            throw new Xyster_Orm_Relation_Exception("'" . $name
144 1
                . "' is not a valid relation for the '" . $this->_class . "' class");
145 0
        }
146
147 40
        return $this->_relations[$name];
148
    }
149
150
    /**
151
     * Gets the names of all relations defined for this entity
152
     *
153
     * @return array The names of defined relations
154
     */
155
    public function getRelationNames()
156
    {
157 34
        return array_keys($this->_relations);
158
    }
159
160
    /**
161
     * Gets the relations for the entity
162
     *
163
     * @return array An array of {@link Xyster_Orm_Relation} objects
164
     */
165
    public function getRelations()
166
    {
167 22
        return $this->_relations;
168
    }
169
170
    /**
171
	 * Creates a 'one to one' relationship for entities on the 'many' end of a 'one to many' relationship
172
	 *
173
	 * Options can contain the following values:
174
	 *
175
	 * <dl>
176
	 * <dt>class</dt><dd>The foreign class. The relation name by default</dd>
177
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the declaring
178
	 * entity.  This should either be an array (if multiple) or a string (if
179
	 * one).  By default, this is <var>class</var>Id</dd>
180
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
181
	 * against the entity to be loaded</dd>
182
	 * </dl>
183
	 *
184
	 * @param string $name  The name of the relationship
185
	 * @param array $options  An array of options
186
	 * @return Xyster_Orm_Relation  The relationship created
187
	 */
188
	public function belongsTo( $name, array $options = array() )
189
	{
190 194
		return $this->_baseCreate('belongs', $name, $options);
191
	}
192
	/**
193
	 * Creates a 'one to one' relationship
194
	 *
195
	 * Options can contain the following values:
196
	 *
197
	 * <dl>
198
	 * <dt>class</dt><dd>The foreign class. The relation name by default</dd>
199
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the declaring
200
	 * entity.  This should either be an array (if multiple) or a string (if
201
	 * one).  By default, this is <var>class</var>Id</dd>
202
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
203
	 * against the entity to be loaded</dd>
204
	 * </dl>
205
	 *
206
	 * @param string $name  The name of the relationship
207
	 * @param array $options  An array of options
208
	 * @return Xyster_Orm_Relation  The relationship created
209
	 */
210
	public function hasOne( $name, array $options = array() )
211
	{
212 1
		return $this->_baseCreate('one', $name, $options);
213
	}
214
	/**
215
	 * Creates a 'one to many' relationship
216
	 *
217
	 * Options can contain the following values:
218
	 *
219
	 * <dl>
220
	 * <dt>class</dt><dd>The foreign class.  The relation name minus a trailing
221
	 * 's' by default</dd>
222
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the related
223
	 * entity.  This should either be an array (if multiple) or a string (if
224
	 * one).  By default, this is <var>class</var>Id</dd>
225
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
226
	 * against the entities to be loaded</dd>
227
	 * </dl>
228
	 *
229
	 * @param string $name  The name of the relationship
230
	 * @param array $options  An array of options
231
	 * @return Xyster_Orm_Relation  The relationship created
232
	 */
233
	public function hasMany( $name, array $options = array() )
234
	{
235 29
		return $this->_baseCreate('many', $name, $options);
236
	}
237
	/**
238
	 * Creates a 'many to many' relationship
239
	 *
240
	 * <dl>
241
	 * <dt>class</dt><dd>The class of entity to load through the join table. It
242
	 * will be the relationship name minus a trailing 's' by default.</dd>
243
	 * <dt>table</dt><dd>The join table name.  By default this will be
244
	 * <var>declaring_class</var>_<var>class</var></dd>
245
	 * <dt>left</dt><dd>The column(s) in the join table referencing the
246
	 * declaringClass entity. By default: <var>declaring_class</var>_id</dd>
247
	 * <dt>right</dt><dd>The column(s) in the join table referencing the
248
	 * foreign entity.  By default, it's <var>class_name</var>_id</dd>
249
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
250
	 * against the join table.  Column names should be specified in the format
251
	 * native to the data store (i.e. with underscores, not camelCase)</dd>
252
	 * </dl>
253
	 *
254
	 * @param string $name  The name of the relationship
255
	 * @param array $options  An array of options
256
	 * @return Xyster_Orm_Relation  The relationship created
257
	 */
258
	public function hasJoined( $name, array $options = array() )
259
	{
260 194
		return $this->_baseCreate('joined', $name, $options);
261
	}
262
263
	/**
264
     * Whether the entity has a relationship with the name supplied
265
     *
266
     * @param string $name The name of the relationship
267
     * @return boolean
268
     */
269
	public function isRelation( $name )
270
	{
271 43
	    return array_key_exists($name, $this->_relations);
272
	}
273
274
    /**
275
     * Base creator method
276
     *
277
     * @param string $type The type of the relationship
278
     * @param string $name The name of the relationship
279
     * @param array $options An array of options
280
     * @return Xyster_Orm_Relation
281
     * @throws Xyster_Orm_Relation_Exception if the relationship is already defined
282
     */
283
    protected function _baseCreate( $type, $name, array $options )
284
    {
285 194
        $declaringClass = $this->_class;
286
287 194
        if ( array_key_exists($name, $this->_relations) ) {
288 1
            require_once 'Xyster/Orm/Relation/Exception.php';
289 1
            throw new Xyster_Orm_Relation_Exception("The relationship '" . $name . "' already exists");
290 0
        }
291
292 194
        require_once 'Xyster/Orm/Relation.php';
293 194
        $this->_relations[$name] = new Xyster_Orm_Relation($this, $type, $name, $options);
294
295 194
        return $this->_relations[$name];
296
    }
297
}


Report generated at 2007-10-08T19:32:23-05:00