/lib/MwbExporter/Core/Model/Base.php

https://github.com/jupeter/mysql-workbench-schema-exporter · PHP · 176 lines · 77 code · 21 blank · 78 comment · 4 complexity · 0bc48cf1b6bb8fae0ba2bfbfdc4ec803 MD5 · raw file

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright (c) 2010 Johannes Mueller <circus2(at)web.de>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace MwbExporter\Core\Model;
  26. abstract class Base
  27. {
  28. protected $id;
  29. protected $attributes;
  30. protected $data;
  31. protected $parent;
  32. public function __construct($data, Base $parent)
  33. {
  34. $this->preLoad();
  35. $this->attributes = $data->attributes();
  36. $this->data = $data;
  37. $this->id = (string) $this->attributes['id'];
  38. $this->parent = $parent;
  39. $this->postLoad();
  40. $this->init();
  41. }
  42. /**
  43. * Return the internal ID of the MySQL Workbench object
  44. *
  45. * @return string
  46. */
  47. public function getId()
  48. {
  49. return $this->id;
  50. }
  51. /**
  52. * Returns the attributes of the current MySQL Workbench object
  53. *
  54. * @return SimpleXmlElement
  55. */
  56. public function getAttributes()
  57. {
  58. return $this->attributes;
  59. }
  60. /**
  61. * Returns an attribute by $key or null
  62. *
  63. * @param String $key
  64. * @return String
  65. */
  66. public function getAttribute($key=null)
  67. {
  68. return isset($this->config[$key]) ? $this->config[$key] : null;
  69. }
  70. /**
  71. * Returns current MySQL Workbench object
  72. *
  73. * @return SimpleXmlElement
  74. */
  75. public function getData()
  76. {
  77. return $this->data;
  78. }
  79. /**
  80. * Returns the comment of the current MySQL Workbench object
  81. *
  82. * @return string
  83. */
  84. public function getComment()
  85. {
  86. return isset($this->config['comment']) ? trim($this->config['comment']) : '';
  87. }
  88. /**
  89. * Filters given comment for embedded code by a given keyword
  90. *
  91. * @param string $needle_raw
  92. * @param string $comment
  93. * @return string
  94. */
  95. protected function parseComment($needle_raw, $comment=null)
  96. {
  97. if($comment === null){
  98. $comment = $this->getComment();
  99. }
  100. $needle_quoted = preg_quote($needle_raw);
  101. $pattern = '@\{(d|doctrine):' . $needle_quoted . '\}(.+)\{\/(d|doctrine):' . $needle_quoted . '\}@si';
  102. preg_match($pattern, $comment, $matches);
  103. return isset($matches[2]) ? $matches[2] : false;
  104. }
  105. /**
  106. * Returns XML of the current MySQL Workbench object
  107. *
  108. * @return string
  109. */
  110. public function debug()
  111. {
  112. return $this->data->asXML();
  113. }
  114. /**
  115. * Returns spaces for Yaml by a given indentation level
  116. *
  117. * @param int $level
  118. * @return string
  119. */
  120. protected function indentation($level = 1)
  121. {
  122. $config = \MwbExporter\Core\Registry::get('config');
  123. if(isset($config['indentation']) && $config['indentation']){
  124. $indentation = $config['indentation'];
  125. } else {
  126. $indentation = 2;
  127. }
  128. return str_repeat(' ', $indentation * $level);
  129. }
  130. /**
  131. * Returns the parent object
  132. *
  133. * @return object
  134. */
  135. public function getParent()
  136. {
  137. return $this->parent;
  138. }
  139. /**
  140. * Returns object by MySQL Workbench object ID
  141. *
  142. * @param string $id
  143. * @return object
  144. */
  145. protected function getElementById($id)
  146. {
  147. return \MwbExporter\Core\Registry::get($id);
  148. }
  149. public function preLoad()
  150. {}
  151. public function postLoad()
  152. {}
  153. public function init()
  154. {}
  155. }