/**
  * {@inheritdoc}
  */
 public function add(Cookie $cookie)
 {
     if (!$cookie->getValue() || !$cookie->getName() || !$cookie->getDomain()) {
         return false;
     }
     // Resolve conflicts with previously set cookies
     foreach ($this->cookies as $i => $c) {
         // Check the regular comparison fields
         if ($c->getPath() != $cookie->getPath() || $c->getMaxAge() != $cookie->getMaxAge() || $c->getDomain() != $cookie->getDomain() || $c->getHttpOnly() != $cookie->getHttpOnly() || $c->getPorts() != $cookie->getPorts() || $c->getSecure() != $cookie->getSecure() || $c->getName() != $cookie->getName()) {
             continue;
         }
         // The previously set cookie is a discard cookie and this one is not
         // so allow the new cookie to be set
         if (!$cookie->getDiscard() && $c->getDiscard()) {
             unset($this->cookies[$i]);
             continue;
         }
         // If the new cookie's expiration is further into the future, then
         // replace the old cookie
         if ($cookie->getExpires() > $c->getExpires()) {
             unset($this->cookies[$i]);
             continue;
         }
         // If the value has changed, we better change it
         if ($cookie->getValue() !== $c->getValue()) {
             unset($this->cookies[$i]);
             continue;
         }
         // The cookie exists, so no need to continue
         return false;
     }
     $this->cookies[] = $cookie;
     return true;
 }
Beispiel #2
0
 public function testHoldsValues()
 {
     $t = time();
     $data = array('name' => 'foo', 'value' => 'baz', 'path' => '/bar', 'domain' => 'baz.com', 'expires' => $t, 'max_age' => 100, 'comment' => 'Hi', 'comment_url' => 'foo.com', 'port' => array(1, 2), 'version' => 2, 'secure' => true, 'discard' => true, 'http_only' => true, 'data' => array('foo' => 'baz', 'bar' => 'bam'));
     $cookie = new Cookie($data);
     $this->assertEquals($data, $cookie->toArray());
     $this->assertEquals('foo', $cookie->getName());
     $this->assertEquals('baz', $cookie->getValue());
     $this->assertEquals('baz.com', $cookie->getDomain());
     $this->assertEquals('/bar', $cookie->getPath());
     $this->assertEquals($t, $cookie->getExpires());
     $this->assertEquals(100, $cookie->getMaxAge());
     $this->assertEquals('Hi', $cookie->getComment());
     $this->assertEquals('foo.com', $cookie->getCommentUrl());
     $this->assertEquals(array(1, 2), $cookie->getPorts());
     $this->assertEquals(2, $cookie->getVersion());
     $this->assertTrue($cookie->getSecure());
     $this->assertTrue($cookie->getDiscard());
     $this->assertTrue($cookie->getHttpOnly());
     $this->assertEquals('baz', $cookie->getAttribute('foo'));
     $this->assertEquals('bam', $cookie->getAttribute('bar'));
     $this->assertEquals(array('foo' => 'baz', 'bar' => 'bam'), $cookie->getAttributes());
     $cookie->setName('a')->setValue('b')->setPath('c')->setDomain('bar.com')->setExpires(10)->setMaxAge(200)->setComment('e')->setCommentUrl('f')->setPorts(array(80))->setVersion(3)->setSecure(false)->setHttpOnly(false)->setDiscard(false)->setAttribute('snoop', 'dog');
     $this->assertEquals('a', $cookie->getName());
     $this->assertEquals('b', $cookie->getValue());
     $this->assertEquals('c', $cookie->getPath());
     $this->assertEquals('bar.com', $cookie->getDomain());
     $this->assertEquals(10, $cookie->getExpires());
     $this->assertEquals(200, $cookie->getMaxAge());
     $this->assertEquals('e', $cookie->getComment());
     $this->assertEquals('f', $cookie->getCommentUrl());
     $this->assertEquals(array(80), $cookie->getPorts());
     $this->assertEquals(3, $cookie->getVersion());
     $this->assertFalse($cookie->getSecure());
     $this->assertFalse($cookie->getDiscard());
     $this->assertFalse($cookie->getHttpOnly());
     $this->assertEquals('dog', $cookie->getAttribute('snoop'));
 }