55use UnexpectedValueException ;
66
77class Insert extends InsertUpdateStatement {
8- /**
9- * @var array
10- */
8+ /** @var array */
119 private $ fields = array ();
12- /**
13- * @var array
14- */
10+ /** @var array */
1511 private $ update = array ();
16- /**
17- * @var string
18- */
12+ /** @var string */
1913 private $ table = null ;
20- /**
21- * @var string
22- */
14+ /** @var string */
2315 private $ keyField = null ;
24- /**
25- * @var bool
26- */
16+ /** @var bool */
2717 private $ ignore = false ;
28- /**
29- * @var Select
30- */
18+ /** @var Select */
3119 private $ from = null ;
32- /**
33- * @var callable
34- */
20+ /** @var callable */
3521 private $ tableFields = null ;
3622
3723 /**
@@ -77,12 +63,7 @@ public function setKey($field) {
7763 * @return $this
7864 */
7965 public function add ($ field , $ value ) {
80- if ($ this ->isFieldNameValid ($ field )) {
81- throw new UnexpectedValueException ('Field name is invalid ' );
82- }
83- $ sqlField = $ field ;
84- $ sqlValue = $ this ->db ()->quote ($ value );
85- $ this ->fields [$ sqlField ] = $ sqlValue ;
66+ $ this ->fields = $ this ->addTo ($ this ->fields , $ field , $ value );
8667 return $ this ;
8768 }
8869
@@ -93,12 +74,7 @@ public function add($field, $value) {
9374 * @return $this
9475 */
9576 public function update ($ field , $ value ) {
96- if ($ this ->isFieldNameValid ($ field )) {
97- throw new UnexpectedValueException ('Field name is invalid ' );
98- }
99- $ sqlField = $ field ;
100- $ sqlValue = $ this ->db ()->quote ($ value );
101- $ this ->update [$ sqlField ] = $ sqlValue ;
77+ $ this ->update = $ this ->addTo ($ this ->update , $ field , $ value );
10278 return $ this ;
10379 }
10480
@@ -148,13 +124,9 @@ public function addOrUpdateExpr($str) {
148124 * @return $this
149125 */
150126 public function addAll (array $ data , array $ mask = null ) {
151- if ($ mask !== null ) {
152- $ data = array_intersect_key ($ data , array_combine ($ mask , $ mask ));
153- }
154- $ data = $ this ->clearValues ($ data );
155- foreach ($ data as $ field => $ value ) {
127+ $ this ->addAllTo ($ data , $ mask , function ($ field , $ value ) {
156128 $ this ->add ($ field , $ value );
157- }
129+ });
158130 return $ this ;
159131 }
160132
@@ -164,15 +136,11 @@ public function addAll(array $data, array $mask = null) {
164136 * @return $this
165137 */
166138 public function updateAll (array $ data , array $ mask = null ) {
167- if ($ mask !== null ) {
168- $ data = array_intersect_key ($ data , array_combine ($ mask , $ mask ));
169- }
170- $ data = $ this ->clearValues ($ data );
171- foreach ($ data as $ field => $ value ) {
172- if ($ field != $ this ->keyField ) {
139+ $ this ->addAllTo ($ data , $ mask , function ($ field , $ value ) {
140+ if ($ field !== $ this ->keyField ) {
173141 $ this ->update ($ field , $ value );
174142 }
175- }
143+ });
176144 return $ this ;
177145 }
178146
@@ -234,6 +202,38 @@ public function __toString() {
234202 return $ query ;
235203 }
236204
205+ /**
206+ * @param array $fields
207+ * @param string $field
208+ * @param bool|int|float|string $value
209+ * @return array
210+ */
211+ private function addTo ($ fields , $ field , $ value ) {
212+ if ($ this ->isFieldNameValid ($ field )) {
213+ throw new UnexpectedValueException ('Field name is invalid ' );
214+ }
215+ $ sqlField = $ field ;
216+ $ sqlValue = $ this ->db ()->quote ($ value );
217+ $ fields [$ sqlField ] = $ sqlValue ;
218+ return $ fields ;
219+ }
220+
221+ /**
222+ * @param array $data
223+ * @param array $mask
224+ * @param callable $fn
225+ * @return $this
226+ */
227+ private function addAllTo ($ data , $ mask , $ fn ) {
228+ if ($ mask !== null ) {
229+ $ data = array_intersect_key ($ data , array_combine ($ mask , $ mask ));
230+ }
231+ $ data = $ this ->clearValues ($ data );
232+ foreach ($ data as $ field => $ value ) {
233+ call_user_func ($ fn , $ field , $ value );
234+ }
235+ }
236+
237237 /**
238238 * @return string
239239 */
0 commit comments