| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Helix\DB\SQL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Helix\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Represents a `CASE` expression, with or without a *subject*. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * If a subject is used, `WHEN` relies on value equivalence (simple case). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Otherwise `WHEN` relies on predicates (searched case). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * With a subject (simple case): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * CASE subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  WHEN value THEN value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *  ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * END | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Without a subject (searched case): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * CASE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *  WHEN predicate then value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *  ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * END | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class Choice extends Value { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $else; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $subject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * [when => then] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected $values = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param DB $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string|ValueInterface $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param array $values `[when => then]` for the subject. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function __construct (DB $db, $subject = null, array $values = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         parent::__construct($db, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->subject = (string)$this->db->quote($subject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->setValues($values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function __toString () { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $sql = 'CASE'; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         if (strlen($this->subject)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $sql .= " {$this->subject}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         foreach ($this->values as $when => $then) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $sql .= " WHEN {$when} THEN {$then}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if (strlen($this->else)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $sql .= " ELSE {$this->else}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $sql .= " END"; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         return $sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string|ValueInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function setElse ($else) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->else = (string)$this->db->quote($else); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Quotes and sets a conditional value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string|ValueInterface $when | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string|ValueInterface $then | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function setValue ($when, $then) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $when = (string)$this->db->quote($when); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $then = (string)$this->db->quote($then); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->values[$when] = $then; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Quotes and sets multiple conditional values for the subject. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param array $values `[when => then]` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function setValues (array $values) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         foreach ($values as $when => $then) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->setValue($when, $then); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | } |