Completed
Push — master ( 13abb0...106f43 )
by mehdi
02:24
created

DayOf::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 15
rs 9.4286
cc 1
eloc 7
nc 1
nop 2
1
<?php namespace Datium\Tools;
2
3
/**
4
 * Define Day of current date.
5
 */
6
class DayOf {
7
8
  protected $date_time;
9
10
  protected $month;
11
12
  protected $day;
13
14
  protected $config;
15
16
  protected $result;
17
18
  protected $calendar_type;
19
20
  protected $geregorian_DayofWeek;
21
22
  public function __construct( $date_time, $calendar_type = 'gregorian' ) {
23
24
    $this->config = include( 'Config.php' );
25
26
    $this->date_time = $date_time;
27
28
    $this->calendar_type = $calendar_type;
29
30
    $this->day = $this->date_time->format('l');
31
32
    $this->geregorian_DayofWeek = $this->date_time->format('w');
33
34
    return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
35
36
  }
37
38
  /**
39
   * Which day of year is current day.
40
   * @since Aug, 03 2015
41
   * @return integer
42
   */
43
  public function year() {
44
45
    $config = include(  'CalendarSettings/' . ucfirst( $this->calendar_type ) . '.php' );
46
47
    return $config[ 'day_of_year' ]( $this->date_time );
48
49
  }
50
51
52
  /**
53
   * Which day of week is current day.
54
   * @since Aug, 09 2015
55
   * @return integer
56
   */
57
  public function week() {
58
59
    switch ( $this->calendar_type ) {
60
61
      case 'ir':
62
63
      $this->result = $this->persian_day_of_week();
64
65
        break;
66
67
      case 'gh':
68
69
      $this->result = $this->islamic_day_of_week();
70
71
       break;
72
73
      case 'gr':
74
75
      $this->result = date( 'w', strtotime( $this->date_time->format('Y-m-d H:i:s') ) ) + 1;
76
77
       break;
78
    }
79
80
    return $this->result;
81
82
  }
83
84
  /**
85
   * @since Sept, 14 2015
86
   * @return integer
87
   */
88 View Code Duplication
  protected function persian_day_of_week() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
90
    $this->day = str_replace( $this->config['week_days_name']['english'], $this->config['week_days_name']['persian'], $this->day);
91
92
    foreach ( $this->config['week_days_name']['persian'] as $key => $value ) {
93
94
      if( $value == $this->day ) return $key += 1;
95
96
    }
97
98
  }
99
100
    /**
101
   * @since Sept, 14 2015
102
   * @return integer
103
   */
104 View Code Duplication
  protected function islamic_day_of_week() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
106
    $this->day = str_replace( $this->config['week_days_name']['english'], $this->config['week_days_name']['islamic'][$this->geregorian_DayofWeek], $this->day);
107
108
    foreach ( $this->config['week_days_name']['islamic'] as $key => $value ) {
109
110
      if( $value == $this->day ) return $key += 1;
111
112
    }
113
114
115
  }
116
117
118
}
119
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
120