| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace PHPSu\ShellCommandBuilder\Conditional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPSu\ShellCommandBuilder\Definition\ConditionalOperator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPSu\ShellCommandBuilder\ShellInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @psalm-internal PHPSu\ShellCommandBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | abstract class BasicExpression implements ShellInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * This is not POSIX-compatible (only eg. Korn and Bash), beware before using it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $bashEnhancedBrackets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var bool this is always double quoted */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $escapedValue = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $negateExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var string|ShellInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $compare = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var string|ShellInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $compareWith = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $operator = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(bool $useBashBrackets, bool $negateExpression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->negateExpression = $negateExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->bashEnhancedBrackets = $useBashBrackets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function escapeValue(bool $enable): BasicExpression | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->escapedValue = $enable; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @todo with min. support of php 7.4 this can be fully implemented here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param bool $useBashBrackets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param bool $negateExpression | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     abstract public static function create(bool $useBashBrackets = false, bool $negateExpression = false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string|ShellInterface $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return string|array<mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private function getValueDebug($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($value instanceof ShellInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             return $value->__toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string|ShellInterface $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function getValue($value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $return = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($value instanceof ShellInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $return = $value->__toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($this->escapedValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             /** @psalm-suppress ImplicitToStringCast */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $return = sprintf('"%s"', $return); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function __toString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             '%s %s%s%s%s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_LEFT_BASH : ConditionalOperator::BRACKET_LEFT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->negateExpression ? '! ' : '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->compare ? $this->getValue($this->compare) . ' ' : '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $this->compareWith ? ' ' . $this->getValue($this->compareWith) : '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_RIGHT_BASH : ConditionalOperator::BRACKET_RIGHT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return array<string,mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function __toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'bashBrackets' => $this->bashEnhancedBrackets, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             'negate' => $this->negateExpression, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             'compare' => $this->getValueDebug($this->compare), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'operator' => $this->operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'compareWith' => $this->getValueDebug($this->compareWith), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |