Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelAttributeToDataProviderAdapter.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 
42  {
46  protected $modelClassName;
47 
51  protected $attribute;
52 
57  protected $relatedAttribute;
58 
63  protected $model;
64 
68  protected $castingHintAttributeModelClassName;
69 
73  protected $castingHintStartingModelClassName;
74 
75  private $relatedModel;
76 
82  public function __construct($modelClassName, $attribute, $relatedAttribute = null)
83  {
84  assert('is_string($modelClassName)');
85  assert('is_string($attribute)');
86  assert('is_string($relatedAttribute) || $relatedAttribute == null');
87  $this->modelClassName = $modelClassName;
88  $this->attribute = $attribute;
89  $this->relatedAttribute = $relatedAttribute;
90  }
91 
100  public function setCastingHintModelClassNameForAttribute($castingHintAttributeModelClassName)
101  {
102  assert('is_string($castingHintAttributeModelClassName)');
103  $this->castingHintAttributeModelClassName = $castingHintAttributeModelClassName;
104  }
105 
110  {
111  return $this->castingHintAttributeModelClassName;
112  }
113 
117  public function setCastingHintStartingModelClassName($castingHintStartingModelClassName)
118  {
119  assert('is_string($castingHintStartingModelClassName) || $castingHintStartingModelClassName == null');
120  $this->castingHintStartingModelClassName = $castingHintStartingModelClassName;
121  }
122 
129  public function getResolvedModelClassName()
130  {
131  if ($this->castingHintStartingModelClassName != null)
132  {
133  return $this->castingHintStartingModelClassName;
134  }
135  return $this->getModelClassName();
136  }
137 
138  public function getModelClassName()
139  {
140  return $this->modelClassName;
141  }
142 
143  public function getAttribute()
144  {
145  return $this->attribute;
146  }
147 
148  public function getRelatedAttribute()
149  {
150  return $this->relatedAttribute;
151  }
152 
156  public function getModelTableName()
157  {
158  $modelClassName = $this->modelClassName;
159  return $modelClassName::getTableName();
160  }
161 
166  public function getAttributeModelClassName()
167  {
168  if ($this->attribute == 'id')
169  {
170  return $this->modelClassName;
171  }
172  $modelClassName = $this->modelClassName;
173  return $modelClassName::getAttributeModelClassName($this->attribute);
174  }
175 
180  public function getAttributeTableName()
181  {
182  $attributeModelClassName = $this->getAttributeModelClassName();
183  return $attributeModelClassName::getTableName();
184  }
185 
186  public function getModel()
187  {
188  if ($this->model == null)
189  {
190  $this->model = new $this->modelClassName(false);
191  }
192  return $this->model;
193  }
194 
198  public function getColumnName()
199  {
200  $modelClassName = $this->modelClassName;
201  return $modelClassName::getColumnNameByAttribute($this->attribute);
202  }
203 
209  public function getColumnNameByPosition($attributePosition)
210  {
211  $modelClassName = $this->modelClassName;
212  $sortAttributes = $modelClassName::getSortAttributesByAttribute($this->attribute);
213  if ($attributePosition >= count($sortAttributes))
214  {
215  throw new InvalidArgumentException('Attribute position is not valid');
216  }
217  $sortAtribute = $sortAttributes[$attributePosition];
218  return $modelClassName::getColumnNameByAttribute($sortAtribute);
219  }
220 
224  public function isRelation()
225  {
226  $modelClassName = $this->modelClassName;
227  return $modelClassName::isRelation($this->attribute);
228  }
229 
233  public function getRelationType()
234  {
235  $modelClassName = $this->modelClassName;
236  return $modelClassName::getRelationType($this->attribute);
237  }
238 
239  public function isOwnedRelation()
240  {
241  if (!$this->getModel()->isRelation($this->attribute))
242  {
243  return false;
244  }
245  return $this->getModel()->isOwnedRelation($this->attribute);
246  }
247 
252  {
253  $modelClassName = $this->modelClassName;
254  return $modelClassName::isRelationTypeAHasManyVariant($this->attribute);
255  }
256 
261  {
262  $modelClassName = $this->modelClassName;
263  return $modelClassName::isRelationTypeAHasOneVariant($this->attribute);
264  }
265 
269  public function hasRelatedAttribute()
270  {
271  if ($this->relatedAttribute == null)
272  {
273  return false;
274  }
275  return true;
276  }
277 
281  protected function getRelationModel()
282  {
283  if ($this->relatedModel == null)
284  {
285  $relationModelClassName = $this->getRelationModelClassName();
286  $this->relatedModel = new $relationModelClassName(false);
287  }
288  return $this->relatedModel;
289  }
290 
294  public function getRelationModelClassName()
295  {
296  $modelClassName = $this->modelClassName;
297  return $modelClassName::getRelationModelClassName($this->attribute);
298  }
299 
305  {
306  if ($this->relatedAttribute == 'id')
307  {
308  return $this->getRelationModelClassName();
309  }
310  $relationModelClassName = $this->getRelationModelClassName();
311  return $relationModelClassName::getAttributeModelClassName($this->relatedAttribute);
312  }
313 
317  public function canRelationHaveTable()
318  {
319  $modelClassName = $this->getRelationModelClassName();
320  return $modelClassName::getCanHaveBean();
321  }
322 
326  public function getRelationTableName()
327  {
328  $modelClassName = $this->getRelationModelClassName();
329  if ($this->canRelationHaveTable())
330  {
331  return $modelClassName::getTableName();
332  }
333  else
334  {
335  while (get_parent_class($modelClassName) != 'RedBeanModel')
336  {
337  $modelClassName = get_parent_class($modelClassName);
338  if ($modelClassName::getCanHaveBean())
339  {
340  return $modelClassName::getTableName();
341  }
342  }
343  throw new NotSupportedException();
344  }
345  }
346 
352  {
353  $modelClassName = $this->getRelationModelClassName();
354  if ($this->canRelationHaveTable())
355  {
356  return $modelClassName;
357  }
358  else
359  {
360  while (get_parent_class($modelClassName) != 'RedBeanModel')
361  {
362  $modelClassName = get_parent_class($modelClassName);
363  if ($modelClassName::getCanHaveBean())
364  {
365  return $modelClassName;
366  }
367  }
368  throw new NotSupportedException();
369  }
370  }
371 
376  {
377  $modelClassName = $this->getRelatedAttributeModelClassName();
378  return $modelClassName::getTableName();
379  }
380 
386  {
387  $modelClassName = $this->getRelationModelClassName();
388  return $modelClassName::getColumnNameByAttribute($this->relatedAttribute);
389  }
390 
397  public function getRelatedAttributeColumnNameByPosition($attributePosition)
398  {
399  $modelClassName = $this->getRelationModelClassName();
400  $sortAttributes = $modelClassName::getSortAttributesByAttribute($this->relatedAttribute);
401  if ($attributePosition >= count($sortAttributes))
402  {
403  throw new InvalidArgumentException('Attribute position is not valid');
404  }
405  $sortAttribute = $sortAttributes[$attributePosition];
406  return $modelClassName::getColumnNameByAttribute($sortAttribute);
407  }
408 
412  public function isRelatedAttributeRelation()
413  {
414  $modelClassName = $this->getRelationModelClassName();
415  return $modelClassName::isRelation($this->relatedAttribute);
416  }
417 
422  {
423  if (!$this->isRelatedAttributeRelation())
424  {
425  throw new NotSupportedException();
426  }
427  $modelClassName = $this->getRelationModelClassName();
428  return $modelClassName::getRelationType($this->relatedAttribute);
429  }
430 
436  {
437  if (!$this->isRelatedAttributeRelation())
438  {
439  throw new NotSupportedException();
440  }
441  $modelClassName = $this->getRelationModelClassName();
442  return $modelClassName::getRelationModelClassName($this->relatedAttribute);
443  }
444 
449  public function getManyToManyTableName()
450  {
452  {
453  throw new NotSupportedException();
454  }
455  $attributeName = $this->getAttribute();
456  return $this->getModel()->{$attributeName}->getTableName();
457  }
458 
462  public function isAttributeMixedIn()
463  {
464  if ($this->getModelClassName() == 'User' &&
465  $this->getAttributeModelClassName() == 'Person')
466  {
467  return true;
468  }
469  return false;
470  }
471 
475  public function isAttributeOnDifferentModel()
476  {
477  if ($this->getAttributeModelClassName() == $this->getModelClassName())
478  {
479  return false;
480  }
481  return true;
482  }
483 
488  public function resolveManyToManyColumnName()
489  {
490  if ($this->getRelatedAttribute() != 'id')
491  {
492  return $this->getRelatedAttributeColumnName();
493  }
494  else
495  {
496  return $this->getRelationTableName() . '_id';
497  }
498  }
499 
500  public function isAttributeDerivedRelationViaCastedUpModel()
501  {
502  $modelClassName = $this->modelClassName;
503  if ($modelClassName::isADerivedRelationViaCastedUpModel($this->attribute))
504  {
505  return true;
506  }
507  return false;
508  }
509 
510  public function getCastedUpModelClassNameForDerivedRelation()
511  {
512  if (!$this->isAttributeDerivedRelationViaCastedUpModel())
513  {
514  throw new NotSupportedException();
515  }
516  $modelClassName = $this->modelClassName;
517  $relationModelClassName = $modelClassName::getDerivedRelationModelClassName($this->attribute);
518  $opposingRelationName = $modelClassName::getDerivedRelationViaCastedUpModelOpposingRelationName($this->attribute);
519  $relationModel = new $relationModelClassName();
520  return $relationModel->getRelationModelClassName($opposingRelationName);
521  }
522 
523  public function getManyToManyTableNameForDerivedRelationViaCastedUpModel()
524  {
525  $modelClassName = $this->modelClassName;
526  $relationModelClassName = $modelClassName::getDerivedRelationModelClassName($this->attribute);
527  $opposingRelationName = $modelClassName::getDerivedRelationViaCastedUpModelOpposingRelationName($this->attribute);
528  $relationModel = new $relationModelClassName();
529 
530  if ($modelClassName::getDerivedRelationType($this->attribute) != RedBeanModel::MANY_MANY)
531  {
532  throw new NotSupportedException();
533  }
534  $attributeName = $this->getAttribute();
535  return $relationModel->{$opposingRelationName}->getTableName();
536  }
537 
545  {
546  $modelClassName = $this->modelClassName;
547  $relationModelClassName = $this->getDerivedRelationViaCastedUpModelClassName();
548  $opposingRelationName = $modelClassName::getDerivedRelationViaCastedUpModelOpposingRelationName($this->attribute);
549  $relationModel = new $relationModelClassName();
550  return $relationModel->getAttributeModelClassName($opposingRelationName);
551  }
552 
557  {
558  $modelClassName = $this->modelClassName;
559  return $modelClassName::getDerivedRelationModelClassName($this->getAttribute());
560  }
561 
562  public function getOpposingRelationTableName()
563  {
564  $opposingRelationModelClassName = $this->getOpposingRelationModelClassName();
565  return $opposingRelationModelClassName::getTableName();
566  }
567 
568  public function isDerivedRelationViaCastedUpModelDifferentThanOpposingModelClassName()
569  {
570  $opposingRelationModelClassName = $this->getOpposingRelationModelClassName();
571  $derivedRelationModelClassName = $this->getDerivedRelationViaCastedUpModelClassName();
572  if ($opposingRelationModelClassName != $derivedRelationModelClassName)
573  {
574  return true;
575  }
576  return false;
577  }
578 
584  public function isInferredRelation()
585  {
586  return false;
587  }
588 
593  public function sortUsesTwoAttributes()
594  {
595  $modelClassName = $this->modelClassName;
596  if (count($modelClassName::getSortAttributesByAttribute($this->attribute)) > 1)
597  {
598  return true;
599  }
600  return false;
601  }
602 
603  public function relatedAttributesSortUsesTwoAttributes()
604  {
605  $modelClassName = $this->getRelationModelClassName();
606  if (count($modelClassName::getSortAttributesByAttribute($this->relatedAttribute)) > 1)
607  {
608  return true;
609  }
610  return false;
611  }
612  }
613 ?>
setCastingHintModelClassNameForAttribute($castingHintAttributeModelClassName)
const MANY_MANY
Definition: BeanModel.php:78
__construct($modelClassName, $attribute, $relatedAttribute=null)
Generated on Sat Jul 11 2020 07:10:25
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.