From a137bc7cf76dc778970e5336cec46e1a38fa4608 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Wed, 17 Apr 2013 15:02:17 +0200 Subject: [PATCH] Add databox_field::toArray method --- lib/classes/databox/field.php | 38 +++++++++++++++++++++ tests/classes/databox/databox_fieldTest.php | 30 ++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/lib/classes/databox/field.php b/lib/classes/databox/field.php index e0eae70ce4..f699bb90f6 100644 --- a/lib/classes/databox/field.php +++ b/lib/classes/databox/field.php @@ -15,6 +15,7 @@ use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface; use Alchemy\Phrasea\Metadata\Tag\Nosource; use PHPExiftool\Driver\TagInterface; use PHPExiftool\Driver\TagFactory; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * @@ -178,6 +179,10 @@ class databox_field implements cache_cacheableInterface $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); + if (!$row) { + throw new NotFoundHttpException(sprintf('Unable to find field in %s', $id)); + } + $this->id = (int) $id; $this->tag = self::loadClassFromTagName($row['src']); @@ -796,6 +801,39 @@ class databox_field implements cache_cacheableInterface return $this->on_error; } + public function toArray() + { + return array( + 'id' => $this->id, + 'name' => $this->name, + 'tag' => $this->tag->getTagname(), + 'business' => $this->Business, + 'type' => $this->type, + 'thumbtitle' => $this->thumbtitle, + 'tbranch' => $this->tbranch, + 'separator' => $this->separator, + 'required' => $this->required, + 'report' => $this->report, + 'readonly' => $this->readonly, + 'multi' => $this->multi, + 'indexable' => $this->indexable, + 'dces-element' => $this->dces_element, + 'vocabulary-name' => $this->Vocabulary ? $this->Vocabulary->getName() : null, + 'vocabulary-restricted' => $this->VocabularyRestriction, + ); + } + + /** + * + * @param \Alchemy\Phrasea\Application $app + * @param databox $databox + * @param type $name + * @param type $multi + * + * @return databox_field + * + * @throws \Exception_InvalidArgument + */ public static function create(Application $app, databox $databox, $name, $multi) { $sorter = 0; diff --git a/tests/classes/databox/databox_fieldTest.php b/tests/classes/databox/databox_fieldTest.php index 5f1a931ecf..837b3d66e5 100644 --- a/tests/classes/databox/databox_fieldTest.php +++ b/tests/classes/databox/databox_fieldTest.php @@ -355,4 +355,34 @@ class databox_fieldTest extends PhraseanetPHPUnitAbstract $this->assertEquals($value->getValue(), $AddedValue); } + public function testToArray() + { + foreach (array($this->object_mono, $this->object_multi) as $object) { + $data = $object->toArray(); + + $this->assertInternalType('array', $data); + $this->assertInternalType('integer', $data['id']); + $this->assertInternalType('string', $data['name']); + $this->assertInternalType('string', $data['tag']); + $this->assertInternalType('boolean', $data['business']); + $this->assertInternalType('string', $data['type']); + if (!is_null($data['thumbtitle'])) { + $this->assertInternalType('string', $data['thumbtitle']); + } + $this->assertInternalType('string', $data['tbranch']); + $this->assertInternalType('string', $data['separator']); + $this->assertInternalType('boolean', $data['required']); + $this->assertInternalType('boolean', $data['report']); + $this->assertInternalType('boolean', $data['readonly']); + $this->assertInternalType('boolean', $data['multi']); + $this->assertInternalType('boolean', $data['indexable']); + if (!is_null($data['dces-element'])) { + $this->assertInternalType('', $data['dces-element']); + } + if (!is_null($data['vocabulary-name'])) { + $this->assertInternalType('string', $data['vocabulary-name']); + } + $this->assertInternalType('boolean', $data['vocabulary-restricted']); + } + } }