class AnnotationWrapper {
-
+ /**
+ * @param $string
+ * @return AnnotationReturn
+ */
public function wrap($string){
- if (preg_match("#{{pastell:now}}#",$string)){
- $return = new AnnotationReturn();
- $return->type = AnnotationReturn::STRING;
- $return->string = date("c");
- return $return;
- }
- if (preg_match("#{{pastell:string:(.*)}}#",$string,$matches)){
- $return = new AnnotationReturn();
- $return->type = AnnotationReturn::STRING;
- $return->string = $matches[1];
- return $return;
- }
- if (preg_match("#{{pastell:integrity}}#",$string,$matches)){
- $return = new AnnotationReturn();
- $return->type = AnnotationReturn::XML_REPLACE;
- $return->string = "<Integrity><Contains algorithme=\"http://www.w3.org/2001/04/xmlenc#sha256\">5f330f324ee15d9a043381750c29a2fb484ef10803ef20d26a8dc5e50a22a77c</Contains><UnitIdentifier>pes_aller.xml</UnitIdentifier></Integrity><Integrity><Contains algorithme=\"http://www.w3.org/2001/04/xmlenc#sha256\">3101c90cebbe6136c118e369c839aa452ae1893056eb7cd84ef4420b596642b5</Contains><UnitIdentifier>pes_acquit.xml</UnitIdentifier></Integrity>";
- return $return;
+ preg_match_all("#{{pastell:([^:]*):?((?:(?!}}).)*)}}#",$string,$matches);
+
+ $return = $this->getAnnotationReturn(AnnotationReturn::EMPTY_RETURN,false);
+
+ foreach($matches[0] as $i=>$one_match) {
+ $command = $matches[1][$i];
+ $data = $matches[2][$i];
+
+ $function = "{$command}Command";
+
+ if (!method_exists($this, $function)) {
+ throw new Exception("La commande « $command » est inconnu sur ce Pastell");
+ }
+
+ /** @var AnnotationReturn $return_i */
+ $return_i = $this->$function($data);
+ $return->type = $return_i->type;
+ $return->string .= $return_i->string;
}
+ return $return;
+ }
+
+ protected function getAnnotationReturn($type,$data){
$return = new AnnotationReturn();
- $return->type = AnnotationReturn::EMPTY_RETURN;
- $return->string = "";
+ $return->type = $type;
+ $return->string = $data;
return $return;
}
+ protected function nowCommand(){
+ return $this->getAnnotationReturn(AnnotationReturn::STRING, date('c'));
+ }
+
+ protected function stringCommand($data){
+ return $this->getAnnotationReturn(AnnotationReturn::STRING, $data);
+ }
+
+ protected function integrityCommand(){
+ $data = "<Integrity><Contains algorithme=\"http://www.w3.org/2001/04/xmlenc#sha256\">5f330f324ee15d9a043381750c29a2fb484ef10803ef20d26a8dc5e50a22a77c</Contains><UnitIdentifier>pes_aller.xml</UnitIdentifier></Integrity><Integrity><Contains algorithme=\"http://www.w3.org/2001/04/xmlenc#sha256\">3101c90cebbe6136c118e369c839aa452ae1893056eb7cd84ef4420b596642b5</Contains><UnitIdentifier>pes_acquit.xml</UnitIdentifier></Integrity>";
+ return $this->getAnnotationReturn(AnnotationReturn::XML_REPLACE, $data);
+ }
+
}
\ No newline at end of file
require_once (__DIR__."/../connecteur/seda-ng/SedaNG.class.php");
-require_once __DIR__."/../connecteur/seda-ng/lib/GenerateXMLFromRelaxNg.class.php";
-require_once __DIR__."/../connecteur/seda-ng/lib/GenerateXMLFromAnnotedRelaxNg.class.php";
-require_once __DIR__."/../connecteur/seda-ng/lib/SedaValidation.class.php";
-require_once __DIR__."/../connecteur/seda-ng/lib/AnnotationWrapper.class.php";
-require_once __DIR__."/../connecteur/seda-ng/lib/RelaxNgImportAgapeAnnotation.class.php";
-require_once __DIR__."/../connecteur/seda-ng/lib/GenerateBordereauSEDA.class.php";
$this->assertEquals($expeted_output,$this->annotationWrapper->wrap($input)->string);
}
+
+ public function testCommandNotExist(){
+ $this->setExpectedException("Exception","La commande « toto » est inconnu sur ce Pastell");
+ $this->assertAnnotation("","{{pastell:toto}}");
+ }
+
public function testWrap(){
$this->assertAnnotation("","totot");
}
$this->assertAnnotation("Archive transfert","{{pastell:string:Archive transfert}}");
}
+ public function testWrapOneClosingBracket(){
+ $this->assertAnnotation("{toto}toto", "{{pastell:string:{toto}toto}}");
+ }
+
+ public function testWrapOneClosingBracketAtEnd(){
+ $this->assertAnnotation("{toto", "{{pastell:string:{toto}}}");
+ }
+
+ public function testDoubleWrap(){
+ $this->assertAnnotation("pimpoum","{{pastell:string:pim}}-pam-{{{pastell:string:poum}}");
+ }
+
public function testDateNow(){
$date = $this->annotationWrapper->wrap("{{pastell:now}}")->string;
$this->assertRegExp("#^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}#",$date); //2012-04-04T16:05:30Z
}
+ public function testIntegrity(){
+ $integrity = $this->annotationWrapper->wrap("{{pastell:integrity}}")->string;
+ $xml = simplexml_load_string("<element>".$integrity."</element>");
+ $this->assertEquals(
+ "http://www.w3.org/2001/04/xmlenc#sha256",
+ (string) $xml->{'Integrity'}[0]->{'Contains'}['algorithme']
+ );
+ }
+
+
+
}
\ No newline at end of file