aboutsummaryrefslogtreecommitdiffstats
path: root/boston_food_trucks.php
blob: 37bb4f9c97c49ab3f25fd4f6f301dd2bb3eb0078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

class BostonFoodTrucks {
	
	private $food_truck_schedule_url = 'http://www.cityofboston.gov/business/mobile/schedule-app.asp?v=1&71';
	private $food_truck_html = '';
	private $dom = null;
	private $finder = null;
	
	private $class_company = 'com';
	private $class_day_of_the_week = 'dow';
	private $class_time_of_day = 'tod';
	private $class_location = 'loc';
	
	private $food_trucks_nodes = array(
		'companies' => null,
		'company_urls' => null,
		'days_of_week' => null,
		'times_of_day' => null,
		'locations' => null,
	);
	
	public $food_truck_output = array(
    	'food_trucks' => array()
	);
	
	public $locations_downtown = array(
		'(25) Innovation District, Seaport Blvd at Thomson',
		'(23) Chinatown/Park Street, Boylston St near Washington St',
		'Rose Kennedy Greenway, Beach Street in Chinatown',
		'Rose Kennedy Greenway, Dewey Square, South Station',
		'Greenway (at High St #1)',
		'Rose Kennedy Greenway,Congress Street near Wharf',
		'Greenway - Rose Kennedy Greenway, Milk Street by Aquarium',
		'Rose Kennedy Greenway, Milk Street by Aquarium',
		'Rose Kennedy Greenway @ State Street',
		'Rose Kennedy Greenway, Cross and Hanover Streets',
		'Boston Common, Brewer Fountain by Tremont and Boylston Streets',
		'(24) Financial District, Pearl Street at Franklin',
		'(22) Financial District, Milk and Kilby Streets',
		'Downtown - City Hall Plaza, Fisher Park',
        'City Hall Plaza, Fisher Park'
	);
	
	
	public function __construct () {
		$this->food_truck_html = file_get_contents($this->food_truck_schedule_url);
		$this->dom = new DOMDocument;
		$this->dom->loadHTML($this->food_truck_html);
		$this->finder = new DomXPath($this->dom);
		
		$this->food_truck_nodes['companies'] = 
			$this->finder->query('//td[@class="' . $this->class_company . '"]/a/text()');
		$this->food_truck_nodes['company_urls'] = 
			$this->finder->query('//td[@class="' . $this->class_company . '"]/a/@href');
		$this->food_truck_nodes['days_of_week'] = 
			$this->finder->query('//td[@class="' . $this->class_day_of_the_week . '"]/text()');
		$this->food_truck_nodes['times_of_day'] = 
			$this->finder->query('//td[@class="' . $this->class_time_of_day . '"]/text()');
		$this->food_truck_nodes['locations'] = 
			$this->finder->query('//td[@class="' . $this->class_location . '"]/text()');	
	}
	
	
	public function schedule ($options = array()) {
		$available_options = array(
			'trucks' => isset($options['trucks']),
			'days_of_week' => isset($options['days_of_week']),
			'times_of_day' => isset($options['times_of_day']),
			'locations' => isset($options['locations'])
		);
		$has_days = isset($options['days_of_week']);
		$has_times = isset($options['times_of_day']);
		$has_locations = isset($options['locations']);
		
		// Copy set options into their own array
		$set_options = array();
		foreach ($available_options as $key => $value) {
			if ($value) {
				$set_options[$key] = $value;
			}
		}
		
		// Go through all food trucks
		for ($i = 0; $i < $this->food_truck_nodes['locations']->length; $i++) {
			$matches = 0;
			
			// Perform filters on this food truck
			foreach ($set_options as $key => $value) {
				if ($value and call_user_func_array(array($this, $key), array($i, $options[$key]))) {
					$matches++;
				}
			}
			
			// If all filters pass, add this truck to the list
			if ($matches == sizeof($set_options)) {
				$this->_add_food_truck($i);
			}
		}
		
		
		return json_encode($this->food_truck_output);
	}
	
	
	// Filtering functions
	private function filter ($list_name, $list_index, $array) {
		return in_array($this->food_truck_nodes[$list_name]->item($list_index)->nodeValue, $array);
	}
	
	
	private function trucks ($list_index, $array) {
		return $this->filter('trucks', $list_index, $array);
	}
	
	
	private function days_of_week ($list_index, $array) {
		return $this->filter('days_of_week', $list_index, $array);
	}
	
	
	private function times_of_day ($list_index, $array) {
		return $this->filter('times_of_day', $list_index, $array);
	}
	
	
	private function locations ($list_index, $array) {
		return $this->filter('locations', $list_index, $array);
	}
	
	
	// Add food truck to the final list
	private function _add_food_truck ($list_index) {
		$this->food_truck_output['food_trucks'][] = array(
			'company' => $this->food_truck_nodes['companies']->item($list_index)->nodeValue,
			'company_url' => $this->food_truck_nodes['company_urls']->item($list_index)->nodeValue,
			'day_of_week' => $this->food_truck_nodes['days_of_week']->item($list_index)->nodeValue,
			'time_of_day' => $this->food_truck_nodes['times_of_day']->item($list_index)->nodeValue,
			'location' => $this->food_truck_nodes['locations']->item($list_index)->nodeValue
		);
	}
	
	
	// Show lunch trucks today
	public function trucks_now () {
		return $this->schedule(array(
			'days_of_week' => array(date('l')),
			'times_of_day' => array('Lunch')
		));
	}
}