Completed
Push — master ( 2d1963...3845e2 )
by Jean-Christophe
02:09
created

DAOUQueries::uParse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\parser\ConditionParser;
7
8
/**
9
 * @author jc
10
 * @property \Ubiquity\db\Database $db
11
 */
12
trait DAOUQueries {
13
	
14
	protected static $annotFieldsInRelations=[];
15
	
16
	abstract protected static function _getAll($className, ConditionParser $conditionParser, $included=true,$useCache=NULL);
17
	abstract protected static function _getOne($className,ConditionParser $conditionParser,$included,$useCache);
18
	
19
	protected static function uParse($className,&$ucondition){
20
		$expressions=self::uGetExpressions($ucondition);
21
		$condition="";
22
		$aliases=[];
23
		foreach ($expressions as $expression){
24
			$expressionArray=explode(".",$expression);
25
			self::uParseExpression($className, $expression, $expressionArray, $condition, $ucondition,$aliases);
26
		}
27
		return $condition;
28
	}
29
	
30
	protected static function uParseExpression($className,$expression,&$expressionArray,&$condition,&$ucondition,&$aliases){
31
		$relations=self::getAnnotFieldsInRelations($className);
32
		$field=array_shift($expressionArray);
33
		if(isset($relations[$field])){
34
			$jSQL=OrmUtils::getUJoinSQL($className, $relations[$field],$field,$aliases);
35
			$condition.=" ".$jSQL["sql"];
36
			if(sizeof($expressionArray)===1){
37
				$ucondition=str_replace($expression, "{$jSQL["alias"]}.".$expressionArray[0], $ucondition);
38
			}else{
39
				self::uParseExpression($jSQL["class"], $expression, $expressionArray, $condition, $ucondition,$aliases);
40
			}
41
		}
42
	}
43
	
44
	protected static function getAnnotFieldsInRelations($className){
45
		if(!isset(self::$annotFieldsInRelations[$className])){
46
			return self::$annotFieldsInRelations[$className]=OrmUtils::getAnnotFieldsInRelations($className);
47
		}
48
		return self::$annotFieldsInRelations[$className];
49
		
50
		
51
	}
52
	
53
	protected static function uGetExpressions($condition){
54
		$condition=preg_replace('@(["\']([^"\']|""|\'\')*["\'])@', "%values%", $condition);
55
		preg_match_all('@[a-zA-Z_$][a-zA-Z_$0-9]*(?:\.[a-zA-Z_$\*][a-zA-Z_$0-9\*]*)+@', $condition,$matches);
56
		if(sizeof($matches)>0){
57
			return array_unique($matches[0]);
58
		}
59
		return [];
60
	}
61
	
62
	/**
63
	 * Returns an array of $className objects from the database
64
	 * @param string $className class name of the model to load
65
	 * @param string $condition UQL condition
0 ignored issues
show
Documentation introduced by
There is no parameter named $condition. Did you maybe mean $ucondition?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
66
	 * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"]
67
	 * @param array|null $parameters the request parameters
68
	 * @param boolean $useCache use the active cache if true
69
	 * @return array
70
	 */
71
	public static function uGetAll($className, $ucondition='', $included=true,$parameters=null,$useCache=NULL) {
72
		$condition=self::uParse($className, $ucondition);
73
		return self::_getAll($className, new ConditionParser($ucondition,$condition,$parameters),$included,$useCache);
1 ignored issue
show
Bug introduced by
It seems like $included defined by parameter $included on line 71 can also be of type array; however, Ubiquity\orm\traits\DAOUQueries::_getAll() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
	}
75
	
76
	/**
77
	 * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter
78
	 * @param string $className complete classname of the model to load
79
	 * @param string $ucondition Part following the WHERE of an SQL statement
80
	 * @param array|null $parameters The query parameters
81
	 * @return int count of objects
82
	 */
83
	public static function uCount($className, $ucondition='',$parameters=null) {
84
		$condition=self::uParse($className, $ucondition);
85
		$tableName=OrmUtils::getTableName($className);
86
		if ($ucondition != ''){
87
			$ucondition=" WHERE " . $ucondition;
88
		}
89
		return self::$db->prepareAndFetchColumn("SELECT COUNT(*) FROM " . $tableName ." ". $condition.$ucondition,$parameters,0);
90
	}
91
	
92
	/**
93
	 * Returns an instance of $className from the database, from $keyvalues values of the primary key
94
	 * @param String $className complete classname of the model to load
95
	 * @param Array|string $ucondition primary key values or condition (UQL)
96
	 * @param boolean|array $included if true, charges associate members with association
97
	 * @param array|null $parameters the request parameters
98
	 * @param boolean $useCache use cache if true
99
	 * @return object the instance loaded or null if not found
100
	 */
101
	public static function uGetOne($className, $ucondition, $included=true,$parameters=null,$useCache=NULL) {
102
		$condition=self::uParse($className, $ucondition);
103
		$conditionParser=new ConditionParser($ucondition,$condition);
104
		if(is_array($parameters)){
105
			$conditionParser->setParams($parameters);
106
		}
107
		return self::_getOne($className, $conditionParser, $included, $useCache);
108
	}
109
}
110
111