| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Divergence package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) Henry Paradiz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Divergence\Models\Mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Divergence\Models\Interfaces\FieldSetMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * This is the default type mapping from PHP userspace to our Model field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package Divergence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author  Henry Paradiz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | class DefaultSetMapper implements FieldSetMapper | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 36 |  |     public static function setStringValue($value): ?string | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 36 |  |         return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 12 |  |     public static function setBooleanValue($value): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 12 |  |         return (bool)$value; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 15 |  |     public static function setDecimalValue($value): ?float | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 15 |  |         return is_null($value) ? null : (float)preg_replace('/[^-\d.]/', '', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 15 |  |     public static function setIntegerValue($value): ?int | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 15 |  |         return is_null($value) ? null : (int)preg_replace('/[^-\d]/', '', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 14 |  |     public static function setTimestampValue($value): ?string | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 14 |  |         if (is_numeric($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 12 |  |             return date('Y-m-d H:i:s', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 3 |  |         } elseif (is_string($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             // trim any extra stuff, or leave as-is if it doesn't fit the pattern | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 3 |  |             if (preg_match('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', $value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 return preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', '$1-$2-$3 $4:$5:$6', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 3 |  |                 return date('Y-m-d H:i:s', strtotime($value)); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Parses a potential date value. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * - If passed a number will assume it's a unix timestamp and convert to Y-m-d based on the provided timestamp. | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * - If passed a string will attempt to match m/d/y format. | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * - If not valid will then attempt  Y/m/d | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * - If passed an array will attempt to look for numeric values in the keys 'yyyy' for year, 'mm' for month, and 'dd' for day. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * - If none of the above worked will attempt to use PHP's strtotime. | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * - Otherwise null. | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param string|int|array $value Date or timestamp in various formats. | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @return null|string Date formatted as Y-m-d | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 13 |  |     public static function setDateValue($value): ?string | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 13 |  |         if (is_numeric($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 9 |  |             $value = date('Y-m-d', $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 6 |  |         } elseif (is_string($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             // check if m/d/y format | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 6 |  |             if (preg_match('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', $value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |                 $value = preg_replace('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', '$3-$1-$2', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             // trim time and any extra crap, or leave as-is if it doesn't fit the pattern | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 6 |  |             $value = preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2}).*/', '$1-$2-$3', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         } elseif (is_array($value) && count(array_filter($value))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             // collapse array date to string | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |             $value = sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |                 '%04u-%02u-%02u', | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |                 is_numeric($value['yyyy']) ? $value['yyyy'] : 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |                 is_numeric($value['mm']) ? $value['mm'] : 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |                 is_numeric($value['dd']) ? $value['dd'] : 0 | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |             if ($value = strtotime($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 $value = date('Y-m-d', $value) ?: null; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |                 $value = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 13 |  |         return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 1 |  |     public static function setSerializedValue($value): string | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 1 |  |         return serialize($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 109 |  |     public static function setEnumValue(array $values, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 | 109 |  |         return (in_array($value, $values) ? $value : null); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 12 |  |     public static function setListValue($value, ?string $delimiter): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 12 |  |         if (!is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             $delim = empty($delimiter) ? ',' : $delimiter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |             $value = array_filter(preg_split('/\s*'.$delim.'\s*/', $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 | 12 |  |         return $value; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 120 |  |  |  |