Skip to content

Commit c802acc

Browse files
Merge pull request #2 from technodelight/feature/add-static-constructor-and-tostring
Updates and fixes
2 parents 6ee42ad + 8ad5c87 commit c802acc

File tree

21 files changed

+745
-311
lines changed

21 files changed

+745
-311
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
bin/
3+
.idea/

README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
2 yrs <-> max time or date # => over [2..X] years
1717
```
1818

19-
This is the default `RuleSet` TimeAgo works with, but it could be changed depending on your needs:
19+
Above example is using the default `RuleSet`, but you can define your own when you want:
2020

2121
```
2222
$ruleSet = new Technodelight\TimeAgo\Translation\RuleSet;
23-
$ruleSet->add(new Rule('aboutOneDay', '23hour'), new Formatter('days', 'day'));
23+
$ruleSet->add(
24+
new Rule(
25+
'aboutOneDay', // rule name, which would be used as a translation key
26+
'23hour' // timespan, as a limit, without spaces between numerics and time. Check DefaultRuleSet for examples
27+
),
28+
new Formatter(
29+
'days', // duration type, according to durations in \Technodelight\TimeAgo\Translation\SecondsDurationMap
30+
'floor' // strategy (function) to use for calculating the amount. In this example "days" are calculated with floor()
31+
)
32+
);
2433
$timeAgo = new Technodelight\TimeAgo(
2534
new DateTime('-1 hour'),
2635
new Technodelight\TimeAgo\Translator(
@@ -32,32 +41,34 @@ $timeAgo = new Technodelight\TimeAgo(
3241
$timeAgo->inWords(); // => 1 day ago
3342
```
3443

35-
With the above code you can pass any of the translations supplied with this repository. All credits goes to Jimmi Westerberg and the contributors, who added this
36-
awesomeness factor.
44+
The example below shows how customisable is TimeAgo:
3745

3846
```
39-
$translationLoader = new Technodelight\TimeAgo\TranslationLoader;
47+
// with all dependencies injected:
48+
$translationLoader = new Technodelight\TimeAgo\TranslationLoader; // can load built-in translations
4049
$timeAgo = new Technodelight\TimeAgo(
41-
new DateTime('-1 hour'),
42-
new Technodelight\TimeAgo\Translator(
43-
$translationLoader->load('hu')
50+
new DateTime('-1 hour'), // static datetime
51+
new Technodelight\TimeAgo\Translator( // this only needs an array as an input
52+
$translationLoader->load('hu') // use a fixed translation
4453
)
4554
);
4655
56+
// or simply:
57+
$timeAgo = Technodelight\TimeAgo::withTranslation(new \DateTime('-1 hour'), 'hu');
58+
4759
$timeAgo->inWords(); // => körülbelül 1 órája
4860
4961
```
50-
62+
In the above example you can pass any of the translations supplied with this repository. All credits goes to Jimmi Westerberg and to the contributors of his repository.
63+
5164
By default, the `TimeAgo` uses the current system language as a guide to determine the required translation file, and it defaults to english if this information was not successfully resolved.
52-
Of course, you can configure the translation loader to define your own translation directory and use the translations from there:
65+
Of course, you can pass your own translation when required:
5366

5467
```
55-
$translationLoader = new Technodelight\TimeAgo\TranslationLoader;
56-
$translationLoader->translationDirectory('/your/own/custom/path/with/even/custom/translations/in/it');
5768
$timeAgo = new Technodelight\TimeAgo(
5869
new DateTime('-1 hour'),
5970
new Technodelight\TimeAgo\Translator(
60-
$translationLoader->load('pirate')
71+
['aboutOneHour' => 'Happened an hour ago! Arrr! Arrr!']
6172
)
6273
);
6374
@@ -72,7 +83,7 @@ $timeAgo->inWords(); // => Happened an hour ago! Arrr! Arrr!
7283

7384
The MIT License (MIT)
7485

75-
Copyright (c) 2015 Zsolt Gál
86+
Copyright (c) 2018 Zsolt Gál
7687

7788
Permission is hereby granted, free of charge, to any person obtaining a copy
7889
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)