Passed
Push — master ( d7f029...202469 )
by Jean-Christophe
15:08
created

DAORelationsPrepareTrait::prepareManyToOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 5
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
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.4
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
			$fkClass = $annot ["className"];
65 37
			$fkAnnot = OrmUtils::getAnnotationInfoMember ( $fkClass, "#joinColumn", $annot ["mappedBy"] );
66 37
			if ($fkAnnot !== false) {
67 37
				$fkMemberName = $annot ["mappedBy"];
68 37
				if (\property_exists ( $fkClass, $fkAnnot ['name'] )) { // $fkClass is a class association (manyToMany with property)
69 20
					$fkMemberName = $fkAnnot ['name'];
70
				}
71 37
				$fkv = OrmUtils::getFirstKeyValue ( $instance );
72 37
				$key = $annot ["className"] . "|" . $member . "|" . $fkMemberName . "|" . $fkAnnot ["className"];
73 37
				if (! isset ( $ret [$key] )) {
74 37
					$ret [$key] = new PendingRelationsRequest ();
75
				}
76 37
				$quote = SqlUtils::$quote;
77 37
				$ret [$key]->addPartObject ( $instance, $quote . $fkAnnot ["name"] . $quote . "= ?", $fkv );
78
			}
79
		}
80 37
	}
81
82
	/**
83
	 * Prepares members associated with $instance with a manyToOne type relationship
84
	 *
85
	 * @param $ret array of sql conditions
86
	 * @param object $instance
87
	 * @param mixed $value
88
	 * @param string $fkField
89
	 * @param array $annotationArray
90
	 */
91 23
	protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray) {
92 23
		$member = $annotationArray ["member"];
93 23
		$fk = OrmUtils::getFirstKey ( $annotationArray ["className"] );
94 23
		$key = $annotationArray ["className"] . "|" . $member . "|" . $fkField . "|";
95 23
		if (! isset ( $ret [$key] )) {
96 23
			$ret [$key] = new PendingRelationsRequest ();
97
		}
98 23
		$ret [$key]->addPartObject ( $instance, SqlUtils::$quote . $fk . SqlUtils::$quote . "= ?", $value );
99 23
	}
100
}
101