TextExport.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class TextExport {
  3. public $mime = 'text/plain';
  4. public $extension = 'txt';
  5. public $description = 'Fichier de texte brut';
  6. public function sample($dataset,$level = 0,$parent=''){
  7. $stream = '';
  8. $parent = ($parent!=''?$parent.'.':'');
  9. $indentation = str_repeat("\t", $level);
  10. foreach($dataset as $macro => $infos){
  11. $infos['type'] = isset($infos['type']) ? $infos['type'] : '';
  12. switch($infos['type']){
  13. case 'list':
  14. $stream .= $indentation.'-'.$parent.$macro.' '.(isset($infos['label'])?': '.$infos['label']:'').' (liste)'.PHP_EOL;
  15. $stream .= $indentation.'{{#'.$parent.$macro.'}}'.PHP_EOL;
  16. if(is_array($infos['value']) && isset($infos['value'][0])) $stream .= self::sample($infos['value'][0],$level+1);
  17. $stream .=$indentation.'{{/'.$parent.$macro.'}}';
  18. break;
  19. case 'object':
  20. $stream .= $indentation.'-'.$parent.$macro.' '.(isset($infos['label'])?': '.$infos['label']:'').PHP_EOL;
  21. $stream .= self::sample($infos['value'],$level+1,$parent.$macro);
  22. break;
  23. case 'image': break;
  24. default :
  25. $stream .= $indentation.'{{'.$parent.$macro.'}} : '.( !isset($infos['label']) ? '': $infos['label']).PHP_EOL;
  26. break;
  27. }
  28. }
  29. return $stream;
  30. }
  31. //permet aux exports enfant de surcharger la méthode pour convertir des valeurs en fct de leurs type sur un export particulier
  32. // (ex: convertion des flux images en base64 sur les fichiers html)
  33. public function formatValue($type,$value){
  34. if($type == 'image') return '';
  35. return $value;
  36. }
  37. public function decomposeKey($datas,$key){
  38. if(array_key_exists($key, $datas)) return isset($datas[$key]) ? $datas[$key] : '';
  39. $attributes = explode('.',$key);
  40. $current = $datas;
  41. $value = null;
  42. foreach ($attributes as $attribute) {
  43. if(!array_key_exists($attribute, $current)) break;
  44. $current = $current[$attribute];
  45. $value = isset($current) ? $current : '';
  46. }
  47. return $value;
  48. }
  49. public function from_template($stream, $datas){
  50. //if / loop
  51. $loopIfRegex = '/\{\{\#([^\/\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  52. $stream = preg_replace_callback($loopIfRegex,function($matches) use ($datas) {
  53. $key = $matches[1];
  54. $streamTpl = $matches[2];
  55. $keyInfos = explode('::',$key);
  56. $key = $keyInfos[0];
  57. $type = isset($keyInfos[1]) ? $keyInfos[1] : 'string';
  58. $value = $this->decomposeKey($datas,$key);
  59. //gestion des boucles
  60. if(is_array($value)){
  61. $stream = '';
  62. foreach($value as $line){
  63. $localData = is_array($line) ? array_merge($datas,$line) : $datas;
  64. $stream .= $this->from_template($streamTpl,$localData);
  65. }
  66. return $stream;
  67. //gestion des if
  68. }else{
  69. return !isset($value) || (!is_array($value) && empty($value)) || (is_array($value) && count($value)==0) ? '' : $this->from_template($streamTpl,$datas);
  70. }
  71. },$stream);
  72. //gestion des else
  73. $elseRegex = '/\{\{\^([^\/\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
  74. $stream = preg_replace_callback($elseRegex,function($matches) use ($datas) {
  75. $key = $matches[1];
  76. $streamTpl = $matches[2];
  77. $keyInfos = explode('::',$key);
  78. $key = $keyInfos[0];
  79. $type = isset($keyInfos[1]) ? $keyInfos[1] : 'string';
  80. $value = $this->decomposeKey($datas,$key);
  81. if(is_array($value)){
  82. $stream = '';
  83. foreach($value as $line){
  84. $localData = is_array($line) ? array_merge($datas,$line) : $datas;
  85. $stream .= $this->from_template($streamTpl,$localData);
  86. }
  87. return $stream;
  88. //gestion des else
  89. }else{
  90. return !isset($value) || (!is_array($value) && empty($value)) || (is_array($value) && count($value)==0) ? $this->from_template($streamTpl,$datas) : '';
  91. }
  92. },$stream);
  93. //gestion des simples variables
  94. $stream = preg_replace_callback('/{{([^#\/}]*)}}/',function($matches) use ($datas) {
  95. $key = $matches[1];
  96. $keyInfos = explode('::',$key);
  97. $key = $keyInfos[0];
  98. $type = isset($keyInfos[1]) ? $keyInfos[1] : 'string';
  99. $value = $this->decomposeKey($datas,$key);
  100. if(!isset($value)) return $this->formatValue($type,$matches[0]);
  101. if(is_array($value)) return 'Array';
  102. return $this->formatValue($type,$value);
  103. },$stream);
  104. return $stream;
  105. }
  106. }