Passed
Push — master ( caa32e...4f267a )
by Jean-Christophe
09:15
created

DAORelationsPrepareTrait::prepareOneToMany()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

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