Skip to content

Commit 5613368

Browse files
committed
Finalized API. Updated docs. Added additional testing. Thanks to @harikt, @MWOP, and @davedevelopment for help with the final API!
1 parent adc6bab commit 5613368

File tree

8 files changed

+398
-215
lines changed

8 files changed

+398
-215
lines changed

README.md

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ Installation
3030
$> composer require dflydev/fig-cookies
3131
```
3232

33-
While in early development, you may be required to be a little more specific:
34-
35-
```bash
36-
$> composer require dflydev/fig-cookies:^0.0@dev
37-
```
38-
3933

4034
Concepts
4135
--------
@@ -76,64 +70,71 @@ return new instances of the original with the requested changes.
7670

7771
While this style of design has many benefits it can become fairly
7872
verbose very quickly. In order to get around that, FIG Cookies provides
79-
a facade named `FigCookies` in an attempt to help simply things and make
80-
the whole process less verbose.
73+
two facades in an attempt to help simply things and make the whole process
74+
less verbose.
8175

8276

8377
Basic Usage
8478
-----------
8579

86-
The easiest way to start working with FIG Cookies is by using the `FigCookies`
87-
class. It is a facade to the primitive FIG Cookies classes. Its job is to
88-
make common tasks easier and less verbose.
80+
The easiest way to start working with FIG Cookies is by using the
81+
`FigRequestCookies` and `FigResponseCookies` classes. They are facades to the
82+
primitive FIG Cookies classes. Their jobs are to make common cookie related
83+
tasks easier and less verbose than working with the primitive classes directly.
8984

9085
There is overhead on creating `Cookies` and `SetCookies` and rebuilding
9186
requests and responses. Each of the `FigCookies` methods will go through this
9287
process so be wary of using too many of these calls in the same section of
9388
code. In some cases it may be better to work with the primitive FIG Cookies
94-
classes directly rather than using the facade.
89+
classes directly rather than using the facades.
9590

9691

9792
### Request Cookies
9893

99-
#### Get a Cookie from a Request
94+
Requests include cookie information in the **Cookie** request header. The
95+
cookies in this header are represented by the `Cookie` class.
96+
97+
To easily work with request cookies, use the `FigRequestCookies` facade.
98+
99+
#### Get a Request Cookie
100100

101-
The `getRequestCookie` method will return a `Cookie` instance that represents
102-
a cookie that exists in the request's headers. If no cookie by the specified
103-
name exists, the `Cookie` instance will have a `null` value.
101+
The `get` method will return a `Cookie` instance. If no cookie by the specified
102+
name exists, the returned `Cookie` instance will have a `null` value.
104103

105-
The optional third parameter to `getRequestCookie` sets the value that
106-
should be used if a cookie does not exist.
104+
The optional third parameter to `get` sets the value that should be used if a
105+
cookie does not exist.
107106

108107
```php
109-
use Dflydev\FigCookies\FigCookie;
108+
use Dflydev\FigCookies\FigRequestCookies;
110109

111-
$cookie = FigCookies::getRequestCookie($request, 'theme');
112-
$cookie = FigCookies::getRequestCookie($request, 'theme', 'default-theme');
110+
$cookie = FigRequestCookies::get($request, 'theme');
111+
$cookie = FigRequestCookies::get($request, 'theme', 'default-theme');
113112
```
114113

115-
#### Set a Cookie on a Request
114+
#### Set a Request Cookie
116115

117-
The `setRequestCookie` will either add or replace an existing cookie.
116+
The `set` method will either add a cookie or replace an existing cookie.
118117

119-
The `Cookie` primitive is used here for consistency with how
120-
`setResponseSetCookie` is called.
118+
The `Cookie` primitive is used as the second argument.
121119

122120
```php
123-
use Dflydev\FigCookies\FigCookie;
121+
use Dflydev\FigCookies\FigRequestCookies;
124122

125-
$request = FigCookies::setRequestCookie($request, Cookie::create('theme', 'blue'));
123+
$request = FigRequestCookies::set($request, Cookie::create('theme', 'blue'));
126124
```
127125

128-
#### Modify a Cookie on a Request
126+
#### Modify a Request Cookie
129127

130-
Sometimes the current value of a cookie needs to be known before it can be set.
131-
`modifyRequestCookie` accepts a call back that accepts a Cookie instance that
132-
represents the current cookie on the request and is expected to return an
133-
instance of Cookie that should be set on the request.
128+
The `modify' method allows for replacing the contents of a cookie based on the
129+
current cookie with the specified name. The third argument is a `callable` that
130+
takes a `Cookie` instance as its first argument and is expected to return a
131+
`Cookie` instance.
132+
133+
If no cookie by the specified name exists, a new `Cookie` instance with a
134+
`null` value will be passed to the callable.
134135

135136
```php
136-
use Dflydev\FigCookies\FigCookie;
137+
use Dflydev\FigCookies\FigRequestCookies;
137138

138139
$modify = function (Cookie $cookie) {
139140
$value = $cookie->getValue();
@@ -145,60 +146,70 @@ $modify = function (Cookie $cookie) {
145146
return $cookie->withValue($value);
146147
}
147148

148-
$request = FigCookies::modifyRequestCookie($request, 'theme', $modify);
149+
$request = FigRequestCookies::modify($request, 'theme', $modify);
149150
```
150151

151-
#### Remove a Cookie from a Request
152+
#### Remove a Request Cookie
152153

153-
The `removeRequestCookie` removes a cookie from the request if it exists.
154+
The `remove` method removes a cookie if it exists.
154155

155156
```php
156-
use Dflydev\FigCookies\FigCookie;
157+
use Dflydev\FigCookies\FigRequestCookies;
157158

158-
$request = FigCookies::removeRequestCookie($request, 'theme');
159+
$request = FigRequestCookies::remove($request, 'theme');
159160
```
160161

161162
### Response Cookies
162163

163-
#### Get a SetCookie from a Response
164+
Responses include cookie information in the **Set-Cookie** response header. The
165+
cookies in these headers are represented by the `SetCookie` class.
166+
167+
To easily work with response cookies, use the `FigResponseCookies` facade.
168+
169+
#### Get a Response Cookie
164170

165-
The `getResponseSetCookie` method will return a `SetCookie` instance that
166-
represents a cookie that exists in the response's headers. If no cookie by the
167-
specified name exists, the `SetCookie` instance will have a `null` value.
171+
The `get` method will return a `SetCookie` instance. If no cookie by the
172+
specified name exists, the returned `SetCookie` instance will have a `null`
173+
value.
168174

169-
The optional third parameter to `getResponseSetCookie` sets the value that
170-
should be used if a cookie does not exist.
175+
The optional third parameter to `get` sets the value that should be used if a
176+
cookie does not exist.
171177

172178
```php
173-
use Dflydev\FigCookies\FigCookie;
179+
use Dflydev\FigCookies\FigResponseCookies;
174180

175-
$setCookie = FigCookies::getResponseSetCookie($response, 'theme');
176-
$setCookie = FigCookies::getResponseSetCookie($response, 'theme', 'simple');
181+
$setCookie = FigResponseCookies::get($response, 'theme');
182+
$setCookie = FigResponseCookies::get($response, 'theme', 'simple');
177183
```
178184

179-
#### Set a SetCookie on a Response
185+
#### Set a Response Cookie
180186

181-
The `setResponseSetCookie` will either add or replace an existing cookie.
187+
The `set` method will either add a cookie or replace an existing cookie.
188+
189+
The `SetCookie` primitive is used as the second argument.
182190

183191
```php
184-
use Dflydev\FigCookies\FigCookie;
192+
use Dflydev\FigCookies\FigResponseCookies;
185193

186-
$response = FigCookies::setResponseSetCookie($response, SetCookie::create('token')
194+
$response = FigResponseCookies::set($response, SetCookie::create('token')
187195
->withValue('a9s87dfz978a9')
188196
->withDomain('example.com')
189197
->withPath('/firewall')
190198
);
191199
```
192200

193-
#### Modify a SetCookie on a Response
201+
#### Modify a Response Cookie
202+
203+
The `modify' method allows for replacing the contents of a cookie based on the
204+
current cookie with the specified name. The third argument is a `callable that
205+
takes a `SetCookie` instance as its first argument and is expected to return a
206+
`SetCookie` instance.
194207

195-
Sometimes the current value of a cookie needs to be known before it can be set.
196-
`modifyResponseSetCookie` accepts a call back that accepts a SetCookie instance
197-
that represents the current cookie on the response and is expected to return an
198-
instance of SetCookie that should be set on the response.
208+
If no cookie by the specified name exists, a new `SetCookie` instance with a
209+
`null` value will be passed to the callable.
199210

200211
```php
201-
use Dflydev\FigCookies\FigCookie;
212+
use Dflydev\FigCookies\FigResponseCookies;
202213

203214
$modify = function (SetCookie $setCookie) {
204215
$value = $setCookie->getValue();
@@ -213,17 +224,17 @@ $modify = function (SetCookie $setCookie) {
213224
;
214225
}
215226

216-
$response = FigCookies::modifyResponseSetCookie($response, 'theme', $modify);
227+
$response = FigResponseCookies::modify($response, 'theme', $modify);
217228
```
218229

219-
#### Remove a SetCookie from a Response
230+
#### Remove a Response Cookie
220231

221-
The `removeResponseSetCookie` removes a cookie from the response if it exists.
232+
The `remove` method removes a cookie from the response if it exists.
222233

223234
```php
224-
use Dflydev\FigCookies\FigCookie;
235+
use Dflydev\FigCookies\FigResponseCookies;
225236

226-
$response = FigCookies::removeResponseSetCookie($response, 'theme');
237+
$response = FigResponseCookies::remove($response, 'theme');
227238
```
228239

229240

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"extra": {
3232
"branch-alias": {
33-
"dev-master": "0.0.x-dev"
33+
"dev-master": "1.0.x-dev"
3434
}
3535
}
3636
}

src/Dflydev/FigCookies/FigCookies.php

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)