Passed
Pull Request — master (#17)
by Anton
03:14
created

Foreign::getAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Entitizer\Utils\Definition\Item {
11
12
	use Modules\Entitizer\Utils\Definition;
13
14
	class Foreign extends Definition\Item {
15
16
		protected $table = '', $field = '', $delete = null, $update = null;
17
18
		/**
19
		 * Validate an action value
20
		 *
21
		 * @return string|null : the action or null if the value is invalid
22
		 */
23
24
		private function validateAction(string $value) {
25
26
			$value = strtoupper($value); $range = ['CASCADE', 'RESTRICT', 'SET NULL'];
27
28
			return (in_array($value, $range, true) ? $value : null);
29
		}
30
31
		/**
32
		 * Constructor
33
		 */
34
35
		public function __construct(string $name, string $table, string $field, string $delete = null, string $update = null) {
36
37
			$this->name = $name; $this->table = $table; $this->field = $field;
38
39
			if (null !== $delete) $this->delete = $this->validateAction($delete);
40
41
			if (null !== $update) $this->update = $this->validateAction($update);
42
		}
43
44
		/**
45
		 * Get the statement
46
		 */
47
48
		public function getStatement() : string {
49
50
			return ("FOREIGN KEY (`" . $this->name . "`) REFERENCES `" . $this->table . "` (`" . $this->field . "`)") .
51
52
				   ((null !== $this->delete) ? (" ON DELETE " . $this->delete) : "") .
53
54
				   ((null !== $this->update) ? (" ON UPDATE " . $this->update) : "");
55
		}
56
	}
57
}
58