Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelAttributeToCastTypeUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero 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 Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero 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 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
51  public static function resolveValueForCast($model, $attributeName, $value)
52  {
53  $castType = self::getCastType($model, $attributeName);
54  settype($value, $castType);
55  return $value;
56  }
57 
63  public static function getCastType($model, $attributeName)
64  {
65  assert('$model instanceof RedBeanModel || $model instanceof RedBeanModels || $model instanceof ModelForm');
66  assert('is_string($attributeName) && $attributeName != ""');
67  if ($attributeName == 'id')
68  {
69  return 'int';
70  }
71  if ($model instanceof CustomField)
72  {
73  return 'string';
74  }
75  if ($model instanceof MultipleValuesCustomField)
76  {
77  return 'array';
78  }
79  $metadata = $model->getMetadata();
80  foreach ($metadata as $className => $perClassMetadata)
81  {
82  if (isset($perClassMetadata['elements'][$attributeName]))
83  {
84  $castType = self::getOperatorTypeFromModelMetadataElement($perClassMetadata['elements'][$attributeName]);
85  if ($castType == null)
86  {
87  break;
88  }
89  else
90  {
91  return $castType;
92  }
93  }
94  }
95  if ($model->isRelation($attributeName))
96  {
97  throw new NotSupportedException();
98  }
99  else
100  {
101  $validators = $model->getValidators($attributeName);
102  foreach ($validators as $validator)
103  {
104  switch(get_class($validator))
105  {
106  case 'CBooleanValidator':
107  return 'bool';
108 
109  case 'CEmailValidator':
110  return 'string';
111 
112  case 'RedBeanModelTypeValidator':
113  case 'TypeValidator':
114  switch ($validator->type)
115  {
116  case 'date':
117  return 'string';
118 
119  case 'datetime':
120  return 'string';
121 
122  case 'integer':
123  return 'int';
124 
125  case 'float':
126  return 'float';
127 
128  case 'time':
129  return 'string';
130 
131  case 'array':
132  throw new NotSupportedException();
133  }
134  break;
135 
136  case 'CUrlValidator':
137  return 'string';
138  }
139  }
140  }
141  return 'string';
142  }
143 
144  protected static function getOperatorTypeFromModelMetadataElement($element)
145  {
146  assert('is_string($element)');
147  switch ($element)
148  {
149  case 'CurrencyValue': //todo: once currency has validation rules, this can be removed.
150  return 'float';
151 
152  case 'DropDown':
153  return 'int';
154 
155  case 'MultiSelectDropDown': //tbd.
156  return 'string'; //tbd.
157 
158  case 'Phone':
159  return 'string';
160 
161  case 'RadioDropDown':
162  return 'int';
163 
164  default :
165  return null;
166  }
167  }
168  }
169 ?>
static resolveValueForCast($model, $attributeName, $value)
static getCastType($model, $attributeName)
Generated on Sat Jul 4 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.