File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -25,4 +25,10 @@ function it_calculates_a_difference_between_two_dates(Translator $translator)
25
25
26
26
$ this ->inWords ();
27
27
}
28
+
29
+ function it_could_be_constructed_with_language_code ()
30
+ {
31
+ $ this ->beConstructedWithTranslation (new DateTime ('-1 minute ' ), 'nl ' );
32
+ $ this ->inWords ()->shouldReturn ('1 minuut geleden ' );
33
+ }
28
34
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Technodelight ;
4
4
5
+ use Technodelight \TimeAgo \TranslationLoader ;
5
6
use Technodelight \TimeAgo \Translator ;
6
- use \DateTime ;
7
7
8
8
class TimeAgo
9
9
{
@@ -16,15 +16,38 @@ class TimeAgo
16
16
*/
17
17
private $ translator ;
18
18
19
- public function __construct (DateTime $ dateTime , Translator $ translator = null )
19
+ /**
20
+ * @param DateTime $dateTime
21
+ * @param Translator|null $translator
22
+ */
23
+ public function __construct (\DateTime $ dateTime , Translator $ translator = null )
20
24
{
21
25
$ this ->dateTime = $ dateTime ;
22
26
$ this ->translator = $ translator ?: new Translator ;
23
27
}
24
28
25
- public function inWords (DateTime $ now = null )
29
+ /**
30
+ * Instantiate TimeAgo with the desired built-in translation
31
+ *
32
+ * @param DateTime $dateTime
33
+ * @param string $languageCode
34
+ *
35
+ * @return TimeAgo
36
+ */
37
+ public static function withTranslation (\DateTime $ dateTime , $ languageCode )
38
+ {
39
+ $ translationLoader = new TranslationLoader ;
40
+ return new self ($ dateTime , new Translator ($ translationLoader ->load ($ languageCode )));
41
+ }
42
+
43
+ /**
44
+ * @param DateTime|null $now accepts a reference date
45
+ *
46
+ * @return string
47
+ */
48
+ public function inWords (\DateTime $ now = null )
26
49
{
27
- $ now = $ now ?: new DateTime ;
50
+ $ now = $ now ?: new \ DateTime ;
28
51
return $ this ->translator ->translate (
29
52
$ now ->getTimestamp () - $ this ->dateTime ->getTimestamp ()
30
53
);
You can’t perform that action at this time.
0 commit comments