Refactor databox_field :: name generation

This commit is contained in:
Romain Neutron
2012-01-20 15:53:18 +01:00
parent 22559b0474
commit 7d98dc7f27
3 changed files with 22 additions and 13 deletions

View File

@@ -75,7 +75,7 @@ class databox_descriptionStructure implements IteratorAggregate
/** /**
* *
* @param <type> $name * @param int $id
* @return databox_field * @return databox_field
*/ */
public function get_element($id) public function get_element($id)
@@ -93,6 +93,8 @@ class databox_descriptionStructure implements IteratorAggregate
*/ */
public function get_element_by_name($name) public function get_element_by_name($name)
{ {
$name = databox_field::generateName($name);
if (isset($this->cache_name_id[$name])) if (isset($this->cache_name_id[$name]))
return $this->elements[$this->cache_name_id[$name]]; return $this->elements[$this->cache_name_id[$name]];

View File

@@ -411,10 +411,7 @@ class databox_field implements cache_cacheableInterface
*/ */
public function set_name($name) public function set_name($name)
{ {
$unicode_processor = new unicode(); $this->name = self::generateName($name);
$name = $unicode_processor->remove_nonazAZ09($name, false, false);
$name = $unicode_processor->remove_first_digits($name);
$this->name = $name;
return $this; return $this;
} }
@@ -852,11 +849,6 @@ class databox_field implements cache_cacheableInterface
{ {
$sorter = 0; $sorter = 0;
$unicode_processor = new unicode();
$name = $unicode_processor->remove_nonazAZ09($name, false, false);
$name = $unicode_processor->remove_first_digits($name);
$sql = 'SELECT (MAX(sorter) + 1) as sorter FROM metadatas_structure'; $sql = 'SELECT (MAX(sorter) + 1) as sorter FROM metadatas_structure';
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
@@ -875,7 +867,7 @@ class databox_field implements cache_cacheableInterface
1, :sorter)"; 1, :sorter)";
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$stmt->execute(array(':name' => $name, ':sorter' => $sorter)); $stmt->execute(array(':name' => self::generateName($name), ':sorter' => $sorter));
$id = $databox->get_connection()->lastInsertId(); $id = $databox->get_connection()->lastInsertId();
$stmt->closeCursor(); $stmt->closeCursor();
@@ -884,6 +876,16 @@ class databox_field implements cache_cacheableInterface
return self::get_instance($databox, $id); return self::get_instance($databox, $id);
} }
public static function generateName($name)
{
$unicode_processor = new unicode();
$name = $unicode_processor->remove_nonazAZ09($name, false, false);
return $unicode_processor->remove_first_digits($name);
}
/** /**
* *
* @return array * @return array

View File

@@ -25,8 +25,13 @@ class databox_fieldTest extends PhraseanetPHPUnitAbstract
public function setUp() public function setUp()
{ {
$this->databox = self::$record_1->get_databox(); $this->databox = self::$record_1->get_databox();
$this->name = 'Field test'; $this->name = 'Field Test';
$this->object = databox_field::create($this->databox, $this->name);
$this->object = $this->databox->get_meta_structure()->get_element_by_name($this->name);
if(!$this->object instanceof databox_field)
$this->object = databox_field::create($this->databox, $this->name);
$this->id = $this->object->get_id(); $this->id = $this->object->get_id();
} }