/traits/Data.php

https://gitlab.com/nitm/yii2-module · PHP · 161 lines · 109 code · 30 blank · 22 comment · 17 complexity · 85f17e6baeee264a6b7b122a1f4e7b6e MD5 · raw file

  1. <?php
  2. namespace nitm\traits;
  3. use yii\helpers\Inflector;
  4. /**
  5. * Traits defined for expanding query scopes until yii2 resolves traits issue.
  6. */
  7. trait Data
  8. {
  9. public $queryOptions = [];
  10. public static $slugIs = [];
  11. protected static $is;
  12. protected static $tableName;
  13. protected static $flags;
  14. public static function tableName()
  15. {
  16. if (!static::$tableName) {
  17. $class = parent::class;
  18. if (method_exists($class, 'tableName')) {
  19. return $class::tableName();
  20. }
  21. }
  22. return static::$tableName;
  23. }
  24. public static function setIs($is)
  25. {
  26. static::$is = $is;
  27. }
  28. public static function isWhat(...$arguments)
  29. {
  30. return static::getIsA(...$arguments);
  31. }
  32. public function getIs(...$arguments)
  33. {
  34. return static::isWhat(...$arguments);
  35. }
  36. /*
  37. * What does this claim to be?
  38. * @param bollean|null $pluralize Should the returnvalue be pluralized or singularized.
  39. * When set to null nothing is done
  40. * @param boolean $forceClassType resolution Don't check for type if this is set to type
  41. */
  42. public static function getIsA($pluralize = null, $forceClassType = false)
  43. {
  44. $slugify = function ($value) {
  45. $stack = explode('\\', $value);
  46. return Inflector::slug(implode(' ', preg_split('/(?=[A-Z])/', array_pop($stack), -1, PREG_SPLIT_NO_EMPTY)));
  47. };
  48. $class = static::class ;
  49. if (!$forceClassType && method_exists($class, 'type') && !empty($class::type())) {
  50. $ret_val = $class::type();
  51. } else {
  52. $ret_val = $class;
  53. }
  54. if (is_null($pluralize)) {
  55. $inflector = 'slug';
  56. } else {
  57. $inflector = $pluralize === true ? 'pluralize' : 'singularize';
  58. }
  59. if (!isset($class::$slugIs[$inflector][$ret_val])) {
  60. $ret_val = $class::$slugIs[$inflector][$ret_val] = Inflector::$inflector($slugify($ret_val));
  61. } else {
  62. $ret_val = $class::$slugIs[$inflector][$ret_val];
  63. }
  64. //If we didn't set the inflector then set the $is value to the return value
  65. if (is_null($pluralize) && !isset($class::$is)) {
  66. $class::$is = $ret_val;
  67. }
  68. return $ret_val;
  69. }
  70. /**
  71. * Get the underlying class.
  72. *
  73. * @return string The class this model really represents
  74. */
  75. public function getMorphClass()
  76. {
  77. return static::class;
  78. }
  79. public function title()
  80. {
  81. return $this->{$this->titleAttribute};
  82. }
  83. public function getTitleAttribute()
  84. {
  85. return 'title';
  86. }
  87. /**
  88. * Merge the fields and extraFields for the specified class.
  89. *
  90. * @param string|null $modelClass The class to get all fields for. If null then use the current class name
  91. *
  92. * @return array An array containing the original fields, extra fields and merged fields
  93. */
  94. public function allFields(string $modelClass = null)
  95. {
  96. $modelClass = $modelClass ?: static::className();
  97. $fields = array_map(function ($k, $v) {
  98. if (is_int($k)) {
  99. return $v;
  100. }
  101. return $k;
  102. }, array_keys($modelClass::fields()), $modelClass::fields());
  103. $extraFields = array_map(function ($k, $v) {
  104. if (is_int($k)) {
  105. return $v;
  106. }
  107. return $k;
  108. }, array_keys($modelClass::extraFields()), $modelClass::extraFields());
  109. return [$fields, $extraFields, array_unique(array_merge($fields, $extraFields))];
  110. }
  111. public function onScenario($scenario = null)
  112. {
  113. return [];
  114. }
  115. public function hasRelation($name, $model = null)
  116. {
  117. $ret_val = null;
  118. $model = is_null($model) ? $this : $model;
  119. $method = 'get'.$name;
  120. try {
  121. if ($model->hasMethod($method) && (new \ReflectionMethod($model, $method))->isPublic()) {
  122. $ret_val = call_user_func([$model, $method]);
  123. if (!($ret_val instanceof \yii\db\ActiveQuery)) {
  124. $ret_val = null;
  125. }
  126. }
  127. } catch (\Exception $e) {
  128. }
  129. return $ret_val;
  130. }
  131. }