http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 16 LOC: 293 Statements: 43

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


Report generated at 2008-01-20T12:13:38-05:00