PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/core/utils/ModelAttributeToCastTypeUtil.php

https://bitbucket.org/ddonthula/zurmofeb
PHP | 159 lines | 102 code | 16 blank | 41 comment | 10 complexity | 1a641bfe047c2bad98c3b72e0f77a8bf MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-2-Clause, GPL-3.0, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * Given a model and its attribute, find the appropriate cast.
  28. */
  29. class ModelAttributeToCastTypeUtil
  30. {
  31. /**
  32. * For a given $attributeName on a $model, set the cast of the $value based on what the castType should be for
  33. * that attribute.
  34. * @param $model
  35. * @param $attributeName
  36. * @param $value
  37. * @see ModelAttributeToCastTypeUtil::getCastType
  38. * @return casted $value
  39. */
  40. public static function resolveValueForCast($model, $attributeName, $value)
  41. {
  42. $castType = self::getCastType($model, $attributeName);
  43. settype($value, $castType);
  44. return $value;
  45. }
  46. /**
  47. * Returns the cast type that should be used with the named attribute
  48. * of the given model. If the model is a customField, it assumes some sort of dropdown and returns
  49. * 'equals'.
  50. */
  51. public static function getCastType($model, $attributeName)
  52. {
  53. assert('$model instanceof RedBeanModel || $model instanceof RedBeanModels || $model instanceof ModelForm');
  54. assert('is_string($attributeName) && $attributeName != ""');
  55. if ($attributeName == 'id')
  56. {
  57. return 'int';
  58. }
  59. if ($model instanceof CustomField)
  60. {
  61. return 'string';
  62. }
  63. if ($model instanceof MultipleValuesCustomField)
  64. {
  65. return 'array';
  66. }
  67. $metadata = $model->getMetadata();
  68. foreach ($metadata as $className => $perClassMetadata)
  69. {
  70. if (isset($perClassMetadata['elements'][$attributeName]))
  71. {
  72. $castType = self::getOperatorTypeFromModelMetadataElement($perClassMetadata['elements'][$attributeName]);
  73. if ($castType == null)
  74. {
  75. break;
  76. }
  77. else
  78. {
  79. return $castType;
  80. }
  81. }
  82. }
  83. if ($model->isRelation($attributeName))
  84. {
  85. throw new NotSupportedException();
  86. }
  87. else
  88. {
  89. $validators = $model->getValidators($attributeName);
  90. foreach ($validators as $validator)
  91. {
  92. switch(get_class($validator))
  93. {
  94. case 'CBooleanValidator':
  95. return 'bool';
  96. case 'CEmailValidator':
  97. return 'string';
  98. case 'RedBeanModelTypeValidator':
  99. case 'TypeValidator':
  100. switch ($validator->type)
  101. {
  102. case 'date':
  103. return 'string';
  104. case 'datetime':
  105. return 'string';
  106. case 'integer':
  107. return 'int';
  108. case 'float':
  109. return 'float';
  110. case 'time':
  111. return 'string';
  112. case 'array':
  113. throw new NotSupportedException();
  114. }
  115. break;
  116. case 'CUrlValidator':
  117. return 'string';
  118. }
  119. }
  120. }
  121. return 'string';
  122. }
  123. protected static function getOperatorTypeFromModelMetadataElement($element)
  124. {
  125. assert('is_string($element)');
  126. switch ($element)
  127. {
  128. case 'CurrencyValue': //todo: once currency has validation rules, this can be removed.
  129. return 'float';
  130. case 'DropDown':
  131. return 'int';
  132. case 'MultiSelectDropDown': //tbd.
  133. return 'string'; //tbd.
  134. case 'Phone':
  135. return 'string';
  136. case 'RadioDropDown':
  137. return 'int';
  138. default :
  139. return null;
  140. }
  141. }
  142. }
  143. ?>