Root/
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 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of Plume Framework, a simple PHP Application Framework. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. # # Plume Framework is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Plume Framework is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * Allow to iterate over an array of standard classes with * 'model_class' and 'model_id' properties set. * * Optional properties from the classses can also be extracted and * added as properties to the model. * * Suppose you have a $data array with: * array( * (object) array('model_class' => 'MyApp_Item', * 'model_id' => 32, * 'foo' => 'bingo plus'), * (object) array('model_class' => 'MyApp_Bong', * 'model_id' => 12, * 'foo' => 'youpla'), * (object) array('model_class' => 'MyApp_Item', * 'model_id' => 14, * 'foo' => 'bingo'), * ); * * You can do: * $set = new Pluf_Model_Set($data, array('foo' => '_Foo')); * foreach ($set as $res) { * echo $res; // Will be the loaded model based on the class and id. * echo $res->_Foo; // will contain the value foo of the $data array. * } * */ class Pluf_Model_Set implements Iterator, ArrayAccess { protected $data = array (); protected $keys = array (); public function __construct( $data , $keys = array ()) { $this ->data = $data ; $this ->keys = $keys ; reset( $this ->data); } /** * Get the current item. */ public function current() { $i = current( $this ->data); $item = Pluf::factory( $i ->model_class, $i ->model_id); foreach ( $this ->keys as $key => $val ) { $item -> $val = $i -> $key ; } return $item ; } public function key() { return key( $this ->data); } public function next() { next( $this ->data); } public function rewind () { reset( $this ->data); } public function valid() { // We know that the boolean false will not be stored as a // field, so we can test against false to check if valid or // not. return (false !== current( $this ->data)); } public function count () { return count ( $this ->data); } public function offsetUnset( $index ) { unset( $this ->data[ $index ]); } public function offsetSet( $index , $value ) { if (! is_object ( $value ) or is_subclass_of ( $value , 'Pluf_Model' )) { throw new Exception( 'Must be a subclass of Pluf_Model: ' . $value ); } $res = array ( 'model_class' => $value ->_model, 'model_id' => $value ->id); foreach ( $this ->keys as $key => $name ) { $res [ $key ] = (isset( $value -> $name )) ? $value -> $name : null; } $this ->data[ $index ] = (object) $res ; } public function offsetGet( $index ) { if (!isset( $this ->data[ $index ])) { throw new Exception( 'Undefined index: ' . $index ); } $i = $this ->data[ $index ]; $item = Pluf::factory( $i ->model_class, $i ->model_id); foreach ( $this ->keys as $key => $val ) { $item -> $val = $i -> $key ; } return $item ; } public function offsetExists( $index ) { return (isset( $this ->data[ $index ])); } } |