2018-08-06 12:30:51 +00:00
|
|
|
<?php
|
|
|
|
|
2018-09-10 13:09:10 +00:00
|
|
|
namespace App\Models;
|
2018-08-06 12:30:51 +00:00
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-09-10 13:09:10 +00:00
|
|
|
use App\Models\Dataset;
|
2019-02-14 14:09:11 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2018-08-06 12:30:51 +00:00
|
|
|
|
|
|
|
class XmlCache extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The database table used by the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'document_xml_cache';
|
2019-02-14 14:09:11 +00:00
|
|
|
public $timestamps = false;
|
2018-08-06 12:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* primaryKey
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
* @access protected
|
|
|
|
*/
|
2019-02-14 14:09:11 +00:00
|
|
|
//protected $primaryKey = null;
|
|
|
|
public $primaryKey = 'document_id';
|
2018-08-06 12:30:51 +00:00
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = ['document_id', 'xml_version', 'server_date_modified', 'xml_data'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the dataset that owns the xml cache.
|
|
|
|
*/
|
|
|
|
public function dataset()
|
|
|
|
{
|
2018-09-10 13:09:10 +00:00
|
|
|
return $this->belongsTo(Dataset::class, 'document_id', 'id');
|
2018-08-06 12:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get dom document of 'xml_data' string
|
|
|
|
*
|
|
|
|
* @return \DOMDocument
|
|
|
|
*/
|
|
|
|
public function getDomDocument()
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument('1.0', 'utf-8');
|
|
|
|
$xmlData = $this->xml_data;
|
|
|
|
$dom->loadXML($xmlData);
|
|
|
|
return $dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a document in a specific xml version is already cached or not.
|
|
|
|
*
|
|
|
|
* @param mixed $datasetId
|
|
|
|
* @param mixed $xmlVersion
|
|
|
|
* @param mixed $serverDateModified
|
|
|
|
* @return bool Returns true on cached hit else false.
|
|
|
|
*/
|
2019-02-14 14:09:11 +00:00
|
|
|
public function hasValidEntry($datasetId, $serverDateModified)
|
|
|
|
{
|
|
|
|
$select = DB::table('document_xml_cache');
|
|
|
|
$select->where('document_id', '=', $datasetId)
|
|
|
|
->where('server_date_modified', '=', $serverDateModified);
|
|
|
|
|
|
|
|
$row = $select->first();
|
2018-08-06 12:30:51 +00:00
|
|
|
|
2019-02-14 14:09:11 +00:00
|
|
|
if (null === $row) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 12:30:51 +00:00
|
|
|
}
|