http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 16 LOC: 298 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 123 2007-10-16 22:00:32Z 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 221
        $this->_class = $map->getEntityName();
55 221
        $this->_mapFactory = $map->getFactory();
56
57 221
        foreach( $map->getFields() as $name => $values ) {
58 221
            $translated = $map->translateField($name);
59 221
            $field = new Xyster_Orm_Entity_Field($translated, $values);
60 221
            $this->_fields[$translated] = $field;
61 221
            if ( $field->isPrimary() ) {
62 221
                $this->_primary[] = $translated;
63 221
            }
64 221
        }
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 221
        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 117
        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 201
        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 209
        return array_values($this->_primary);
130
        // so current() can be called without reset()ing the array
131
    }
132
133
    /**
134
     * Gets the relationship by name
135
     *
136
     * @param string $name The name of the relationship
137
     * @return Xyster_Orm_Relation
138
     * @throws Xyster_Orm_Relation_Exception if the relationship name is invalid
139
     */
140
    public function getRelation( $name )
141
    {
142 57
        if ( !$this->isRelation($name) ) {
143 1
            require_once 'Xyster/Orm/Relation/Exception.php';
144 1
            throw new Xyster_Orm_Relation_Exception("'" . $name
145 1
                . "' is not a valid relation for the '" . $this->_class . "' class");
146 0
        }
147
148 56
        return $this->_relations[$name];
149
    }
150
151
    /**
152
     * Gets the names of all relations defined for this entity
153
     *
154
     * @return array The names of defined relations
155
     */
156
    public function getRelationNames()
157
    {
158 34
        return array_keys($this->_relations);
159
    }
160
161
    /**
162
     * Gets the relations for the entity
163
     *
164
     * @return array An array of {@link Xyster_Orm_Relation} objects
165
     */
166
    public function getRelations()
167
    {
168 39
        return $this->_relations;
169
    }
170
171
    /**
172
	 * Creates a 'one to one' relationship for entities on the 'many' end of a 'one to many' relationship
173
	 *
174
	 * Options can contain the following values:
175
	 *
176
	 * <dl>
177
	 * <dt>class</dt><dd>The foreign class. The relation name by default</dd>
178
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the declaring
179
	 * entity.  This should either be an array (if multiple) or a string (if
180
	 * one).  By default, this is <var>class</var>Id</dd>
181
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
182
	 * against the entity to be loaded</dd>
183
	 * </dl>
184
	 *
185
	 * @param string $name  The name of the relationship
186
	 * @param array $options  An array of options
187
	 * @return Xyster_Orm_Relation  The relationship created
188
	 */
189
	public function belongsTo( $name, array $options = array() )
190
	{
191 201
		return $this->_baseCreate('belongs', $name, $options);
192
	}
193
	/**
194
	 * Creates a 'one to one' relationship
195
	 *
196
	 * Options can contain the following values:
197
	 *
198
	 * <dl>
199
	 * <dt>class</dt><dd>The foreign class. The relation name by default</dd>
200
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the declaring
201
	 * entity.  This should either be an array (if multiple) or a string (if
202
	 * one).  By default, this is <var>class</var>Id</dd>
203
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
204
	 * against the entity to be loaded</dd>
205
	 * </dl>
206
	 *
207
	 * @param string $name  The name of the relationship
208
	 * @param array $options  An array of options
209
	 * @return Xyster_Orm_Relation  The relationship created
210
	 */
211
	public function hasOne( $name, array $options = array() )
212
	{
213 1
		return $this->_baseCreate('one', $name, $options);
214
	}
215
	/**
216
	 * Creates a 'one to many' relationship
217
	 *
218
	 * Options can contain the following values:
219
	 *
220
	 * <dl>
221
	 * <dt>class</dt><dd>The foreign class.  The relation name minus a trailing
222
	 * 's' by default</dd>
223
	 * <dt>id</dt><dd>The name of the foreign key field(s) on the related
224
	 * entity.  This should either be an array (if multiple) or a string (if
225
	 * one).  By default, this is <var>class</var>Id</dd>
226
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
227
	 * against the entities to be loaded</dd>
228
	 * </dl>
229
	 *
230
	 * @param string $name  The name of the relationship
231
	 * @param array $options  An array of options
232
	 * @return Xyster_Orm_Relation  The relationship created
233
	 */
234
	public function hasMany( $name, array $options = array() )
235
	{
236 33
		return $this->_baseCreate('many', $name, $options);
237
	}
238
	/**
239
	 * Creates a 'many to many' relationship
240
	 *
241
	 * <dl>
242
	 * <dt>class</dt><dd>The class of entity to load through the join table. It
243
	 * will be the relationship name minus a trailing 's' by default.</dd>
244
	 * <dt>table</dt><dd>The join table name.  By default this will be
245
	 * <var>declaring_class</var>_<var>class</var></dd>
246
	 * <dt>left</dt><dd>The column(s) in the join table referencing the
247
	 * declaringClass entity. By default: <var>declaring_class</var>_id</dd>
248
	 * <dt>right</dt><dd>The column(s) in the join table referencing the
249
	 * foreign entity.  By default, it's <var>class_name</var>_id</dd>
250
	 * <dt>filters</dt><dd>In XSQL, any Criteria that should be used
251
	 * against the join table.  Column names should be specified in the format
252
	 * native to the data store (i.e. with underscores, not camelCase)</dd>
253
	 * </dl>
254
	 *
255
	 * @param string $name  The name of the relationship
256
	 * @param array $options  An array of options
257
	 * @return Xyster_Orm_Relation  The relationship created
258
	 */
259
	public function hasJoined( $name, array $options = array() )
260
	{
261 201
		return $this->_baseCreate('joined', $name, $options);
262
	}
263
264
	/**
265
     * Whether the entity has a relationship with the name supplied
266
     *
267
     * @param string $name The name of the relationship
268
     * @return boolean
269
     */
270
	public function isRelation( $name )
271
	{
272 59
	    return array_key_exists($name, $this->_relations);
273
	}
274
275
    /**
276
     * Base creator method
277
     *
278
     * @param string $type The type of the relationship
279
     * @param string $name The name of the relationship
280
     * @param array $options An array of options
281
     * @return Xyster_Orm_Relation
282
     * @throws Xyster_Orm_Relation_Exception if the relationship is already defined
283
     */
284
    protected function _baseCreate( $type, $name, array $options )
285
    {
286 201
        $declaringClass = $this->_class;
287
288 201
        if ( array_key_exists($name, $this->_relations) ) {
289 1
            require_once 'Xyster/Orm/Relation/Exception.php';
290 1
            throw new Xyster_Orm_Relation_Exception("The relationship '" . $name . "' already exists");
291 0
        }
292
293 201
        require_once 'Xyster/Orm/Relation.php';
294 201
        $this->_relations[$name] = new Xyster_Orm_Relation($this, $type, $name, $options);
295
296 201
        return $this->_relations[$name];
297
    }
298
}


Report generated at 2007-11-05T09:09:01-05:00