Completed
Push — master ( 997533...5f070c )
by Jean-Christophe
01:37
created

OrmUtilsFieldsTrait::getFirstKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
trait OrmUtilsFieldsTrait {
6
	abstract public static function getAnnotationInfo($class, $keyAnnotation);
7
	
8
	public static function getFieldTypes($className) {
9
		$fieldTypes=self::getAnnotationInfo($className, "#fieldTypes");
10
		if ($fieldTypes !== false){
11
			return $fieldTypes;
12
		}
13
		return [ ];
14
	}
15
	
16
	public static function getFieldType($className,$field){
17
		$types= self::getFieldTypes($className);
18
		if(isset($types[$field])){
19
			return $types[$field];
20
		}
21
		return "int";
22
	}
23
	
24
	public static function getKeyFields($instance) {
25
		if(!\is_string($instance)){
26
			$instance=\get_class($instance);
27
		}
28
		return self::getAnnotationInfo($instance, "#primaryKeys");
29
	}
30
	
31
	public static function getFirstKey($class) {
32
		$kf=self::getAnnotationInfo($class, "#primaryKeys");
33
		return \reset($kf);
34
	}
35
	
36
	public static function getAllFields($class){
37
		return \array_keys(self::getAnnotationInfo($class, "#fieldNames"));
38
	}
39
	
40
	
41
	public static function getFieldNames($model){
42
		$fields=self::getAnnotationInfo($model, "#fieldNames");
43
		$result=[];
44
		$serializables=self::getSerializableFields($model);
45
		foreach ($fields as $member=>$field){
46
			if(\array_search($member, $serializables)!==false)
47
				$result[$field]=$member;
48
		}
49
		return $result;
50
	}
51
	
52
	public static function getSerializableFields($class) {
53
		$notSerializable=self::getAnnotationInfo($class, "#notSerializable");
54
		$fieldNames=\array_keys(self::getAnnotationInfo($class, "#fieldNames"));
55
		return \array_diff($fieldNames, $notSerializable);
56
	}
57
	
58
	public static function getFormAllFields($class){
59
		$result=self::getSerializableFields($class);
60 View Code Duplication
		if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
			foreach ($manyToOne as $member){
62
				$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member );
0 ignored issues
show
Bug introduced by
The method getAnnotationInfoMember() does not exist on Ubiquity\orm\traits\OrmUtilsFieldsTrait. Did you maybe mean getAnnotationInfo()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
				$result[]=$joinColumn["name"];
64
			}
65
		}
66 View Code Duplication
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
			$manyToMany=array_keys($manyToMany);
68
			foreach ($manyToMany as $member){
69
				$result[]=$member . "Ids";
70
			}
71
		}
72
		return $result;
73
	}
74
}
75
76