| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Core\Tour; |
| 4 |
|
| 5 |
class TourFactory |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @param $tour_id : tour ID |
| 9 |
* |
| 10 |
* @return RegularTour |
| 11 |
*/ |
| 12 |
public function get_tour($tour_id) |
| 13 |
{ |
| 14 |
$tour_id = $this->get_tour_id($tour_id); |
| 15 |
|
| 16 |
if (!$tour_id) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
|
| 20 |
$tour_type = self::get_tour_type($tour_id); |
| 21 |
|
| 22 |
$class = self::get_tour_classname($tour_id, $tour_type); |
| 23 |
|
| 24 |
return new $class($tour_id); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
public function get_tour_id($tour_id) |
| 30 |
{ |
| 31 |
if ($tour_id instanceof \WP_Post) { |
| 32 |
return $tour_id->ID; |
| 33 |
} |
| 34 |
return absint($tour_id); |
| 35 |
} |
| 36 |
|
| 37 |
public static function get_tour_type($tour_id) |
| 38 |
{ |
| 39 |
$default = 'regular'; |
| 40 |
|
| 41 |
$tour_type = get_post_meta($tour_id, 'yatra_tour_meta_tour_type', true); |
| 42 |
|
| 43 |
if ($tour_type == '') { |
| 44 |
|
| 45 |
return $default; |
| 46 |
} |
| 47 |
|
| 48 |
$all_types = yatra_get_tour_types(); |
| 49 |
|
| 50 |
if (isset($all_types[$tour_type])) { |
| 51 |
|
| 52 |
return $tour_type; |
| 53 |
} |
| 54 |
return $default; |
| 55 |
} |
| 56 |
|
| 57 |
public static function get_tour_classname($tour_id, $tour_type) |
| 58 |
{ |
| 59 |
$classname = apply_filters('yatra_tour_class_name', self::get_classname_from_tour_type($tour_type), $tour_type, $tour_id); |
| 60 |
|
| 61 |
if (!$classname || !class_exists($classname)) { |
| 62 |
|
| 63 |
$classname = 'Yatra\\Core\\Tour\\RegularTour'; |
| 64 |
} |
| 65 |
|
| 66 |
return $classname; |
| 67 |
} |
| 68 |
|
| 69 |
public static function get_classname_from_tour_type($tour_type) |
| 70 |
{ |
| 71 |
$namespace = "Yatra\\Core\\Tour\\"; |
| 72 |
|
| 73 |
$tour_type = ucwords(str_replace('_', ' ', $tour_type)); |
| 74 |
|
| 75 |
$tour_type = preg_replace('/\s+/', '', $tour_type) . 'Tour'; |
| 76 |
|
| 77 |
return ($namespace . $tour_type); |
| 78 |
} |
| 79 |
} |