| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Ivory Http Adapter package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Eric GELOEN <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please read the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Ivory\HttpAdapter\Event\Cookie\Jar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Ivory\HttpAdapter\Event\Cookie\CookieFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Ivory\HttpAdapter\Event\Cookie\CookieInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ivory\HttpAdapter\Message\InternalRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Ivory\HttpAdapter\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author GeLo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class CookieJar implements CookieJarInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var CookieFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $cookieFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var CookieInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $cookies = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param CookieFactoryInterface|null $cookieFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 387 |  |     public function __construct(CookieFactoryInterface $cookieFactory = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 387 |  |         $this->setCookieFactory($cookieFactory ?: new CookieFactory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 387 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 126 |  |     public function getCookieFactory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 126 |  |         return $this->cookieFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 387 |  |     public function setCookieFactory(CookieFactoryInterface $cookieFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 387 |  |         $this->cookieFactory = $cookieFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 387 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 9 |  |     public function clean() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 9 |  |         foreach ($this->cookies as $cookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 9 |  |             if ($cookie->isExpired()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 9 |  |                 $this->removeCookie($cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 7 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 7 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 207 |  |     public function clear($domain = null, $path = null, $name = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 207 |  |         foreach ($this->cookies as $cookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 63 |  |             if ($domain !== null && !$cookie->matchDomain($domain)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 9 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 63 |  |             if ($path !== null && !$cookie->matchPath($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 63 |  |             if ($name !== null && $cookie->getName() !== $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 63 |  |             $this->removeCookie($cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 161 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 207 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 189 |  |     public function hasCookies() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 189 |  |         return !empty($this->cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 288 |  |     public function getCookies() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 288 |  |         return $this->cookies; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 117 |  |     public function setCookies(array $cookies) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 117 |  |         $this->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 117 |  |         $this->addCookies($cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 117 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 117 |  |     public function addCookies(array $cookies) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 117 |  |         foreach ($cookies as $cookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 117 |  |             $this->addCookie($cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 91 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 117 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 9 |  |     public function removeCookies(array $cookies) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 9 |  |         foreach ($cookies as $cookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 9 |  |             $this->removeCookie($cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 7 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 261 |  |     public function hasCookie(CookieInterface $cookie) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 261 |  |         return array_search($cookie, $this->cookies, true) !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 261 |  |     public function addCookie(CookieInterface $cookie) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 261 |  |         if (!$cookie->hasName() || $this->hasCookie($cookie)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 9 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 252 |  |         if (!$cookie->hasValue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 9 |  |             $this->clear( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 9 |  |                 $cookie->getAttribute(CookieInterface::ATTR_DOMAIN), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 9 |  |                 $cookie->getAttribute(CookieInterface::ATTR_PATH), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 9 |  |                 $cookie->getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 7 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 9 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 252 |  |         foreach ($this->cookies as $jarCookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 189 |  |             if (!$cookie->compare($jarCookie)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 162 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 27 |  |             if ($cookie->getExpires() > $jarCookie->getExpires()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 9 |  |                 $this->removeCookie($jarCookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 9 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 18 |  |             if ($cookie->getValue() !== $jarCookie->getValue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 9 |  |                 $this->removeCookie($jarCookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 9 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 9 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 196 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 252 |  |         $this->cookies[] = $cookie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 252 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 108 |  |     public function removeCookie(CookieInterface $cookie) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 108 |  |         unset($this->cookies[array_search($cookie, $this->cookies, true)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 108 |  |         $this->cookies = array_values($this->cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 108 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 9 |  |     public function populate(InternalRequestInterface $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 9 |  |         foreach ($this->cookies as $cookie) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 9 |  |             if (!$cookie->isExpired() && $cookie->match($request)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 9 |  |                 $request = $request->withAddedHeader('Cookie', (string) $cookie); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 7 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 7 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 9 |  |         return $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 211 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 212 | 9 |  |     public function extract(InternalRequestInterface $request, ResponseInterface $response) | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 214 | 9 |  |         foreach ($response->getHeader('Set-Cookie') as $header) { | 
            
                                                                        
                            
            
                                    
            
            
                | 215 | 9 |  |             $cookie = $this->cookieFactory->parse($header); | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 217 | 9 |  |             if (!$cookie->hasAttribute(CookieInterface::ATTR_DOMAIN)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 218 | 9 |  |                 $cookie->setAttribute(CookieInterface::ATTR_DOMAIN, $request->getUri()->getHost()); | 
            
                                                                        
                            
            
                                    
            
            
                | 219 | 7 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 221 | 9 |  |             $this->addCookie($cookie); | 
            
                                                                        
                            
            
                                    
            
            
                | 222 | 7 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 223 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 162 |  |     public function count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 162 |  |         return count($this->cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 | 162 |  |     public function getIterator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 | 162 |  |         return new \ArrayIterator($this->cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 240 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 241 |  |  |  | 
            
                        
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.