| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Chubbyphp\Serialization\Mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Chubbyphp\Serialization\Normalizer\LinkNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Chubbyphp\Serialization\Policy\PolicyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Chubbyphp\Serialization\Policy\NullPolicy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Psr\Link\LinkInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | final class NormalizationLinkMappingBuilder implements NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $groups = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var LinkNormalizerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $linkNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var PolicyInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $policy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |     private function __construct(string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string                  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param LinkNormalizerInterface $linkNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |     public static function create( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         string $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         LinkNormalizerInterface $linkNormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     ): NormalizationLinkMappingBuilderInterface { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |         $self = new self($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |         $self->linkNormalizer = $linkNormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |         return $self; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string   $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     public static function createCallback( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         string $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     ): NormalizationLinkMappingBuilderInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         $self->linkNormalizer = new CallbackLinkNormalizer($callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string        $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param LinkInterface $link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     public static function createLink( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         string $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         LinkInterface $link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     ): NormalizationLinkMappingBuilderInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $self->linkNormalizer = new LinkNormalizer($link); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param array $groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |     public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         $this->groups = $groups; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param PolicyInterface $policy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     public function setPolicy(PolicyInterface $policy): NormalizationLinkMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $this->policy = $policy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return NormalizationLinkMappingInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |     public function getMapping(): NormalizationLinkMappingInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  |         return new NormalizationLinkMapping( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 4 |  |             $this->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 4 |  |             $this->groups, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 4 |  |             $this->linkNormalizer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 4 |  |             $this->policy ?? new NullPolicy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                        
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.