Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http\Message\Header;
4
 
5
use Guzzle\Http\Message\Header;
6
 
7
/**
8
 * Provides helpful functionality for link headers
9
 */
10
class Link extends Header
11
{
12
    /**
13
     * Add a link to the header
14
     *
15
     * @param string $url    Link URL
16
     * @param string $rel    Link rel
17
     * @param array  $params Other link parameters
18
     *
19
     * @return self
20
     */
21
    public function addLink($url, $rel, array $params = array())
22
    {
23
        $values = array("<{$url}>", "rel=\"{$rel}\"");
24
 
25
        foreach ($params as $k => $v) {
26
            $values[] = "{$k}=\"{$v}\"";
27
        }
28
 
29
        return $this->add(implode('; ', $values));
30
    }
31
 
32
    /**
33
     * Check if a specific link exists for a given rel attribute
34
     *
35
     * @param string $rel rel value
36
     *
37
     * @return bool
38
     */
39
    public function hasLink($rel)
40
    {
41
        return $this->getLink($rel) !== null;
42
    }
43
 
44
    /**
45
     * Get a specific link for a given rel attribute
46
     *
47
     * @param string $rel Rel value
48
     *
49
     * @return array|null
50
     */
51
    public function getLink($rel)
52
    {
53
        foreach ($this->getLinks() as $link) {
54
            if (isset($link['rel']) && $link['rel'] == $rel) {
55
                return $link;
56
            }
57
        }
58
 
59
        return null;
60
    }
61
 
62
    /**
63
     * Get an associative array of links
64
     *
65
     * For example:
66
     * Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"
67
     *
68
     * <code>
69
     * var_export($response->getLinks());
70
     * array(
71
     *     array(
72
     *         'url' => 'http:/.../front.jpeg',
73
     *         'rel' => 'back',
74
     *         'type' => 'image/jpeg',
75
     *     )
76
     * )
77
     * </code>
78
     *
79
     * @return array
80
     */
81
    public function getLinks()
82
    {
83
        $links = $this->parseParams();
84
 
85
        foreach ($links as &$link) {
86
            $key = key($link);
87
            unset($link[$key]);
88
            $link['url'] = trim($key, '<> ');
89
        }
90
 
91
        return $links;
92
    }
93
}