Browse Source

Use ORM for CurrentConditions class

master
Chimo 2 years ago
parent
commit
4ba2551e6f
4 changed files with 26 additions and 30 deletions
  1. +1
    -24
      private/src/class/CurrentConditions.php
  2. +10
    -5
      private/src/class/Site.php
  3. +15
    -1
      private/src/scripts/create_table.php
  4. BIN
      private/weather.sql.dump

+ 1
- 24
private/src/class/CurrentConditions.php View File

@ -1,29 +1,6 @@
<?php
class CurrentConditions {
private $condition;
private $humidity; // percent
private $temperature; // celcius
public function __construct($condition, $temperature, $humidity) {
$this->condition = $condition;
$this->temperature = $temperature;
$this->humidity = $humidity;
}
public function getCondition() {
return $this->condition;
}
public function getTemperature() {
return $this->temperature;
}
public function getHumidity() {
return $this->humidity;
}
class CurrentConditions extends Model {
public function toJSON() {
$data = [
'humidity' => $this->humidity,


+ 10
- 5
private/src/class/Site.php View File

@ -57,13 +57,18 @@ class Site extends Model {
public function parseCurrentConditions($xmlstr) {
$data = new SimpleXMLElement($xmlstr);
$currentConditions = $data->currentConditions;
$_currentConditions = $data->currentConditions;
$condition = (string)$currentConditions->condition;
$temperature = (string)$currentConditions->temperature;
$humidity = (string)$currentConditions->relativeHumidity;
$condition = (string)$_currentConditions->condition;
$temperature = (string)$_currentConditions->temperature;
$humidity = (string)$_currentConditions->relativeHumidity;
return new CurrentConditions($condition, $temperature, $humidity);
$currentConditions = CurrentConditions::create();
$currentConditions->condition = $condition;
$currentConditions->temperature = $temperature;
$currentConditions->humidity = $humidity;
return $currentConditions;
}
}

+ 15
- 1
private/src/scripts/create_table.php View File

@ -22,9 +22,23 @@ $dbh = new PDO('pgsql:dbname=' . $config['dbName'], $config['dbUser'], $config['
// Throw exception on error
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create table
// Create site table
$dbh->query($query);
// Create index
$dbh->query('CREATE INDEX idx_site_location ON site USING GIST(location)');
$query = <<<SQL
CREATE TABLE current_conditions(
id serial primary key,
site_id integer REFERENCES site(id),
humidity varchar(12),
condition varchar(191),
temperature varchar(12),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
SQL;
// Create 'current_conditions' table
$dbh->query($query);

BIN
private/weather.sql.dump View File


Loading…
Cancel
Save