Refactor caption serialization

This commit is contained in:
Romain Neutron
2012-03-20 15:42:59 +01:00
parent a10fb04999
commit 583ee314b4
6 changed files with 280 additions and 19 deletions

View File

@@ -53,6 +53,97 @@ class caption_record implements caption_interface, cache_cacheableInterface
return $this;
}
const SERIALIZE_XML = 'xml';
const SERIALIZE_YAML = 'yaml';
public function serialize($format)
{
switch ($format)
{
case self::SERIALIZE_XML:
return $this->serializeXML();
break;
case self::SERIALIZE_YAML:
return $this->serializeYAML();
break;
default:
throw new \Exception(sprintf('Unknown format %s', $format));
break;
}
}
protected function serializeYAML()
{
$buffer = array();
foreach ($this->get_fields() as $field)
{
$vi = $field->get_values();
if ($field->is_multi())
{
$buffer[$field->get_name()] = array();
foreach ($vi as $value)
{
$val = $value->getValue();
$buffer[$field->get_name()][] = ctype_digit($val) ? (int) $val : $val;
}
}
else
{
$value = array_pop($vi);
$val = $value->getValue();
$buffer[$field->get_name()] = ctype_digit($val) ? (int) $val : $val;
}
}
$buffer = array('record' => array('description' => $buffer));
$dumper = new Symfony\Component\Yaml\Dumper();
return $dumper->dump($buffer, 3);
}
protected function serializeXML()
{
$dom_doc = new DOMDocument('1.0', 'UTF-8');
$dom_doc->formatOutput = true;
$dom_doc->standalone = true;
$record = $dom_doc->createElement('record');
$record->setAttribute('record_id', $this->record->get_record_id());
$dom_doc->appendChild($record);
$description = $dom_doc->createElement('description');
$record->appendChild($description);
foreach ($this->get_fields() as $field)
{
$values = $field->get_values();
foreach ($values as $value)
{
$elem = $dom_doc->createElement($field->get_name());
$elem->appendChild($dom_doc->createTextNode($value->getValue()));
$elem->setAttribute('meta_id', $value->getId());
$elem->setAttribute('meta_struct_id', $field->get_meta_struct_id());
$description->appendChild($elem);
}
}
$doc = $dom_doc->createElement('doc');
$tc_datas = $this->record->get_technical_infos();
foreach ($tc_datas as $key => $data)
{
$doc->setAttribute($key, $data);
}
$record->appendChild($doc);
return $dom_doc->saveXML();
}
protected function retrieve_fields()
{
if (is_array($this->fields))
@@ -83,7 +174,7 @@ class caption_record implements caption_interface, cache_cacheableInterface
try
{
$databox_meta_struct = databox_field::get_instance($this->databox, $row['structure_id']);
$metadata = new caption_field($databox_meta_struct, $this->record);
$metadata = new caption_field($databox_meta_struct, $this->record);
$rec_fields[$databox_meta_struct->get_id()] = $metadata;
$dces_element = $metadata->get_databox_field()->get_dces_element();
@@ -133,7 +224,6 @@ class caption_record implements caption_interface, cache_cacheableInterface
foreach ($this->retrieve_fields() as $meta_struct_id => $field)
{
if ($field->get_name() == $fieldname)
return $field;
}
@@ -206,7 +296,7 @@ class caption_record implements caption_interface, cache_cacheableInterface
foreach ($fields as $key => $value)
{
if(!isset($fields[$key]))
if (!isset($fields[$key]))
continue;
//if(strpos($fields[$key]['value'], '<a ') === false)

View File

@@ -222,7 +222,7 @@ class record_exportElement extends record_adapter
}
}
$xml = $this->get_xml();
$xml = $this->get_caption()->serialize(caption_record::SERIALIZE_XML);
if ($xml)
{

View File

@@ -470,7 +470,9 @@ class task_period_ftp extends task_appboxAbstract
{
$sbas_id = phrasea::sbasFromBas($base_id);
$record = new record_adapter($sbas_id, $record_id);
$sdcaption = $record->get_xml();
$sdcaption = $record->get_caption()->serialize(caption_record::SERIALIZE_XML);
$remotefile = $file["filename"];
if ($subdef == 'caption')

View File

@@ -1325,7 +1325,7 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase
}
if (self::$record_1 instanceof record_adapter)
{
self::$record_1->delete();
// self::$record_1->delete();
self::$record_1 = null;
}
if (self::$record_2 instanceof record_adapter)

View File

@@ -0,0 +1,182 @@
<?php
require_once __DIR__ . '/../PhraseanetPHPUnitAbstract.class.inc';
/**
* Test class for caption_record.
* Generated by PHPUnit on 2012-03-20 at 15:12:31.
*/
class caption_recordTest extends PhraseanetPHPUnitAbstract
{
/**
* @var caption_record
*/
protected $object;
protected static $need_records = 1;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
public function setUp()
{
$this->object = new caption_record(self::$record_1, self::$record_1->get_databox());
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testSerialize().
*/
public function testSerializeXML()
{
foreach (self::$record_1->get_databox()->get_meta_structure() as $databox_field)
{
$n = $databox_field->is_multi() ? 3 : 1;
for ($i = 0; $i < $n; $i++)
{
\caption_Field_Value::create($databox_field, self::$record_1, \random::generatePassword());
}
}
$xml = $this->object->serialize(\caption_record::SERIALIZE_XML);
$sxe = simplexml_load_string($xml);
$this->assertInstanceOf('SimpleXMLElement', $sxe);
foreach (self::$record_1->get_caption()->get_fields() as $field)
{
if($field->get_databox_field()->is_multi())
{
$tagname = $field->get_name();
$retrieved = array();
foreach($sxe->description->$tagname as $value)
{
$retrieved[] = (string) $value;
}
$values = $field->get_values();
$this->assertEquals(count($values), count($retrieved));
foreach($values as $val)
{
$this->assertTrue(in_array($val->getValue(), $retrieved));
}
}
else
{
$tagname = $field->get_name();
$value = array_pop($field->get_values());
$this->assertEquals($value->getValue(), (string) $sxe->description->$tagname);
}
}
}
public function testSerializeYAML()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_fields().
*/
public function testGet_fields()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_field().
*/
public function testGet_field()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_dc_field().
*/
public function testGet_dc_field()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_highlight_fields().
*/
public function testGet_highlight_fields()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_cache_key().
*/
public function testGet_cache_key()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGet_data_from_cache().
*/
public function testGet_data_from_cache()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testSet_data_to_cache().
*/
public function testSet_data_to_cache()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testDelete_data_from_cache().
*/
public function testDelete_data_from_cache()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
?>

View File

@@ -246,19 +246,6 @@ class record_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract
$this->assertTrue((static::$record_1->get_caption() instanceof caption_record));
}
public function testGet_xml()
{
$xml = self::$record_1->get_xml();
$sxe = simplexml_load_string($xml);
$this->assertInstanceOf('SimpleXMLElement', $sxe);
foreach (self::$record_1->get_caption()->get_fields() as $field)
{
$tagname = $field->get_name();
$this->assertEquals($field->get_serialized_values(), (string) $sxe->description->$tagname);
}
}
public function testGet_original_name()
{
$this->assertTrue(static::$record_1->get_original_name() === self::$record_sf_1->getFilename());