| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Selami\Entity\DataType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Selami\Entity\Interfaces\DataTypeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Assert\Assertion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Assert\AssertionFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Text extends DataTypeAbstract implements DataTypeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const DATA_TYPE_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const DATA_LENGTH       = 'Assertion failed for value "%s" for "%s" : INVALID_TEXT_LENGTH'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected static $defaults = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         'default'   => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         'min'       => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'max'       => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'pad'       => 'left', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'left_pad'  => ' ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'right_pad' => ' ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected static $padOptions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'left'  => STR_PAD_LEFT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'right' => STR_PAD_RIGHT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Boolean constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param mixed $datum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @throws validArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct(string $key, $datum, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->key = $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->datum = $datum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->checkValidOptions($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->options = array_merge(self::$defaults, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function assert() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->isString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->checkLength(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private function isString() | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             Assertion::string($this->datum); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } catch (AssertionFailedException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $this->errorMessageTemplate = self::DATA_TYPE_ERROR; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $this->throwException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private function checkLength() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($this->options['max'] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return $this->checkLengthBetween(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             Assertion::minLength($this->datum, (int) $this->options['min']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } catch (AssertionFailedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->errorMessageTemplate = self::DATA_LENGTH . 'MIN:' . $this->options['min']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->throwException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private function checkLengthBetween() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             Assertion::betweenLength($this->datum, (int) $this->options['min'], (int) $this->options['max']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } catch (AssertionFailedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->errorMessageTemplate = self::DATA_LENGTH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 . 'MIN:' . $this->options['min'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 . 'MAX:' . $this->options['max']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->throwException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function normalize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (null === $this->datum) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return $this->options['default']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if ($this->options['max'] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $length = (int) $this->options['max']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return mb_substr($this->datum, 0, $length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if (strlen($this->datum) < (int) $this->options['min']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $padType = self::$padOptions[$this->options['pad']] ?: STR_PAD_RIGHT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $padding = $this->options[$this->options['pad'].'_pad']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return str_pad($this->datum, (int) $this->options['min'], $padding, $padType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $this->datum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.