AWRequiredFields::php()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 6.7272
cc 7
eloc 17
nc 3
nop 1
1
<?php
2
/**
3
 * Extends RequiredFields so we can prevent DO writes in AW's controller(s) without needing to catch Exceptions from DO->validate() all over the place.
4
 * Note specifically $this->getExtendedValidationRoutines() - anti-pattern anyone?
5
 *
6
 * @author Russell Michell [email protected]
7
 * @package advancedworkflow
8
 */
9
class AWRequiredFields extends RequiredFields {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
	protected $data = array();
12
	protected static $caller;
13
14
	public function php($data) {
15
		$valid = parent::php($data);
16
		$this->setData($data);
17
18
		// Fetch any extended validation routines on the caller
19
		$extended = $this->getExtendedValidationRoutines();
20
21
		// Only deal-to extended routines once the parent is done
22
		if($valid && $extended['fieldValid'] !== true) {
23
			$fieldName = $extended['fieldName'];
24
			$formField = $extended['fieldField'];
25
			$errorMessage = sprintf($extended['fieldMsg'],
26
				strip_tags('"'.(($formField && $formField->Title()) ? $formField->Title() : $fieldName).'"'));
27
28
			if($formField && $msg = $formField->getCustomValidationMessage()) {
29
				$errorMessage = $msg;
30
			}
31
			
32
			$this->validationError(
33
				$fieldName,
34
				$errorMessage,
35
				"required"
36
			);
37
			$valid = false;
38
		}
39
		return $valid;
40
	}
41
42
	/*
43
	 * Allows for the addition of an arbitrary no. additional, dedicated and "extended" validation methods on classes that call AWRequiredFields.
44
	 * To add specific validation methods to a caller:
45
	 *
46
	 * 1). Write each checking method using this naming prototype: public function extendedRequiredFieldsXXX(). All methods so named will be called.
47
	 * 2). Call AWRequiredFields->setCaller($this)
48
	 *
49
	 * Each extended method thus called, should return an array of a specific format. (See: static $extendedMethodReturn on the caller)
50
	 *
51
	 * @return array $return
52
	 */
53
	public function getExtendedValidationRoutines() {
54
		// Setup a return array 
55
		$return = array(
56
			'fieldValid'=>true,
57
			'fieldName'	=>null,
58
			'fieldField'=>null,
59
			'fieldMsg'	=>null
60
		);
61
		$caller = $this->getCaller();
62
		$methods = get_class_methods($caller);
63
		if(!$methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
			return $return;
65
		}
66
		foreach($methods as $method) {
67
			if(!preg_match("#extendedRequiredFields#",$method)) {
68
				continue;
69
			}
70
			// One of the DO's validation methods has failed
71
			$extended = $caller->$method($this->getData());
72
			if($extended['fieldValid'] !== true) {
73
				$return['fieldValid']	= $extended['fieldValid'];
74
				$return['fieldName']	= $extended['fieldName'];
75
				$return['fieldField']	= $extended['fieldField'];
76
				$return['fieldMsg']		= $extended['fieldMsg'];
77
				break;
78
			}
79
		}
80
		return $return;
81
	}
82
83
	protected function setData($data) {
84
		$this->data = $data;
85
	}
86
87
	protected function getData() {
88
		return $this->data;
89
	}
90
91
	public function setCaller($caller) {
92
		self::$caller = $caller;
93
	}
94
95
	public function getCaller() {
96
		return self::$caller;
97
	}
98
}