1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Stream;
38
39/**
40 * Class \Hoa\Stream\Context.
41 *
42 * Make a multiton of stream contexts.
43 *
44 * @copyright  Copyright © 2007-2017 Hoa community
45 * @license    New BSD License
46 */
47class Context
48{
49    /**
50     * Context ID.
51     *
52     * @var string
53     */
54    protected $_id               = null;
55
56    /**
57     * Multiton.
58     *
59     * @var array
60     */
61    protected static $_instances = [];
62
63
64
65    /**
66     * Construct a context.
67     *
68     */
69    protected function __construct($id)
70    {
71        $this->_id      = $id;
72        $this->_context = stream_context_create();
73
74        return;
75    }
76
77    /**
78     * Multiton.
79     *
80     * @param   string  $id    ID.
81     * @return  \Hoa\Stream\Context
82     * @throws  \Hoa\Stream\Exception
83     */
84    public static function getInstance($id)
85    {
86        if (empty($id)) {
87            throw new Exception('Context ID must not be null.', 0);
88        }
89
90        if (false === static::contextExists($id)) {
91            static::$_instances[$id] = new static($id);
92        }
93
94        return static::$_instances[$id];
95    }
96
97    /**
98     * Get context ID.
99     *
100     * @return  string
101     */
102    public function getId()
103    {
104        return $this->_id;
105    }
106
107    /**
108     * Check if a context exists.
109     *
110     * @param   string  $id    ID.
111     * @return  bool
112     */
113    public static function contextExists($id)
114    {
115        return array_key_exists($id, static::$_instances);
116    }
117
118    /**
119     * Set options.
120     * Please, see http://php.net/context.
121     *
122     * @param   array   $options    Options.
123     * @return  bool
124     */
125    public function setOptions(array $options)
126    {
127        return stream_context_set_option($this->getContext(), $options);
128    }
129
130    /**
131     * Set parameters.
132     * Please, see http://php.net/context.params.
133     *
134     * @param   array   $parameters    Parameters.
135     * @return  bool
136     */
137    public function setParameters(array $parameters)
138    {
139        return stream_context_set_params($this->getContext(), $parameters);
140    }
141
142    /**
143     * Get options.
144     *
145     * @return  array
146     */
147    public function getOptions()
148    {
149        return stream_context_get_options($this->getContext());
150    }
151
152    /**
153     * Get parameters.
154     * .
155     * @return  array
156     */
157    public function getParameters()
158    {
159        return stream_context_get_params($this->getContext());
160    }
161
162    /**
163     * Get context as a resource.
164     *
165     * @return  resource
166     */
167    public function getContext()
168    {
169        return $this->_context;
170    }
171}
172