# Source for file Filter.php

Documentation is available at Filter.php

1. <?php
2. /*
3. * $Id: Record.php 1298 2007-05-01 19:26:03Z zYne $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
22. /**
23. * Doctrine_Record_Filter
24. * Filters and prepares the record data
25. *
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @package Doctrine
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. * @version $Revision: 1298 $
33. */
34. class Doctrine_Record_Filter extends Doctrine_Object
35. {
36. /**
37. * @var Doctrine_Record $_record the record object this filter belongs to
38. */
39. protected $_record;
40. /**
41. * constructor
42. *
43. * @param Doctrine_Record $_record the record object this filter belongs to
44. */
45. public function __construct(Doctrine_Record $record)
46. {
47. $this->_record = $record;
48. }
49. /**
50. * getRecord
51. *
52. * @return Doctrine_Record $_record the record object this filter belongs to
53. */
54. public function getRecord()
55. {
56. return $this->_record;
57. }
58. /**
59. * setDefaultValues
60. * sets the default values for records internal data
61. *
62. * @param boolean $overwrite whether or not to overwrite the already set values
63. * @return boolean
64. */
65. public function assignDefaultValues($data, $overwrite = false)
66. {
67. $table = $this->_record->getTable();
69. if ( ! $table->hasDefaultValues()) {
70. return false;
71. }
72. $modified = array();
73. foreach ($data as $column => $value) {
74. $default = $table->getDefaultValueOf($column);
76. if ($default === null) {
77. $default = self::$_null;
78. }
80. if ($value === self::$_null || $overwrite) {
81. $this->_record->rawSet($column, $default);
82. $modified[] = $column;
83. $this->_record->state(Doctrine_Record::STATE_TDIRTY);
84. }
85. }
86. $this->_record->setModified($modified);
87. }
88. /**
89. * prepareIdentifiers
90. * prepares identifiers for later use
91. *
92. * @param boolean $exists whether or not this record exists in persistent data store
93. * @return void
94. */
95. private function prepareIdentifiers($exists = true)
96. {
97. $id = $this->_table->getIdentifier();
98. $this->_id = array();
99. if (count($id) > 1) {
100. foreach ($id as $name) {
101. if ($this->_data[$name] === self::$_null) {
102. $this->_id[$name] = null;
103. } else {
104. $this->_id[$name] = $this->_data[$name];
105. }
106. }
107. } else {
108. if (isset($this->_data[$id]) && $this->_data[$id] !== self::$_null) {
109. $this->_id[$id] = $this->_data[$id];
110. }
111. }
112. }
113. /**
114. * getPrepared
115. *
116. * returns an array of modified fields and values with data preparation
117. * adds column aggregation inheritance and converts Records into primary key values
118. *
119. * @param array $array
120. * @return array
121. */
122. public function getPrepared(array $array = array()) {
123. $a = array();
125. if (empty($array)) {
126. $array = $this->_modified;
127. }
128. foreach ($array as $k => $v) {
129. $type = $this->_table->getTypeOf($v);
131. if ($this->_data[$v] === self::$_null) {
132. $a[$v] = null;
133. continue;
134. }
136. switch ($type) {
137. case 'array':
138. case 'object':
139. $a[$v] = serialize($this->_data[$v]);
140. break;
141. case 'gzip':
142. $a[$v] = gzcompress($this->_data[$v],5);
143. break;
144. case 'boolean':
145. $a[$v] = $this->getTable()->getConnection()->convertBooleans($this->_data[$v]);
146. break;
147. case 'enum':
148. $a[$v] = $this->_table->enumIndex($v,$this->_data[$v]);
149. break;
150. default:
151. if ($this->_data[$v] instanceof Doctrine_Record) {
152. $this->_data[$v] = $this->_data[$v]->getIncremented();
153. }
155. $a[$v] = $this->_data[$v];
156. }
157. }
158. $map = $this->_table->inheritanceMap;
159. foreach ($map as $k => $v) {
160. $old = $this->get($k, false);
162. if ((string) $old !== (string) $v || $old === null) {
163. $a[$k] = $v;
164. $this->_data[$k] = $v;
165. }
166. }
168. return $a;
169. }
170. }