ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

functional_programming.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
42 function _return_or_throw( $DefaultValue , $Field )
43 {
44 if( $DefaultValue === '_throw_exception' )
45 {
46 throw( new Exception( "Key '$Field' does not exists" ) );
47 }
48 else
49 {
50 return( $DefaultValue );
51 }
52 }
53
84 function get_field( &$Entity , $Field , $DefaultValue = '_throw_exception' )
85 {
86 try
87 {
88 if( is_object( $Entity ) )
89 {
90 if( property_exists( $Entity , $Field ) )
91 {
92 return( $Entity->$Field );
93 }
94
95 return( _return_or_throw( $DefaultValue , $Field ) );
96 }
97
98 if( is_array( $Entity ) )
99 {
100 if( array_key_exists( $Field , $Entity ) )
101 {
102 return( $Entity[ $Field ] );
103 }
104
105 return( _return_or_throw( $DefaultValue , $Field ) );
106 }
107
108 throw( new Exception( "Illegal value was passed" ) );
109 }
110 catch( Exception $e )
111 {
112 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
113 }
114 }
115
146 function get_fields( &$Entity , $Fields , $DefaultValue = '_throw_exception' )
147 {
148 try
149 {
150 $Values = array();
151 $Fields = explode( ',' , $Fields );
152
153 foreach( $Fields as $i => $Field )
154 {
155 $Values [] = get_field( $Entity , $Field , $DefaultValue );
156 }
157
158 return( $Values );
159 }
160 catch( Exception $e )
161 {
162 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
163 }
164 }
165
192 function is_field_set( &$Entity , $Field )
193 {
194 try
195 {
196 if( is_object( $Entity ) )
197 {
198 if( property_exists( $Entity , $Field ) )
199 {
200 return( true );
201 }
202 }
203
204 if( is_array( $Entity ) )
205 {
206 if( isset( $Entity[ $Field ] ) )
207 {
208 return( true );
209 }
210 }
211
212 return( false );
213 }
214 catch( Exception $e )
215 {
216 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
217 }
218 }
219
250 function get_field_ex( &$ArrayOfEntities , $Field , $DefaultValue = '_throw_exception' )
251 {
252 try
253 {
254 $RetValues = array();
255
256 foreach( $ArrayOfEntities as $k => $v )
257 {
258 $RetValues [] = get_field( $v , $Field , $DefaultValue );
259 }
260
261 return( $RetValues );
262 }
263 catch( Exception $e )
264 {
265 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
266 }
267 }
268
295 function array_filter_ex( &$ArrayOfEntities , $Condition = '1 == 1' )
296 {
297 try
298 {
299 $FilterFunction = create_function( '$Element' , "return( $Condition );" );
300
301 $FilteredArrayOfEntities = array_filter( $ArrayOfEntities , $FilterFunction );
302
303 return( $FilteredArrayOfEntities );
304 }
305 catch( Exception $e )
306 {
307 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
308 }
309 }
310
345 function get_field_cond( &$ArrayOfEntities , $Field , $Condition = '1 == 1' ,
346 $DefaultValue = '_throw_exception' )
347 {
348 try
349 {
350 $RetValues = array();
351
352 $FilteredArrayOfEntities = array_filter_ex( $ArrayOfEntities , $Condition );
353
354 foreach( $FilteredArrayOfEntities as $k => $v )
355 {
356 $RetValues [] = get_field( $v , $Field , $DefaultValue );
357 }
358
359 return( $RetValues );
360 }
361 catch( Exception $e )
362 {
363 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
364 }
365 }
366
397 function array_sum_cond( &$Array , $Field = false , $Condition = '1 == 1' )
398 {
399 try
400 {
401 if( count( $Array ) )
402 {
403 $Keys = array_keys( $Array );
404
405 if( is_array( $Array[ $Keys[ 0 ] ] ) )
406 {
407 $Sum = 0;
408 foreach( $Array as $i => $Element )
409 {
410 $Sum += array_sum_cond( $Element , $Field , $Condition );
411 }
412 return( $Sum );
413 }
414 }
415
416 if( $Field !== false )
417 {
418 $Array = get_field_cond( $Array , $Field , $Condition );
419 }
420
421 return( array_sum( $Array ) );
422 }
423 catch( Exception $e )
424 {
425 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
426 }
427 }
428
459 function set_field( &$Entity , $Field , $Value )
460 {
461 try
462 {
463 if( is_object( $Entity ) )
464 {
465 $Entity->$Field = $Value;
466 return( $Entity );
467 }
468
469 if( is_array( $Entity ) )
470 {
471 $Entity[ $Field ] = $Value;
472 return( $Entity );
473 }
474
475 return( $Entity );
476 }
477 catch( Exception $e )
478 {
479 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
480 }
481 }
482
513 function append_to_field( &$Entity , $Field , $Value )
514 {
515 try
516 {
517 if( is_field_set( $Entity , $Field ) === false || get_field( $Entity , $Field , false ) === false )
518 {
519 set_field( $Entity , $Field , array() );
520 }
521
522 if( is_object( $Entity ) )
523 {
524 array_push( $Entity->$Field , $Value );
525 }
526
527 if( is_array( $Entity ) )
528 {
529 array_push( $Entity[ $Field ] , $Value );
530 }
531
532 return( $Entity );
533 }
534 catch( Exception $e )
535 {
536 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
537 }
538 }
539
570 function implode_ex( $Str , &$Arr , $Field )
571 {
572 try
573 {
574 return( implode( $Str , get_field_ex( $Arr , $Field ) ) );
575 }
576 catch( Exception $e )
577 {
578 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
579 }
580 }
581
608 function extend( &$Destination , $Source )
609 {
610 try
611 {
612 if( $Source === false )
613 {
614 return( $Destination );
615 }
616
617 foreach( $Source as $k => $v )
618 {
619 $Destination = set_field( $Destination , $k , $v );
620 }
621
622 return( $Destination );
623 }
624 catch( Exception $e )
625 {
626 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
627 }
628 }
629
656 function array_sum_ex( &$Array , $Field = false )
657 {
658 try
659 {
660 if( $Field !== false )
661 {
662 $Array2 = get_field_ex( $Array , $Field );
663 }
664 else
665 {
666 $Array2 = $Array;
667 }
668
669 return( array_sum( $Array2 ) );
670 }
671 catch( Exception $e )
672 {
673 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
674 }
675 }
676
699 function get_dummie( &$Entity )
700 {
701 try
702 {
703 if( is_array( $Entity ) )
704 {
705 return( array() );
706 }
707 elseif( is_object( $Entity ) )
708 {
709 return( new stdClass() );
710 }
711 else
712 {
713 throw( new Exception( "Illegal data type : ".gettype( $Entity ) ) );
714 }
715 }
716 catch( Exception $e )
717 {
718 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
719 }
720 }
721
748 function remove_fields( &$Entity , $Fields )
749 {
750 try
751 {
752 if( is_array( $Fields ) === false )
753 {
754 $Fields = array( $Fields );
755 }
756
757 $ChangedEntity = get_dummie( $Entity );
758
759 foreach( $Entity as $Field => $Value )
760 {
761 if( array_search( $Field , $Fields ) === false )
762 {
763 set_field( $ChangedEntity , $Field , $Value );
764 }
765 }
766
767 return( $Entity = $ChangedEntity );
768 }
769 catch( Exception $e )
770 {
771 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
772 }
773 }
774
797 function make_array( &$Scalar )
798 {
799 try
800 {
801 if( is_array( $Scalar ) || is_object( $Scalar ) )
802 {
803 return( $Scalar );
804 }
805 else
806 {
807 return( array( $Scalar ) );
808 }
809 }
810 catch( Exception $e )
811 {
812 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
813 }
814 }
815
842 function make_hash_by_field( &$Array , $Field )
843 {
844 try
845 {
846 $Return = array();
847
848 foreach( $Array as $k => $v )
849 {
850 $Return[ get_field( $v , $Field ) ] = $v;
851 }
852
853 return( $Return );
854 }
855 catch( Exception $e )
856 {
857 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
858 }
859 }
860
887 function vectorize_by_field( &$Array , $Field )
888 {
889 try
890 {
891 $Return = array();
892
893 foreach( $Array as $k => $v )
894 {
895 $Key = get_field( $v , $Field );
896
897 if( isset( $Return[ $Key ] ) === false )
898 {
899 $Return[ $Key ] = array();
900 }
901
902 $Return[ $Key ][] = $v;
903 }
904
905 return( $Return );
906 }
907 catch( Exception $e )
908 {
909 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
910 }
911 }
912
947 function get_record_by_field( &$Array , $Field , $Value , $DefaultValue = '_throw_exception' )
948 {
949 try
950 {
951 foreach( $Array as $i => $Element )
952 {
953 if( get_field( $Element , $Field , $DefaultValue ) == $Value )
954 {
955 return( $Element );
956 }
957 }
958
959 return( false );
960 }
961 catch( Exception $e )
962 {
963 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
964 }
965 }
966
967 ?>