Exemplo n.º 1
0
 /**
  * @param string $filecontent
  *
  * @return CookieCollectionInterface
  */
 public function parseContent(string $filecontent) : CookieCollectionInterface
 {
     $cookies = new CookieCollection();
     foreach (explode("\n", $filecontent) as $line) {
         $line = trim($line);
         $cookieData = array_map('trim', explode("\t", $line, 7));
         if (count($cookieData) !== 7) {
             continue;
         }
         $httpOnly = false;
         $httpOnlyPrefixLength = strlen(self::HTTP_ONLY_PREFIX);
         if (substr($cookieData[0], 0, $httpOnlyPrefixLength) === self::HTTP_ONLY_PREFIX) {
             $cookieData[0] = substr($cookieData[0], $httpOnlyPrefixLength);
             $httpOnly = true;
         } else {
             if ($cookieData[0][0] === '#') {
                 continue;
             }
         }
         $expire = empty($cookieData[4]) ? null : $cookieData[4];
         if (preg_match('#^[0-9]+$#i', $expire)) {
             $expire = new DateTime(date('Y-m-d H:i:s', (int) $expire));
         }
         $cookies->add((new Cookie())->setDomain($cookieData[0])->setHttpOnly($httpOnly)->setFlag(strtolower($cookieData[1]) === 'true')->setPath($cookieData[2])->setSecure(strtolower($cookieData[3]) === 'true')->setExpire($expire)->setName($cookieData[5])->setValue($cookieData[6]));
     }
     return $cookies;
 }
 public function testAddNoDomainEmptyCollection()
 {
     $this->expectException(CookieCollectionException::class);
     $collection = new CookieCollection();
     $collection->add((new Cookie())->setName('foo')->setValue('bar'));
 }
 /**
  * @param string|null $domain
  *
  * @return CookieCollectionInterface
  */
 public function getAll(string $domain = null) : CookieCollectionInterface
 {
     if ($domain === null) {
         return $this;
     } else {
         $collection = new CookieCollection();
         if (isset($this->cookies[$domain])) {
             foreach ($this->cookies[$domain] as $cookie) {
                 $collection->add($cookie);
             }
         }
         return $collection;
     }
 }