Passed
Push — master ( 5305f0...4e53b3 )
by Jean-Christophe
11:14
created

DAORelationsPrepareTrait::prepareManyToMany()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 17
ccs 13
cts 14
cp 0.9286
rs 9.5222
c 0
b 0
f 0
cc 5
nc 10
nop 5
crap 5.009
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
use Ubiquity\db\SqlUtils;
9
10
/**
11
 * Used by DAO class, prepare relations for loading.
12
 * Ubiquity\orm\traits$DAORelationsPrepareTrait
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.3
17
 *
18
 */
19
trait DAORelationsPrepareTrait {
20
21
	/**
22
	 * Prepares members associated with $instance with a ManyToMany type relationship
23
	 *
24
	 * @param $db
25
	 * @param $ret array of sql conditions
26
	 * @param object $instance
27
	 * @param string $member Member on which a ManyToMany annotation must be present
28
	 * @param array $annot used internally
29
	 */
30 23
	protected static function prepareManyToMany($db, &$ret, $instance, $member, $annot = null) {
31 23
		$class = get_class ( $instance );
32 23
		if (! isset ( $annot )) {
33
			$annot = OrmUtils::getAnnotationInfoMember ( $class, "#ManyToMany", $member );
34
		}
35 23
		if ($annot !== false) {
36 23
			$key = $annot ["targetEntity"] . "|" . $member . "|" . $annot ["inversedBy"] . "|";
37 23
			if (! isset ( $ret [$key] )) {
38 23
				$parser = new ManyToManyParser ( $db, $instance, $member );
39
40 23
				$parser->init ( $annot );
41 23
				$ret [$key] = $parser;
42
			}
43 23
			$accessor = "get" . ucfirst ( $ret [$key]->getMyPk () );
44 23
			if (method_exists ( $instance, $accessor )) {
45 23
				$fkv = $instance->$accessor ();
46 23
				$ret [$key]->addValue ( $fkv );
47
			}
48
		}
49 23
	}
50
51
	/**
52
	 * Prepares members associated with $instance with a oneToMany type relationship
53
	 *
54
	 * @param $ret array of sql conditions
55
	 * @param object $instance
56
	 * @param string $member Member on which a OneToMany annotation must be present
57
	 * @param array $annot used internally
58
	 */
59 37
	protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null) {
60 37
		$class = get_class ( $instance );
61 37
		if (! isset ( $annot ))
62
			$annot = OrmUtils::getAnnotationInfoMember ( $class, "#oneToMany", $member );
63 37
		if ($annot !== false) {
64 37
			$fkAnnot = OrmUtils::getAnnotationInfoMember ( $annot ["className"], "#joinColumn", $annot ["mappedBy"] );
65 37
			if ($fkAnnot !== false) {
66 37
				$fkv = OrmUtils::getFirstKeyValue ( $instance );
67 37
				$key = $annot ["className"] . "|" . $member . "|" . $annot ["mappedBy"] . "|" . $fkAnnot ["className"];
68 37
				if (! isset ( $ret [$key] )) {
69 37
					$ret [$key] = new PendingRelationsRequest ();
70
				}
71 37
				$quote = SqlUtils::$quote;
72 37
				$ret [$key]->addPartObject ( $instance, $quote . $fkAnnot ["name"] . $quote . "= ?", $fkv );
73
			}
74
		}
75 37
	}
76
77
	/**
78
	 * Prepares members associated with $instance with a manyToOne type relationship
79
	 *
80
	 * @param $ret array of sql conditions
81
	 * @param object $instance
82
	 * @param mixed $value
83
	 * @param string $fkField
84
	 * @param array $annotationArray
85
	 */
86 23
	protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray) {
87 23
		$member = $annotationArray ["member"];
88 23
		$fk = OrmUtils::getFirstKey ( $annotationArray ["className"] );
89 23
		$key = $annotationArray ["className"] . "|" . $member . "|" . $fkField . "|";
90 23
		if (! isset ( $ret [$key] )) {
91 23
			$ret [$key] = new PendingRelationsRequest ();
92
		}
93 23
		$ret [$key]->addPartObject ( $instance, SqlUtils::$quote . $fk . SqlUtils::$quote . "= ?", $value );
94 23
	}
95
}
96