File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?
2+
3+ namespace TgUtils ;
4+
5+ class DateRange {
6+
7+ protected $ from ;
8+ protected $ until ;
9+
10+ public function __construct ($ from = NULL , $ until = NULL ) {
11+ $ this ->from = $ from ;
12+ $ this ->until = $ until ;
13+ }
14+
15+ public function setFrom ($ value ) {
16+ $ this ->from = $ value ;
17+ $ this ->checkRange ();
18+ return $ this ;
19+ }
20+
21+ public function getFrom () {
22+ return $ this ->from ;
23+ }
24+
25+ public function setUntil ($ value ) {
26+ $ this ->until = $ value ;
27+ $ this ->checkRange ();
28+ return $ this ;
29+ }
30+
31+ public function getUntil () {
32+ return $ this ->until ;
33+ }
34+
35+ public function set ($ from , $ until ) {
36+ $ this ->from = $ from ;
37+ $ this ->until = $ until ;
38+ $ this ->checkRange ();
39+ return $ this ;
40+ }
41+
42+ protected function checkRange () {
43+ if (($ this ->from != NULL ) && ($ this ->until != NULL )) {
44+ if ($ this ->from ->toUnix () > $ this ->until ->toUnix ()) {
45+ $ foo = $ this ->until ;
46+ $ this ->until = $ this ->from ;
47+ $ this ->from = $ foo ;
48+ }
49+ }
50+ }
51+
52+ public function __toString () {
53+ $ rc = ($ this ->from != NULL ) ? $ this ->from ->toMySql (TRUE ) : 'N/A ' ;
54+ $ rc .= ' - ' ;
55+ $ rc .= ($ this ->until != NULL ) ? $ this ->until ->toMySql (TRUE ) : 'N/A ' ;
56+ return $ rc ;
57+ }
58+ }
59+
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace TgUtils ;
4+
5+ class Stack {
6+
7+ protected $ stack ;
8+
9+ public function __construct (...$ values ) {
10+ $ this ->stack = array ();
11+ foreach ($ values AS $ value ) {
12+ $ this ->push ($ value );
13+ }
14+ }
15+
16+ public function isEmpty () {
17+ return count ($ this ->stack ) == 0 ;
18+ }
19+
20+ public function size () {
21+ return count ($ this ->stack );
22+ }
23+
24+ public function push ($ value ) {
25+ if ($ value != NULL ) array_push ($ this ->stack , $ value );
26+ else throw new \Exception ('Cannot push NULL element to stack ' );
27+ }
28+
29+ public function pop () {
30+ if (!$ this ->isEmpty ()) {
31+ return array_pop ($ this ->stack );
32+ }
33+ return NULL ;
34+ }
35+
36+ public function peek () {
37+ if (!$ this ->isEmpty ()) {
38+ return $ this ->stack [count ($ this ->stack )-1 ];
39+ }
40+ return NULL ;
41+ }
42+
43+ /** Returns the stack elements with top element first */
44+ public function elements ($ reverse = FALSE ) {
45+ return $ reverse ? $ this ->stack : array_reverse ($ this ->stack );
46+ }
47+
48+ }
49+
You can’t perform that action at this time.
0 commit comments