Builder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PHPExiftool\ClassUtils;
  11. /**
  12. * Build and write Tag classes
  13. *
  14. * @author Romain Neutron - imprec@gmail.com
  15. * @license http://opensource.org/licenses/MIT MIT
  16. */
  17. class Builder
  18. {
  19. protected $license = '/*
  20. * This file is part of the PHPExifTool package.
  21. *
  22. * (c) Alchemy <support@alchemy.fr>
  23. *
  24. * For the full copyright and license information, please view the LICENSE
  25. * file that was distributed with this source code.
  26. */';
  27. protected $namespace;
  28. protected $classname;
  29. protected $properties;
  30. protected $extends;
  31. protected $uses;
  32. protected $classAnnotations;
  33. public function __construct($namespace, $classname, array $properties, $extends = null, Array $uses = array(), Array $classAnnotations = array())
  34. {
  35. $namespace = trim($namespace, '\\');
  36. foreach (explode('\\', $namespace) as $piece) {
  37. if ($piece == '') {
  38. continue;
  39. }
  40. if ( ! $this->checkPHPVarName($piece)) {
  41. throw new \Exception(sprintf('Invalid namespace %s', $namespace));
  42. }
  43. }
  44. if ( ! $this->checkPHPVarName($classname)) {
  45. throw new \Exception(sprintf('Invalid namespace %s', $namespace));
  46. }
  47. $this->namespace = trim('PHPExiftool\\Driver\\' . $namespace, '\\');
  48. $this->classname = $classname;
  49. $this->properties = $properties;
  50. $this->extends = $extends;
  51. $this->uses = $uses;
  52. $this->classAnnotations = $classAnnotations;
  53. return $this;
  54. }
  55. public function getNamespace()
  56. {
  57. return $this->namespace;
  58. }
  59. public function getClassname()
  60. {
  61. return $this->classname;
  62. }
  63. public function getProperty($property)
  64. {
  65. return isset($this->properties[$property]) ? $this->properties[$property] : null;
  66. }
  67. public function setProperty($property, $value)
  68. {
  69. $this->properties[$property] = $value;
  70. }
  71. public function getPathfile()
  72. {
  73. return __DIR__ . '/../../'
  74. . str_replace('\\', '/', $this->namespace) . "/"
  75. . $this->classname . '.php';
  76. }
  77. public function write($force = false)
  78. {
  79. if ( ! $force && file_exists($this->getPathfile()))
  80. throw new \Exception(sprintf('%s already exists', $this->getPathfile()));
  81. if (file_exists($this->getPathfile())) {
  82. unlink($this->getPathfile());
  83. }
  84. file_put_contents($this->getPathfile(), $this->generateContent());
  85. return $this;
  86. }
  87. public function generateContent()
  88. {
  89. $content = "<?php\n\n<license>\n\nnamespace <namespace>;\n\n";
  90. foreach ($this->uses as $use) {
  91. $content .= "use " . ltrim($use, "\\") . ";\n";
  92. }
  93. if ($this->uses) {
  94. $content .= "\n";
  95. }
  96. if ($this->classAnnotations) {
  97. $content .= "/**\n";
  98. foreach ($this->classAnnotations as $annotation) {
  99. $content .= " * " . $annotation . "\n";
  100. }
  101. $content .= " */\n";
  102. }
  103. $content .= "class <classname>";
  104. if ($this->extends) {
  105. $content .= " extends <extends>";
  106. }
  107. $content .= "\n{\n";
  108. $content .= $this->generateClassProperties($this->properties);
  109. $content .= "\n}\n";
  110. if ( ! is_dir(dirname($this->getPathfile()))) {
  111. mkdir(dirname($this->getPathfile()), 0754, true);
  112. }
  113. $content = str_replace(
  114. array('<license>', '<namespace>', '<classname>', '<spaces>', '<extends>')
  115. , array($this->license, $this->namespace, $this->classname, ' ', $this->extends)
  116. , $content
  117. );
  118. return $content;
  119. }
  120. protected function generateClassProperties(array $properties, $depth = 0)
  121. {
  122. $buffer = "";
  123. foreach ($properties as $key => $value) {
  124. if (is_array($value)) {
  125. $val = "array(\n" . $this->generateClassProperties($value, $depth + 1);
  126. for ($i = 0; $i != $depth; $i ++) {
  127. $val .= "<spaces>";
  128. }
  129. $val .= "<spaces>)";
  130. } else {
  131. $val = $this->quote($value);
  132. }
  133. if ($depth == 0) {
  134. $buffer .= "\n<spaces>protected \$$key = $val;\n";
  135. } else {
  136. for ($i = 0; $i != $depth; $i ++) {
  137. $buffer .= "<spaces>";
  138. }
  139. $buffer .= "<spaces>" . $this->quote($key) . " => " . $val . ",\n";
  140. }
  141. }
  142. return $buffer;
  143. }
  144. protected function checkPHPVarName($var)
  145. {
  146. return preg_match('/^[a-zA-Z]+[a-zA-Z0-9]*$/', $var);
  147. }
  148. protected function quote($value)
  149. {
  150. if (ctype_digit(trim($value))) {
  151. $data = strval(intval($value));
  152. // Do not use PHP_INT_MAX as 32/64 bit dependant
  153. if ($data <= -2147483648 || $data >= 2147483647) {
  154. return "'" . $value . "'";
  155. }
  156. return $data;
  157. }
  158. if (in_array(strtolower($value), array('true', 'false'))) {
  159. return strtolower($value);
  160. }
  161. return "'" . str_replace(array('\\', '\''), array('\\\\', '\\\''), $value) . "'";
  162. }
  163. }