1: <?php
2:
3: App::import('Lib', 'Base.ArrayUtil');
4:
5: class Basics {
6:
7: 8: 9: 10: 11: 12:
13: public function parseUrl($url) {
14: if (!is_array($url)) {
15: $url = Router::parse($url);
16: }
17: foreach (array('named', 'pass') as $urlKey) {
18: if (!empty($url[$urlKey])) {
19: foreach ($url[$urlKey] as $key => $value) {
20: $url[$key] = $value;
21: }
22: }
23: unset($url[$urlKey]);
24: }
25: return $url;
26: }
27:
28: 29: 30: 31: 32: 33: 34:
35: public static function fieldValue($data, $field, $defaultModel) {
36: return ArrayUtil::arrayIndex($data, self::fieldPath($field, $defaultModel));
37: }
38:
39: 40: 41: 42: 43: 44:
45: public static function fieldPath($field, $defaultModel) {
46: $fieldPath = explode('.', $field);
47:
48: if (count($fieldPath) == 1 && $defaultModel) {
49: $fieldPath = array_merge(array($defaultModel), $fieldPath);
50: }
51: return $fieldPath;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public static function fieldFullName($field, $defaultModel) {
61: return implode('.', self::fieldPath($field, $defaultModel));
62: }
63:
64: 65: 66: 67: 68:
69: public static function fieldNameToArray($fieldName) {
70: return explode('.', $fieldName);
71: }
72:
73: public static function lcd($n, $m, $maxvarianzpercent = 0) {
74:
75: 76: 77: 78:
79:
80:
81: $d = $n / $m;
82: $f = 1;
83: while ($d * $f != intval($d * $f)) {
84: $f++;
85: }
86: $r = ($d * $f) . '/' . $f;
87: if (($d * $f) <= 10 or $f <= 10)
88: return $r;
89: else if ($maxvarianzpercent > 0) {
90: $f = 1;
91: while ($d * $f != intval($d * $f) and ($d * $f) - intval($d * $f) > $maxvarianzpercent / 100) {
92: $f++;
93: }
94: return intval($d * $f) . '/' . $f;
95: } else
96: return $r;
97: }
98:
99: public static function multibyteTrim($string) {
100: $string = preg_replace("/(^\s+)|(\s+$)/us", "", $string);
101:
102: return $string;
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: public static function saveModelOrThrowException($model, $data) {
113: $model = self::_getModel($model);
114:
115: if (empty($data[$model->alias][$model->primaryKey])) {
116: $model->create();
117: }
118:
119: if (!$model->save($data)) {
120: $validationErrors = $model->validationErrors;
121: $alias = $model->alias;
122: throw new Exception("Failed to save {$model->name}: " . print_r(compact('data', 'validationErrors', 'alias'), true));
123: } else {
124: return $model->read();
125: }
126: }
127:
128: public static function deleteModelOrThrowException($model, $data) {
129: $model = self::_getModel($model);
130: if (!$model->delete($data[$model->alias][$model->primaryKey])) {
131: $alias = $model->alias;
132: throw new Exception("Failed to delete {$model->name}: " . print_r(compact('data', 'alias'), true));
133: }
134: }
135:
136: 137: 138: 139: 140: 141:
142: private static function _getModel($model) {
143: if ($model instanceof Model) {
144: return $model;
145: } else if (is_string($model)) {
146: $modelName = $model;
147: $model = ClassRegistry::init($modelName);
148: if ($model instanceof Model) {
149: return $model;
150: } else {
151: throw new Exception("\"$modelName\" is not a Model's name");
152: }
153: } else {
154: throw new Exception("Parameter \$model is not a Model neither a string name");
155: }
156: }
157:
158: }
159:
160: ?>
161: