Completed
Push — master ( 6aa9cd...5cada7 )
by Jean-Christophe
05:53
created

DAORelationsPrepareTrait::prepareOneToMany()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
ccs 11
cts 12
cp 0.9167
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 4
crap 5.0144
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\core\PendingRelationsRequest;
7
use Ubiquity\orm\parser\ManyToManyParser;
8
9
trait DAORelationsPrepareTrait {
10
	
11
	/**
12
	 * Prepares members associated with $instance with a ManyToMany type relationship
13
	 * @param $ret array of sql conditions
14
	 * @param object $instance
15
	 * @param string $member Member on which a ManyToMany annotation must be present
16
	 * @param array $annot used internally
17
	 */
18 4
	protected static function prepareManyToMany(&$ret,$instance, $member, $annot=null) {
19 4
		$class=get_class($instance);
20 4
		if (!isset($annot)){
21
			$annot=OrmUtils::getAnnotationInfoMember($class, "#ManyToMany", $member);
22
		}
23 4
		if ($annot !== false) {
24 4
			$key=$annot["targetEntity"]."|".$member."|".$annot["inversedBy"];
25 4
			if(!isset($ret[$key])){
26 4
				$parser=new ManyToManyParser($instance, $member);
27 4
				$parser->init($annot);
28 4
				$ret[$key]=$parser;
29
			}
30 4
			$accessor="get" . ucfirst($ret[$key]->getMyPk());
31 4
			if(method_exists($instance, $accessor)){
32 4
				$fkv=$instance->$accessor();
33 4
				$ret[$key]->addValue($fkv);
34
			}
35
		}
36 4
	}
37
	
38
	/**
39
	 * Prepares members associated with $instance with a oneToMany type relationship
40
	 * @param $ret array of sql conditions
41
	 * @param object $instance
42
	 * @param string $member Member on which a OneToMany annotation must be present
43
	 * @param array $annot used internally
44
	 */
45 4
	protected static function prepareOneToMany(&$ret,$instance, $member, $annot=null) {
46 4
		$class=get_class($instance);
47 4
		if (!isset($annot))
48
			$annot=OrmUtils::getAnnotationInfoMember($class, "#oneToMany", $member);
49 4
			if ($annot !== false) {
50 4
				$fkAnnot=OrmUtils::getAnnotationInfoMember($annot["className"], "#joinColumn", $annot["mappedBy"]);
51 4
				if ($fkAnnot !== false) {
52 4
					$fkv=OrmUtils::getFirstKeyValue($instance);
53 4
					$key=$annot["className"]."|".$member."|".$annot["mappedBy"];
54 4
					if(!isset($ret[$key])){
55 4
						$ret[$key]=new PendingRelationsRequest();
56
					}
57 4
					$ret[$key]->addPartObject($instance,$fkAnnot["name"] . "= ?",$fkv);
58
				}
59
			}
60 4
	}
61
	
62
	/**
63
	 * Prepares members associated with $instance with a manyToOne type relationship
64
	 * @param $ret array of sql conditions
65
	 * @param object $instance
66
	 * @param mixed $value
67
	 * @param string $fkField
68
	 * @param array $annotationArray
69
	 */
70 5
	protected static function prepareManyToOne(&$ret, $instance,$value, $fkField,$annotationArray) {
71 5
		$member=$annotationArray["member"];
72 5
		$fk=OrmUtils::getFirstKey($annotationArray["className"]);
73 5
		$key=$annotationArray["className"]."|".$member."|".$fkField;
74 5
		if(!isset($ret[$key])){
75 5
			$ret[$key]=new PendingRelationsRequest();
76
		}
77 5
		$ret[$key]->addPartObject($instance,$fk . "= ?",$value);
78 5
	}
79
}
80