Skip to content

Commit 3237ec2

Browse files
authored
Adding interface for Message (#20)
* Adding interface for Message * Make message immutable * minor * cs * Added tests * Added changelog * Removed withKey * fixed tests * fixed tests
1 parent 6d6b78d commit 3237ec2

File tree

5 files changed

+195
-75
lines changed

5 files changed

+195
-75
lines changed

Changelog.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5-
## UNRELEASED
5+
## 0.3.0
6+
7+
### Added
8+
9+
- `MessageInterface`
10+
11+
### Changed
12+
13+
- The `Message` is now immutable. Replaced "setters" with "withers".
14+
- `Storage::create()` and `Storage::update()` has updated signatures to use the`MessageInterface`.
15+
16+
## 0.2.3
17+
18+
### Added
19+
20+
- Support for Symfony 4
21+
22+
## 0.2.2
23+
24+
### Changed
25+
26+
- Documentation change on interface.
627

728
## 0.2.1
829

src/Model/Message.php

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111

1212
namespace Translation\Common\Model;
1313

14-
/**
15-
* A object representation of a translation in a specific language.
16-
*
17-
* @author Tobias Nyholm <[email protected]>
18-
*/
19-
final class Message
14+
final class Message implements MessageInterface
2015
{
2116
/**
2217
* @var string
@@ -60,7 +55,7 @@ final class Message
6055
* @param string $translation
6156
* @param array $meta
6257
*/
63-
public function __construct($key = '', $domain = '', $locale = '', $translation = '', array $meta = [])
58+
public function __construct($key, $domain = '', $locale = '', $translation = '', array $meta = [])
6459
{
6560
$this->key = $key;
6661
$this->domain = $domain;
@@ -70,129 +65,109 @@ public function __construct($key = '', $domain = '', $locale = '', $translation
7065
}
7166

7267
/**
73-
* @return string
68+
* {@inheritdoc}
7469
*/
7570
public function getDomain()
7671
{
7772
return $this->domain;
7873
}
7974

8075
/**
81-
* @param string $domain
82-
*
83-
* @return Message
76+
* {@inheritdoc}
8477
*/
85-
public function setDomain($domain)
78+
public function withDomain($domain)
8679
{
87-
$this->domain = $domain;
80+
$new = clone $this;
81+
$new->domain = $domain;
8882

89-
return $this;
83+
return $new;
9084
}
9185

9286
/**
93-
* @return string
87+
* {@inheritdoc}
9488
*/
9589
public function getKey()
9690
{
9791
return $this->key;
9892
}
9993

10094
/**
101-
* @param string $key
102-
*
103-
* @return Message
104-
*/
105-
public function setKey($key)
106-
{
107-
$this->key = $key;
108-
109-
return $this;
110-
}
111-
112-
/**
113-
* @return string
95+
* {@inheritdoc}
11496
*/
11597
public function getLocale()
11698
{
11799
return $this->locale;
118100
}
119101

120102
/**
121-
* @param string $locale
122-
*
123-
* @return Message
103+
* {@inheritdoc}
124104
*/
125-
public function setLocale($locale)
105+
public function withLocale($locale)
126106
{
127-
$this->locale = $locale;
107+
$new = clone $this;
108+
$new->locale = $locale;
128109

129-
return $this;
110+
return $new;
130111
}
131112

132113
/**
133-
* @return string
114+
* {@inheritdoc}
134115
*/
135116
public function getTranslation()
136117
{
137118
return $this->translation;
138119
}
139120

140121
/**
141-
* @param string $translation
142-
*
143-
* @return Message
122+
* {@inheritdoc}
144123
*/
145-
public function setTranslation($translation)
124+
public function withTranslation($translation)
146125
{
147-
$this->translation = $translation;
126+
$new = clone $this;
127+
$new->translation = $translation;
148128

149-
return $this;
129+
return $new;
150130
}
151131

152132
/**
153-
* @return array
133+
* {@inheritdoc}
154134
*/
155135
public function getAllMeta()
156136
{
157137
return $this->meta;
158138
}
159139

160140
/**
161-
* @param array $meta
162-
*
163-
* @return Message
141+
* {@inheritdoc}
164142
*/
165-
public function setMeta(array $meta)
143+
public function withMeta(array $meta)
166144
{
167-
$this->meta = $meta;
145+
$new = clone $this;
146+
$new->meta = $meta;
168147

169-
return $this;
148+
return $new;
170149
}
171150

172151
/**
173-
* @param string $key
174-
* @param string $value
175-
*
176-
* @return $this
152+
* {@inheritdoc}
177153
*/
178-
public function addMeta($key, $value)
154+
public function withAddedMeta($key, $value)
179155
{
180-
$this->meta[$key] = $value;
156+
$new = clone $this;
157+
$new->meta[$key] = $value;
181158

182-
return $this;
159+
return $new;
183160
}
184161

185162
/**
186-
* @param string $key
187-
*
188-
* @return mixed|null
163+
* {@inheritdoc}
189164
*/
190-
public function getMeta($key)
165+
public function getMeta($key, $default = null)
191166
{
192-
if (isset($this->meta[$key])) {
167+
if (array_key_exists($key, $this->meta)) {
193168
return $this->meta[$key];
194169
}
195170

196-
return;
171+
return $default;
197172
}
198173
}

src/Model/MessageInterface.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Common\Model;
13+
14+
/**
15+
* A object representation of a translation in a specific language.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
interface MessageInterface
20+
{
21+
/**
22+
* @return string
23+
*/
24+
public function getDomain();
25+
26+
/**
27+
* This method MUST be implemented in such a way as to retain the
28+
* immutability of the message, and MUST return an instance that has the
29+
* changed request target.
30+
*
31+
* @param string $domain
32+
*
33+
* @return static
34+
*/
35+
public function withDomain($domain);
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getKey();
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getLocale();
46+
47+
/**
48+
* This method MUST be implemented in such a way as to retain the
49+
* immutability of the message, and MUST return an instance that has the
50+
* changed request target.
51+
*
52+
* @param string $locale
53+
*
54+
* @return static
55+
*/
56+
public function withLocale($locale);
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getTranslation();
62+
63+
/**
64+
* This method MUST be implemented in such a way as to retain the
65+
* immutability of the message, and MUST return an instance that has the
66+
* changed request target.
67+
*
68+
* @param string $translation
69+
*
70+
* @return static
71+
*/
72+
public function withTranslation($translation);
73+
74+
/**
75+
* @return array
76+
*/
77+
public function getAllMeta();
78+
79+
/**
80+
* This method MUST be implemented in such a way as to retain the
81+
* immutability of the message, and MUST return an instance that has the
82+
* changed request target.
83+
*
84+
* @param array $meta
85+
*
86+
* @return static
87+
*/
88+
public function withMeta(array $meta);
89+
90+
/**
91+
* This method MUST be implemented in such a way as to retain the
92+
* immutability of the message, and MUST return an instance that has the
93+
* changed request target.
94+
*
95+
* @param string $key
96+
* @param string $value
97+
*
98+
* @return static
99+
*/
100+
public function withAddedMeta($key, $value);
101+
102+
/**
103+
* @param string $key
104+
* @param mixed|null $default
105+
*
106+
* @return mixed|null
107+
*/
108+
public function getMeta($key, $default = null);
109+
}

src/Storage.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Translation\Common;
1313

14-
use Translation\Common\Model\Message;
14+
use Translation\Common\Model\MessageInterface;
1515

1616
/**
1717
* The storage is a place when you can store your translations. A database, filesystem
@@ -28,24 +28,24 @@ interface Storage
2828
* @param string $domain
2929
* @param string $key
3030
*
31-
* @return Message
31+
* @return MessageInterface
3232
*/
3333
public function get($locale, $domain, $key);
3434

3535
/**
3636
* Create a new translation or asset. If a translation already exist this function
3737
* will do nothing.
3838
*
39-
* @param Message $message
39+
* @param MessageInterface $message
4040
*/
41-
public function create(Message $message);
41+
public function create(MessageInterface $message);
4242

4343
/**
4444
* Update a translation. Creates a translation if there is none to update.
4545
*
46-
* @param Message $message
46+
* @param MessageInterface $message
4747
*/
48-
public function update(Message $message);
48+
public function update(MessageInterface $message);
4949

5050
/**
5151
* Remove a translation from the storage. If the storage implementation makes

0 commit comments

Comments
 (0)