Completed
Push — develop ( 32764c...cb3cfa )
by
unknown
20:26
created
vendor/justinrainbow/json-schema/src/JsonSchema/Entity/JsonPointer.php 1 patch
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -18,144 +18,144 @@
 block discarded – undo
18 18
  */
19 19
 class JsonPointer
20 20
 {
21
-    /** @var string */
22
-    private $filename;
23
-
24
-    /** @var string[] */
25
-    private $propertyPaths = array();
26
-
27
-    /**
28
-     * @var bool Whether the value at this path was set from a schema default
29
-     */
30
-    private $fromDefault = false;
31
-
32
-    /**
33
-     * @param string $value
34
-     *
35
-     * @throws InvalidArgumentException when $value is not a string
36
-     */
37
-    public function __construct($value)
38
-    {
39
-        if (!is_string($value)) {
40
-            throw new InvalidArgumentException('Ref value must be a string');
41
-        }
42
-
43
-        $splitRef = explode('#', $value, 2);
44
-        $this->filename = $splitRef[0];
45
-        if (array_key_exists(1, $splitRef)) {
46
-            $this->propertyPaths = $this->decodePropertyPaths($splitRef[1]);
47
-        }
48
-    }
49
-
50
-    /**
51
-     * @param string $propertyPathString
52
-     *
53
-     * @return string[]
54
-     */
55
-    private function decodePropertyPaths($propertyPathString)
56
-    {
57
-        $paths = array();
58
-        foreach (explode('/', trim($propertyPathString, '/')) as $path) {
59
-            $path = $this->decodePath($path);
60
-            if (is_string($path) && '' !== $path) {
61
-                $paths[] = $path;
62
-            }
63
-        }
64
-
65
-        return $paths;
66
-    }
67
-
68
-    /**
69
-     * @return array
70
-     */
71
-    private function encodePropertyPaths()
72
-    {
73
-        return array_map(
74
-            array($this, 'encodePath'),
75
-            $this->getPropertyPaths()
76
-        );
77
-    }
78
-
79
-    /**
80
-     * @param string $path
81
-     *
82
-     * @return string
83
-     */
84
-    private function decodePath($path)
85
-    {
86
-        return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));
87
-    }
88
-
89
-    /**
90
-     * @param string $path
91
-     *
92
-     * @return string
93
-     */
94
-    private function encodePath($path)
95
-    {
96
-        return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25'));
97
-    }
98
-
99
-    /**
100
-     * @return string
101
-     */
102
-    public function getFilename()
103
-    {
104
-        return $this->filename;
105
-    }
106
-
107
-    /**
108
-     * @return string[]
109
-     */
110
-    public function getPropertyPaths()
111
-    {
112
-        return $this->propertyPaths;
113
-    }
114
-
115
-    /**
116
-     * @param array $propertyPaths
117
-     *
118
-     * @return JsonPointer
119
-     */
120
-    public function withPropertyPaths(array $propertyPaths)
121
-    {
122
-        $new = clone $this;
123
-        $new->propertyPaths = $propertyPaths;
124
-
125
-        return $new;
126
-    }
127
-
128
-    /**
129
-     * @return string
130
-     */
131
-    public function getPropertyPathAsString()
132
-    {
133
-        return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/');
134
-    }
135
-
136
-    /**
137
-     * @return string
138
-     */
139
-    public function __toString()
140
-    {
141
-        return $this->getFilename() . $this->getPropertyPathAsString();
142
-    }
143
-
144
-    /**
145
-     * Mark the value at this path as being set from a schema default
146
-     */
147
-    public function setFromDefault()
148
-    {
149
-        $this->fromDefault = true;
150
-    }
151
-
152
-    /**
153
-     * Check whether the value at this path was set from a schema default
154
-     *
155
-     * @return bool
156
-     */
157
-    public function fromDefault()
158
-    {
159
-        return $this->fromDefault;
160
-    }
21
+	/** @var string */
22
+	private $filename;
23
+
24
+	/** @var string[] */
25
+	private $propertyPaths = array();
26
+
27
+	/**
28
+	 * @var bool Whether the value at this path was set from a schema default
29
+	 */
30
+	private $fromDefault = false;
31
+
32
+	/**
33
+	 * @param string $value
34
+	 *
35
+	 * @throws InvalidArgumentException when $value is not a string
36
+	 */
37
+	public function __construct($value)
38
+	{
39
+		if (!is_string($value)) {
40
+			throw new InvalidArgumentException('Ref value must be a string');
41
+		}
42
+
43
+		$splitRef = explode('#', $value, 2);
44
+		$this->filename = $splitRef[0];
45
+		if (array_key_exists(1, $splitRef)) {
46
+			$this->propertyPaths = $this->decodePropertyPaths($splitRef[1]);
47
+		}
48
+	}
49
+
50
+	/**
51
+	 * @param string $propertyPathString
52
+	 *
53
+	 * @return string[]
54
+	 */
55
+	private function decodePropertyPaths($propertyPathString)
56
+	{
57
+		$paths = array();
58
+		foreach (explode('/', trim($propertyPathString, '/')) as $path) {
59
+			$path = $this->decodePath($path);
60
+			if (is_string($path) && '' !== $path) {
61
+				$paths[] = $path;
62
+			}
63
+		}
64
+
65
+		return $paths;
66
+	}
67
+
68
+	/**
69
+	 * @return array
70
+	 */
71
+	private function encodePropertyPaths()
72
+	{
73
+		return array_map(
74
+			array($this, 'encodePath'),
75
+			$this->getPropertyPaths()
76
+		);
77
+	}
78
+
79
+	/**
80
+	 * @param string $path
81
+	 *
82
+	 * @return string
83
+	 */
84
+	private function decodePath($path)
85
+	{
86
+		return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));
87
+	}
88
+
89
+	/**
90
+	 * @param string $path
91
+	 *
92
+	 * @return string
93
+	 */
94
+	private function encodePath($path)
95
+	{
96
+		return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25'));
97
+	}
98
+
99
+	/**
100
+	 * @return string
101
+	 */
102
+	public function getFilename()
103
+	{
104
+		return $this->filename;
105
+	}
106
+
107
+	/**
108
+	 * @return string[]
109
+	 */
110
+	public function getPropertyPaths()
111
+	{
112
+		return $this->propertyPaths;
113
+	}
114
+
115
+	/**
116
+	 * @param array $propertyPaths
117
+	 *
118
+	 * @return JsonPointer
119
+	 */
120
+	public function withPropertyPaths(array $propertyPaths)
121
+	{
122
+		$new = clone $this;
123
+		$new->propertyPaths = $propertyPaths;
124
+
125
+		return $new;
126
+	}
127
+
128
+	/**
129
+	 * @return string
130
+	 */
131
+	public function getPropertyPathAsString()
132
+	{
133
+		return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/');
134
+	}
135
+
136
+	/**
137
+	 * @return string
138
+	 */
139
+	public function __toString()
140
+	{
141
+		return $this->getFilename() . $this->getPropertyPathAsString();
142
+	}
143
+
144
+	/**
145
+	 * Mark the value at this path as being set from a schema default
146
+	 */
147
+	public function setFromDefault()
148
+	{
149
+		$this->fromDefault = true;
150
+	}
151
+
152
+	/**
153
+	 * Check whether the value at this path was set from a schema default
154
+	 *
155
+	 * @return bool
156
+	 */
157
+	public function fromDefault()
158
+	{
159
+		return $this->fromDefault;
160
+	}
161 161
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/UriRetrieverInterface.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -14,13 +14,13 @@
 block discarded – undo
14 14
  */
15 15
 interface UriRetrieverInterface
16 16
 {
17
-    /**
18
-     * Retrieve a URI
19
-     *
20
-     * @param string      $uri     JSON Schema URI
21
-     * @param null|string $baseUri
22
-     *
23
-     * @return object JSON Schema contents
24
-     */
25
-    public function retrieve($uri, $baseUri = null);
17
+	/**
18
+	 * Retrieve a URI
19
+	 *
20
+	 * @param string      $uri     JSON Schema URI
21
+	 * @param null|string $baseUri
22
+	 *
23
+	 * @return object JSON Schema contents
24
+	 */
25
+	public function retrieve($uri, $baseUri = null);
26 26
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.php 1 patch
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -4,65 +4,65 @@
 block discarded – undo
4 4
 
5 5
 class LooseTypeCheck implements TypeCheckInterface
6 6
 {
7
-    public static function isObject($value)
8
-    {
9
-        return
10
-            is_object($value) ||
11
-            (is_array($value) && (count($value) == 0 || self::isAssociativeArray($value)));
12
-    }
7
+	public static function isObject($value)
8
+	{
9
+		return
10
+			is_object($value) ||
11
+			(is_array($value) && (count($value) == 0 || self::isAssociativeArray($value)));
12
+	}
13 13
 
14
-    public static function isArray($value)
15
-    {
16
-        return
17
-            is_array($value) &&
18
-            (count($value) == 0 || !self::isAssociativeArray($value));
19
-    }
14
+	public static function isArray($value)
15
+	{
16
+		return
17
+			is_array($value) &&
18
+			(count($value) == 0 || !self::isAssociativeArray($value));
19
+	}
20 20
 
21
-    public static function propertyGet($value, $property)
22
-    {
23
-        if (is_object($value)) {
24
-            return $value->{$property};
25
-        }
21
+	public static function propertyGet($value, $property)
22
+	{
23
+		if (is_object($value)) {
24
+			return $value->{$property};
25
+		}
26 26
 
27
-        return $value[$property];
28
-    }
27
+		return $value[$property];
28
+	}
29 29
 
30
-    public static function propertySet(&$value, $property, $data)
31
-    {
32
-        if (is_object($value)) {
33
-            $value->{$property} = $data;
34
-        } else {
35
-            $value[$property] = $data;
36
-        }
37
-    }
30
+	public static function propertySet(&$value, $property, $data)
31
+	{
32
+		if (is_object($value)) {
33
+			$value->{$property} = $data;
34
+		} else {
35
+			$value[$property] = $data;
36
+		}
37
+	}
38 38
 
39
-    public static function propertyExists($value, $property)
40
-    {
41
-        if (is_object($value)) {
42
-            return property_exists($value, $property);
43
-        }
39
+	public static function propertyExists($value, $property)
40
+	{
41
+		if (is_object($value)) {
42
+			return property_exists($value, $property);
43
+		}
44 44
 
45
-        return array_key_exists($property, $value);
46
-    }
45
+		return array_key_exists($property, $value);
46
+	}
47 47
 
48
-    public static function propertyCount($value)
49
-    {
50
-        if (is_object($value)) {
51
-            return count(get_object_vars($value));
52
-        }
48
+	public static function propertyCount($value)
49
+	{
50
+		if (is_object($value)) {
51
+			return count(get_object_vars($value));
52
+		}
53 53
 
54
-        return count($value);
55
-    }
54
+		return count($value);
55
+	}
56 56
 
57
-    /**
58
-     * Check if the provided array is associative or not
59
-     *
60
-     * @param array $arr
61
-     *
62
-     * @return bool
63
-     */
64
-    private static function isAssociativeArray($arr)
65
-    {
66
-        return array_keys($arr) !== range(0, count($arr) - 1);
67
-    }
57
+	/**
58
+	 * Check if the provided array is associative or not
59
+	 *
60
+	 * @param array $arr
61
+	 *
62
+	 * @return bool
63
+	 */
64
+	private static function isAssociativeArray($arr)
65
+	{
66
+		return array_keys($arr) !== range(0, count($arr) - 1);
67
+	}
68 68
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Constraints/TypeCheck/TypeCheckInterface.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -4,15 +4,15 @@
 block discarded – undo
4 4
 
5 5
 interface TypeCheckInterface
6 6
 {
7
-    public static function isObject($value);
7
+	public static function isObject($value);
8 8
 
9
-    public static function isArray($value);
9
+	public static function isArray($value);
10 10
 
11
-    public static function propertyGet($value, $property);
11
+	public static function propertyGet($value, $property);
12 12
 
13
-    public static function propertySet(&$value, $property, $data);
13
+	public static function propertySet(&$value, $property, $data);
14 14
 
15
-    public static function propertyExists($value, $property);
15
+	public static function propertyExists($value, $property);
16 16
 
17
-    public static function propertyCount($value);
17
+	public static function propertyCount($value);
18 18
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php 1 patch
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -4,37 +4,37 @@
 block discarded – undo
4 4
 
5 5
 class StrictTypeCheck implements TypeCheckInterface
6 6
 {
7
-    public static function isObject($value)
8
-    {
9
-        return is_object($value);
10
-    }
11
-
12
-    public static function isArray($value)
13
-    {
14
-        return is_array($value);
15
-    }
16
-
17
-    public static function propertyGet($value, $property)
18
-    {
19
-        return $value->{$property};
20
-    }
21
-
22
-    public static function propertySet(&$value, $property, $data)
23
-    {
24
-        $value->{$property} = $data;
25
-    }
26
-
27
-    public static function propertyExists($value, $property)
28
-    {
29
-        return property_exists($value, $property);
30
-    }
31
-
32
-    public static function propertyCount($value)
33
-    {
34
-        if (!is_object($value)) {
35
-            return 0;
36
-        }
37
-
38
-        return count(get_object_vars($value));
39
-    }
7
+	public static function isObject($value)
8
+	{
9
+		return is_object($value);
10
+	}
11
+
12
+	public static function isArray($value)
13
+	{
14
+		return is_array($value);
15
+	}
16
+
17
+	public static function propertyGet($value, $property)
18
+	{
19
+		return $value->{$property};
20
+	}
21
+
22
+	public static function propertySet(&$value, $property, $data)
23
+	{
24
+		$value->{$property} = $data;
25
+	}
26
+
27
+	public static function propertyExists($value, $property)
28
+	{
29
+		return property_exists($value, $property);
30
+	}
31
+
32
+	public static function propertyCount($value)
33
+	{
34
+		if (!is_object($value)) {
35
+			return 0;
36
+		}
37
+
38
+		return count(get_object_vars($value));
39
+	}
40 40
 }
Please login to merge, or discard this patch.
json-schema/src/JsonSchema/Constraints/CollectionConstraint.php 1 patch
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -19,103 +19,103 @@
 block discarded – undo
19 19
  */
20 20
 class CollectionConstraint extends Constraint
21 21
 {
22
-    /**
23
-     * {@inheritdoc}
24
-     */
25
-    public function check(&$value, $schema = null, JsonPointer $path = null, $i = null)
26
-    {
27
-        // Verify minItems
28
-        if (isset($schema->minItems) && count($value) < $schema->minItems) {
29
-            $this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array('minItems' => $schema->minItems));
30
-        }
22
+	/**
23
+	 * {@inheritdoc}
24
+	 */
25
+	public function check(&$value, $schema = null, JsonPointer $path = null, $i = null)
26
+	{
27
+		// Verify minItems
28
+		if (isset($schema->minItems) && count($value) < $schema->minItems) {
29
+			$this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array('minItems' => $schema->minItems));
30
+		}
31 31
 
32
-        // Verify maxItems
33
-        if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
34
-            $this->addError($path, 'There must be a maximum of ' . $schema->maxItems . ' items in the array', 'maxItems', array('maxItems' => $schema->maxItems));
35
-        }
32
+		// Verify maxItems
33
+		if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
34
+			$this->addError($path, 'There must be a maximum of ' . $schema->maxItems . ' items in the array', 'maxItems', array('maxItems' => $schema->maxItems));
35
+		}
36 36
 
37
-        // Verify uniqueItems
38
-        if (isset($schema->uniqueItems) && $schema->uniqueItems) {
39
-            $unique = $value;
40
-            if (is_array($value) && count($value)) {
41
-                $unique = array_map(function ($e) {
42
-                    return var_export($e, true);
43
-                }, $value);
44
-            }
45
-            if (count(array_unique($unique)) != count($value)) {
46
-                $this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems');
47
-            }
48
-        }
37
+		// Verify uniqueItems
38
+		if (isset($schema->uniqueItems) && $schema->uniqueItems) {
39
+			$unique = $value;
40
+			if (is_array($value) && count($value)) {
41
+				$unique = array_map(function ($e) {
42
+					return var_export($e, true);
43
+				}, $value);
44
+			}
45
+			if (count(array_unique($unique)) != count($value)) {
46
+				$this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems');
47
+			}
48
+		}
49 49
 
50
-        // Verify items
51
-        if (isset($schema->items)) {
52
-            $this->validateItems($value, $schema, $path, $i);
53
-        }
54
-    }
50
+		// Verify items
51
+		if (isset($schema->items)) {
52
+			$this->validateItems($value, $schema, $path, $i);
53
+		}
54
+	}
55 55
 
56
-    /**
57
-     * Validates the items
58
-     *
59
-     * @param array            $value
60
-     * @param \stdClass        $schema
61
-     * @param JsonPointer|null $path
62
-     * @param string           $i
63
-     */
64
-    protected function validateItems(&$value, $schema = null, JsonPointer $path = null, $i = null)
65
-    {
66
-        if (is_object($schema->items)) {
67
-            // just one type definition for the whole array
68
-            foreach ($value as $k => &$v) {
69
-                $initErrors = $this->getErrors();
56
+	/**
57
+	 * Validates the items
58
+	 *
59
+	 * @param array            $value
60
+	 * @param \stdClass        $schema
61
+	 * @param JsonPointer|null $path
62
+	 * @param string           $i
63
+	 */
64
+	protected function validateItems(&$value, $schema = null, JsonPointer $path = null, $i = null)
65
+	{
66
+		if (is_object($schema->items)) {
67
+			// just one type definition for the whole array
68
+			foreach ($value as $k => &$v) {
69
+				$initErrors = $this->getErrors();
70 70
 
71
-                // First check if its defined in "items"
72
-                $this->checkUndefined($v, $schema->items, $path, $k);
71
+				// First check if its defined in "items"
72
+				$this->checkUndefined($v, $schema->items, $path, $k);
73 73
 
74
-                // Recheck with "additionalItems" if the first test fails
75
-                if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
76
-                    $secondErrors = $this->getErrors();
77
-                    $this->checkUndefined($v, $schema->additionalItems, $path, $k);
78
-                }
74
+				// Recheck with "additionalItems" if the first test fails
75
+				if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
76
+					$secondErrors = $this->getErrors();
77
+					$this->checkUndefined($v, $schema->additionalItems, $path, $k);
78
+				}
79 79
 
80
-                // Reset errors if needed
81
-                if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) {
82
-                    $this->errors = $secondErrors;
83
-                } elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) {
84
-                    $this->errors = $initErrors;
85
-                }
86
-            }
87
-            unset($v); /* remove dangling reference to prevent any future bugs
80
+				// Reset errors if needed
81
+				if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) {
82
+					$this->errors = $secondErrors;
83
+				} elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) {
84
+					$this->errors = $initErrors;
85
+				}
86
+			}
87
+			unset($v); /* remove dangling reference to prevent any future bugs
88 88
                         * caused by accidentally using $v elsewhere */
89
-        } else {
90
-            // Defined item type definitions
91
-            foreach ($value as $k => &$v) {
92
-                if (array_key_exists($k, $schema->items)) {
93
-                    $this->checkUndefined($v, $schema->items[$k], $path, $k);
94
-                } else {
95
-                    // Additional items
96
-                    if (property_exists($schema, 'additionalItems')) {
97
-                        if ($schema->additionalItems !== false) {
98
-                            $this->checkUndefined($v, $schema->additionalItems, $path, $k);
99
-                        } else {
100
-                            $this->addError(
101
-                                $path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems));
102
-                        }
103
-                    } else {
104
-                        // Should be valid against an empty schema
105
-                        $this->checkUndefined($v, new \stdClass(), $path, $k);
106
-                    }
107
-                }
108
-            }
109
-            unset($v); /* remove dangling reference to prevent any future bugs
89
+		} else {
90
+			// Defined item type definitions
91
+			foreach ($value as $k => &$v) {
92
+				if (array_key_exists($k, $schema->items)) {
93
+					$this->checkUndefined($v, $schema->items[$k], $path, $k);
94
+				} else {
95
+					// Additional items
96
+					if (property_exists($schema, 'additionalItems')) {
97
+						if ($schema->additionalItems !== false) {
98
+							$this->checkUndefined($v, $schema->additionalItems, $path, $k);
99
+						} else {
100
+							$this->addError(
101
+								$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems));
102
+						}
103
+					} else {
104
+						// Should be valid against an empty schema
105
+						$this->checkUndefined($v, new \stdClass(), $path, $k);
106
+					}
107
+				}
108
+			}
109
+			unset($v); /* remove dangling reference to prevent any future bugs
110 110
                         * caused by accidentally using $v elsewhere */
111 111
 
112
-            // Treat when we have more schema definitions than values, not for empty arrays
113
-            if (count($value) > 0) {
114
-                for ($k = count($value); $k < count($schema->items); $k++) {
115
-                    $undefinedInstance = $this->factory->createInstanceFor('undefined');
116
-                    $this->checkUndefined($undefinedInstance, $schema->items[$k], $path, $k);
117
-                }
118
-            }
119
-        }
120
-    }
112
+			// Treat when we have more schema definitions than values, not for empty arrays
113
+			if (count($value) > 0) {
114
+				for ($k = count($value); $k < count($schema->items); $k++) {
115
+					$undefinedInstance = $this->factory->createInstanceFor('undefined');
116
+					$this->checkUndefined($undefinedInstance, $schema->items[$k], $path, $k);
117
+				}
118
+			}
119
+		}
120
+	}
121 121
 }
Please login to merge, or discard this patch.
vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php 1 patch
Indentation   +196 added lines, -196 removed lines patch added patch discarded remove patch
@@ -19,200 +19,200 @@
 block discarded – undo
19 19
  */
20 20
 abstract class Constraint extends BaseConstraint implements ConstraintInterface
21 21
 {
22
-    protected $inlineSchemaProperty = '$schema';
23
-
24
-    const CHECK_MODE_NONE =             0x00000000;
25
-    const CHECK_MODE_NORMAL =           0x00000001;
26
-    const CHECK_MODE_TYPE_CAST =        0x00000002;
27
-    const CHECK_MODE_COERCE_TYPES =     0x00000004;
28
-    const CHECK_MODE_APPLY_DEFAULTS =   0x00000008;
29
-    const CHECK_MODE_EXCEPTIONS =       0x00000010;
30
-    const CHECK_MODE_DISABLE_FORMAT =   0x00000020;
31
-    const CHECK_MODE_ONLY_REQUIRED_DEFAULTS   = 0x00000080;
32
-    const CHECK_MODE_VALIDATE_SCHEMA =  0x00000100;
33
-
34
-    /**
35
-     * Bubble down the path
36
-     *
37
-     * @param JsonPointer|null $path Current path
38
-     * @param mixed            $i    What to append to the path
39
-     *
40
-     * @return JsonPointer;
41
-     */
42
-    protected function incrementPath(JsonPointer $path = null, $i)
43
-    {
44
-        $path = $path ?: new JsonPointer('');
45
-
46
-        if ($i === null || $i === '') {
47
-            return $path;
48
-        }
49
-
50
-        $path = $path->withPropertyPaths(
51
-            array_merge(
52
-                $path->getPropertyPaths(),
53
-                array($i)
54
-            )
55
-        );
56
-
57
-        return $path;
58
-    }
59
-
60
-    /**
61
-     * Validates an array
62
-     *
63
-     * @param mixed            $value
64
-     * @param mixed            $schema
65
-     * @param JsonPointer|null $path
66
-     * @param mixed            $i
67
-     */
68
-    protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null)
69
-    {
70
-        $validator = $this->factory->createInstanceFor('collection');
71
-        $validator->check($value, $schema, $path, $i);
72
-
73
-        $this->addErrors($validator->getErrors());
74
-    }
75
-
76
-    /**
77
-     * Validates an object
78
-     *
79
-     * @param mixed            $value
80
-     * @param mixed            $schema
81
-     * @param JsonPointer|null $path
82
-     * @param mixed            $properties
83
-     * @param mixed            $additionalProperties
84
-     * @param mixed            $patternProperties
85
-     */
86
-    protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
87
-        $additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
88
-    {
89
-        $validator = $this->factory->createInstanceFor('object');
90
-        $validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
91
-
92
-        $this->addErrors($validator->getErrors());
93
-    }
94
-
95
-    /**
96
-     * Validates the type of a property
97
-     *
98
-     * @param mixed            $value
99
-     * @param mixed            $schema
100
-     * @param JsonPointer|null $path
101
-     * @param mixed            $i
102
-     */
103
-    protected function checkType(&$value, $schema = null, JsonPointer $path = null, $i = null)
104
-    {
105
-        $validator = $this->factory->createInstanceFor('type');
106
-        $validator->check($value, $schema, $path, $i);
107
-
108
-        $this->addErrors($validator->getErrors());
109
-    }
110
-
111
-    /**
112
-     * Checks a undefined element
113
-     *
114
-     * @param mixed            $value
115
-     * @param mixed            $schema
116
-     * @param JsonPointer|null $path
117
-     * @param mixed            $i
118
-     */
119
-    protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false)
120
-    {
121
-        $validator = $this->factory->createInstanceFor('undefined');
122
-
123
-        $validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $fromDefault);
124
-
125
-        $this->addErrors($validator->getErrors());
126
-    }
127
-
128
-    /**
129
-     * Checks a string element
130
-     *
131
-     * @param mixed            $value
132
-     * @param mixed            $schema
133
-     * @param JsonPointer|null $path
134
-     * @param mixed            $i
135
-     */
136
-    protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
137
-    {
138
-        $validator = $this->factory->createInstanceFor('string');
139
-        $validator->check($value, $schema, $path, $i);
140
-
141
-        $this->addErrors($validator->getErrors());
142
-    }
143
-
144
-    /**
145
-     * Checks a number element
146
-     *
147
-     * @param mixed       $value
148
-     * @param mixed       $schema
149
-     * @param JsonPointer $path
150
-     * @param mixed       $i
151
-     */
152
-    protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
153
-    {
154
-        $validator = $this->factory->createInstanceFor('number');
155
-        $validator->check($value, $schema, $path, $i);
156
-
157
-        $this->addErrors($validator->getErrors());
158
-    }
159
-
160
-    /**
161
-     * Checks a enum element
162
-     *
163
-     * @param mixed            $value
164
-     * @param mixed            $schema
165
-     * @param JsonPointer|null $path
166
-     * @param mixed            $i
167
-     */
168
-    protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
169
-    {
170
-        $validator = $this->factory->createInstanceFor('enum');
171
-        $validator->check($value, $schema, $path, $i);
172
-
173
-        $this->addErrors($validator->getErrors());
174
-    }
175
-
176
-    /**
177
-     * Checks format of an element
178
-     *
179
-     * @param mixed            $value
180
-     * @param mixed            $schema
181
-     * @param JsonPointer|null $path
182
-     * @param mixed            $i
183
-     */
184
-    protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
185
-    {
186
-        $validator = $this->factory->createInstanceFor('format');
187
-        $validator->check($value, $schema, $path, $i);
188
-
189
-        $this->addErrors($validator->getErrors());
190
-    }
191
-
192
-    /**
193
-     * Get the type check based on the set check mode.
194
-     *
195
-     * @return TypeCheck\TypeCheckInterface
196
-     */
197
-    protected function getTypeCheck()
198
-    {
199
-        return $this->factory->getTypeCheck();
200
-    }
201
-
202
-    /**
203
-     * @param JsonPointer $pointer
204
-     *
205
-     * @return string property path
206
-     */
207
-    protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
208
-    {
209
-        $result = array_map(
210
-            function ($path) {
211
-                return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
212
-            },
213
-            $pointer->getPropertyPaths()
214
-        );
215
-
216
-        return trim(implode('', $result), '.');
217
-    }
22
+	protected $inlineSchemaProperty = '$schema';
23
+
24
+	const CHECK_MODE_NONE =             0x00000000;
25
+	const CHECK_MODE_NORMAL =           0x00000001;
26
+	const CHECK_MODE_TYPE_CAST =        0x00000002;
27
+	const CHECK_MODE_COERCE_TYPES =     0x00000004;
28
+	const CHECK_MODE_APPLY_DEFAULTS =   0x00000008;
29
+	const CHECK_MODE_EXCEPTIONS =       0x00000010;
30
+	const CHECK_MODE_DISABLE_FORMAT =   0x00000020;
31
+	const CHECK_MODE_ONLY_REQUIRED_DEFAULTS   = 0x00000080;
32
+	const CHECK_MODE_VALIDATE_SCHEMA =  0x00000100;
33
+
34
+	/**
35
+	 * Bubble down the path
36
+	 *
37
+	 * @param JsonPointer|null $path Current path
38
+	 * @param mixed            $i    What to append to the path
39
+	 *
40
+	 * @return JsonPointer;
41
+	 */
42
+	protected function incrementPath(JsonPointer $path = null, $i)
43
+	{
44
+		$path = $path ?: new JsonPointer('');
45
+
46
+		if ($i === null || $i === '') {
47
+			return $path;
48
+		}
49
+
50
+		$path = $path->withPropertyPaths(
51
+			array_merge(
52
+				$path->getPropertyPaths(),
53
+				array($i)
54
+			)
55
+		);
56
+
57
+		return $path;
58
+	}
59
+
60
+	/**
61
+	 * Validates an array
62
+	 *
63
+	 * @param mixed            $value
64
+	 * @param mixed            $schema
65
+	 * @param JsonPointer|null $path
66
+	 * @param mixed            $i
67
+	 */
68
+	protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null)
69
+	{
70
+		$validator = $this->factory->createInstanceFor('collection');
71
+		$validator->check($value, $schema, $path, $i);
72
+
73
+		$this->addErrors($validator->getErrors());
74
+	}
75
+
76
+	/**
77
+	 * Validates an object
78
+	 *
79
+	 * @param mixed            $value
80
+	 * @param mixed            $schema
81
+	 * @param JsonPointer|null $path
82
+	 * @param mixed            $properties
83
+	 * @param mixed            $additionalProperties
84
+	 * @param mixed            $patternProperties
85
+	 */
86
+	protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
87
+		$additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
88
+	{
89
+		$validator = $this->factory->createInstanceFor('object');
90
+		$validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
91
+
92
+		$this->addErrors($validator->getErrors());
93
+	}
94
+
95
+	/**
96
+	 * Validates the type of a property
97
+	 *
98
+	 * @param mixed            $value
99
+	 * @param mixed            $schema
100
+	 * @param JsonPointer|null $path
101
+	 * @param mixed            $i
102
+	 */
103
+	protected function checkType(&$value, $schema = null, JsonPointer $path = null, $i = null)
104
+	{
105
+		$validator = $this->factory->createInstanceFor('type');
106
+		$validator->check($value, $schema, $path, $i);
107
+
108
+		$this->addErrors($validator->getErrors());
109
+	}
110
+
111
+	/**
112
+	 * Checks a undefined element
113
+	 *
114
+	 * @param mixed            $value
115
+	 * @param mixed            $schema
116
+	 * @param JsonPointer|null $path
117
+	 * @param mixed            $i
118
+	 */
119
+	protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false)
120
+	{
121
+		$validator = $this->factory->createInstanceFor('undefined');
122
+
123
+		$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $fromDefault);
124
+
125
+		$this->addErrors($validator->getErrors());
126
+	}
127
+
128
+	/**
129
+	 * Checks a string element
130
+	 *
131
+	 * @param mixed            $value
132
+	 * @param mixed            $schema
133
+	 * @param JsonPointer|null $path
134
+	 * @param mixed            $i
135
+	 */
136
+	protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
137
+	{
138
+		$validator = $this->factory->createInstanceFor('string');
139
+		$validator->check($value, $schema, $path, $i);
140
+
141
+		$this->addErrors($validator->getErrors());
142
+	}
143
+
144
+	/**
145
+	 * Checks a number element
146
+	 *
147
+	 * @param mixed       $value
148
+	 * @param mixed       $schema
149
+	 * @param JsonPointer $path
150
+	 * @param mixed       $i
151
+	 */
152
+	protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
153
+	{
154
+		$validator = $this->factory->createInstanceFor('number');
155
+		$validator->check($value, $schema, $path, $i);
156
+
157
+		$this->addErrors($validator->getErrors());
158
+	}
159
+
160
+	/**
161
+	 * Checks a enum element
162
+	 *
163
+	 * @param mixed            $value
164
+	 * @param mixed            $schema
165
+	 * @param JsonPointer|null $path
166
+	 * @param mixed            $i
167
+	 */
168
+	protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
169
+	{
170
+		$validator = $this->factory->createInstanceFor('enum');
171
+		$validator->check($value, $schema, $path, $i);
172
+
173
+		$this->addErrors($validator->getErrors());
174
+	}
175
+
176
+	/**
177
+	 * Checks format of an element
178
+	 *
179
+	 * @param mixed            $value
180
+	 * @param mixed            $schema
181
+	 * @param JsonPointer|null $path
182
+	 * @param mixed            $i
183
+	 */
184
+	protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
185
+	{
186
+		$validator = $this->factory->createInstanceFor('format');
187
+		$validator->check($value, $schema, $path, $i);
188
+
189
+		$this->addErrors($validator->getErrors());
190
+	}
191
+
192
+	/**
193
+	 * Get the type check based on the set check mode.
194
+	 *
195
+	 * @return TypeCheck\TypeCheckInterface
196
+	 */
197
+	protected function getTypeCheck()
198
+	{
199
+		return $this->factory->getTypeCheck();
200
+	}
201
+
202
+	/**
203
+	 * @param JsonPointer $pointer
204
+	 *
205
+	 * @return string property path
206
+	 */
207
+	protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
208
+	{
209
+		$result = array_map(
210
+			function ($path) {
211
+				return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
212
+			},
213
+			$pointer->getPropertyPaths()
214
+		);
215
+
216
+		return trim(implode('', $result), '.');
217
+	}
218 218
 }
Please login to merge, or discard this patch.
justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -19,36 +19,36 @@
 block discarded – undo
19 19
  */
20 20
 class EnumConstraint extends Constraint
21 21
 {
22
-    /**
23
-     * {@inheritdoc}
24
-     */
25
-    public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
26
-    {
27
-        // Only validate enum if the attribute exists
28
-        if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
29
-            return;
30
-        }
31
-        $type = gettype($element);
22
+	/**
23
+	 * {@inheritdoc}
24
+	 */
25
+	public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
26
+	{
27
+		// Only validate enum if the attribute exists
28
+		if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
29
+			return;
30
+		}
31
+		$type = gettype($element);
32 32
 
33
-        foreach ($schema->enum as $enum) {
34
-            $enumType = gettype($enum);
35
-            if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
36
-                if ((object) $element == $enum) {
37
-                    return;
38
-                }
39
-            }
33
+		foreach ($schema->enum as $enum) {
34
+			$enumType = gettype($enum);
35
+			if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
36
+				if ((object) $element == $enum) {
37
+					return;
38
+				}
39
+			}
40 40
 
41
-            if ($type === gettype($enum)) {
42
-                if ($type == 'object') {
43
-                    if ($element == $enum) {
44
-                        return;
45
-                    }
46
-                } elseif ($element === $enum) {
47
-                    return;
48
-                }
49
-            }
50
-        }
41
+			if ($type === gettype($enum)) {
42
+				if ($type == 'object') {
43
+					if ($element == $enum) {
44
+						return;
45
+					}
46
+				} elseif ($element === $enum) {
47
+					return;
48
+				}
49
+			}
50
+		}
51 51
 
52
-        $this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array('enum' => $schema->enum));
53
-    }
52
+		$this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array('enum' => $schema->enum));
53
+	}
54 54
 }
Please login to merge, or discard this patch.
justinrainbow/json-schema/src/JsonSchema/Constraints/TypeConstraint.php 1 patch
Indentation   +237 added lines, -237 removed lines patch added patch discarded remove patch
@@ -21,241 +21,241 @@
 block discarded – undo
21 21
  */
22 22
 class TypeConstraint extends Constraint
23 23
 {
24
-    /**
25
-     * @var array|string[] type wordings for validation error messages
26
-     */
27
-    public static $wording = array(
28
-        'integer' => 'an integer',
29
-        'number'  => 'a number',
30
-        'boolean' => 'a boolean',
31
-        'object'  => 'an object',
32
-        'array'   => 'an array',
33
-        'string'  => 'a string',
34
-        'null'    => 'a null',
35
-        'any'     => null, // validation of 'any' is always true so is not needed in message wording
36
-        0         => null, // validation of a false-y value is always true, so not needed as well
37
-    );
38
-
39
-    /**
40
-     * {@inheritdoc}
41
-     */
42
-    public function check(&$value = null, $schema = null, JsonPointer $path = null, $i = null)
43
-    {
44
-        $type = isset($schema->type) ? $schema->type : null;
45
-        $isValid = false;
46
-        $wording = array();
47
-
48
-        if (is_array($type)) {
49
-            $this->validateTypesArray($value, $type, $wording, $isValid, $path);
50
-        } elseif (is_object($type)) {
51
-            $this->checkUndefined($value, $type, $path);
52
-
53
-            return;
54
-        } else {
55
-            $isValid = $this->validateType($value, $type);
56
-        }
57
-
58
-        if ($isValid === false) {
59
-            if (!is_array($type)) {
60
-                $this->validateTypeNameWording($type);
61
-                $wording[] = self::$wording[$type];
62
-            }
63
-            $this->addError($path, ucwords(gettype($value)) . ' value found, but ' .
64
-                $this->implodeWith($wording, ', ', 'or') . ' is required', 'type');
65
-        }
66
-    }
67
-
68
-    /**
69
-     * Validates the given $value against the array of types in $type. Sets the value
70
-     * of $isValid to true, if at least one $type mateches the type of $value or the value
71
-     * passed as $isValid is already true.
72
-     *
73
-     * @param mixed $value             Value to validate
74
-     * @param array $type              TypeConstraints to check agains
75
-     * @param array $validTypesWording An array of wordings of the valid types of the array $type
76
-     * @param bool  $isValid           The current validation value
77
-     * @param $path
78
-     */
79
-    protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path)
80
-    {
81
-        foreach ($type as $tp) {
82
-            // $tp can be an object, if it's a schema instead of a simple type, validate it
83
-            // with a new type constraint
84
-            if (is_object($tp)) {
85
-                if (!$isValid) {
86
-                    $validator = $this->factory->createInstanceFor('type');
87
-                    $subSchema = new \stdClass();
88
-                    $subSchema->type = $tp;
89
-                    $validator->check($value, $subSchema, $path, null);
90
-                    $error = $validator->getErrors();
91
-                    $isValid = !(bool) $error;
92
-                    $validTypesWording[] = self::$wording['object'];
93
-                }
94
-            } else {
95
-                $this->validateTypeNameWording($tp);
96
-                $validTypesWording[] = self::$wording[$tp];
97
-                if (!$isValid) {
98
-                    $isValid = $this->validateType($value, $tp);
99
-                }
100
-            }
101
-        }
102
-    }
103
-
104
-    /**
105
-     * Implodes the given array like implode() with turned around parameters and with the
106
-     * difference, that, if $listEnd isn't false, the last element delimiter is $listEnd instead of
107
-     * $delimiter.
108
-     *
109
-     * @param array  $elements  The elements to implode
110
-     * @param string $delimiter The delimiter to use
111
-     * @param bool   $listEnd   The last delimiter to use (defaults to $delimiter)
112
-     *
113
-     * @return string
114
-     */
115
-    protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = false)
116
-    {
117
-        if ($listEnd === false || !isset($elements[1])) {
118
-            return implode($delimiter, $elements);
119
-        }
120
-        $lastElement  = array_slice($elements, -1);
121
-        $firsElements = join($delimiter, array_slice($elements, 0, -1));
122
-        $implodedElements = array_merge(array($firsElements), $lastElement);
123
-
124
-        return join(" $listEnd ", $implodedElements);
125
-    }
126
-
127
-    /**
128
-     * Validates the given $type, if there's an associated self::$wording. If not, throws an
129
-     * exception.
130
-     *
131
-     * @param string $type The type to validate
132
-     *
133
-     * @throws StandardUnexpectedValueException
134
-     */
135
-    protected function validateTypeNameWording($type)
136
-    {
137
-        if (!isset(self::$wording[$type])) {
138
-            throw new StandardUnexpectedValueException(
139
-                sprintf(
140
-                    'No wording for %s available, expected wordings are: [%s]',
141
-                    var_export($type, true),
142
-                    implode(', ', array_filter(self::$wording)))
143
-            );
144
-        }
145
-    }
146
-
147
-    /**
148
-     * Verifies that a given value is of a certain type
149
-     *
150
-     * @param mixed  $value Value to validate
151
-     * @param string $type  TypeConstraint to check against
152
-     *
153
-     * @throws InvalidArgumentException
154
-     *
155
-     * @return bool
156
-     */
157
-    protected function validateType(&$value, $type)
158
-    {
159
-        //mostly the case for inline schema
160
-        if (!$type) {
161
-            return true;
162
-        }
163
-
164
-        if ('any' === $type) {
165
-            return true;
166
-        }
167
-
168
-        if ('object' === $type) {
169
-            return $this->getTypeCheck()->isObject($value);
170
-        }
171
-
172
-        if ('array' === $type) {
173
-            return $this->getTypeCheck()->isArray($value);
174
-        }
175
-
176
-        $coerce = $this->factory->getConfig(Constraint::CHECK_MODE_COERCE_TYPES);
177
-
178
-        if ('integer' === $type) {
179
-            if ($coerce) {
180
-                $value = $this->toInteger($value);
181
-            }
182
-
183
-            return is_int($value);
184
-        }
185
-
186
-        if ('number' === $type) {
187
-            if ($coerce) {
188
-                $value = $this->toNumber($value);
189
-            }
190
-
191
-            return is_numeric($value) && !is_string($value);
192
-        }
193
-
194
-        if ('boolean' === $type) {
195
-            if ($coerce) {
196
-                $value = $this->toBoolean($value);
197
-            }
198
-
199
-            return is_bool($value);
200
-        }
201
-
202
-        if ('string' === $type) {
203
-            return is_string($value);
204
-        }
205
-
206
-        if ('email' === $type) {
207
-            return is_string($value);
208
-        }
209
-
210
-        if ('null' === $type) {
211
-            return is_null($value);
212
-        }
213
-
214
-        throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type);
215
-    }
216
-
217
-    /**
218
-     * Converts a value to boolean. For example, "true" becomes true.
219
-     *
220
-     * @param $value The value to convert to boolean
221
-     *
222
-     * @return bool|mixed
223
-     */
224
-    protected function toBoolean($value)
225
-    {
226
-        if ($value === 'true') {
227
-            return true;
228
-        }
229
-
230
-        if ($value === 'false') {
231
-            return false;
232
-        }
233
-
234
-        return $value;
235
-    }
236
-
237
-    /**
238
-     * Converts a numeric string to a number. For example, "4" becomes 4.
239
-     *
240
-     * @param mixed $value the value to convert to a number
241
-     *
242
-     * @return int|float|mixed
243
-     */
244
-    protected function toNumber($value)
245
-    {
246
-        if (is_numeric($value)) {
247
-            return $value + 0; // cast to number
248
-        }
249
-
250
-        return $value;
251
-    }
252
-
253
-    protected function toInteger($value)
254
-    {
255
-        if (is_numeric($value) && (int) $value == $value) {
256
-            return (int) $value; // cast to number
257
-        }
258
-
259
-        return $value;
260
-    }
24
+	/**
25
+	 * @var array|string[] type wordings for validation error messages
26
+	 */
27
+	public static $wording = array(
28
+		'integer' => 'an integer',
29
+		'number'  => 'a number',
30
+		'boolean' => 'a boolean',
31
+		'object'  => 'an object',
32
+		'array'   => 'an array',
33
+		'string'  => 'a string',
34
+		'null'    => 'a null',
35
+		'any'     => null, // validation of 'any' is always true so is not needed in message wording
36
+		0         => null, // validation of a false-y value is always true, so not needed as well
37
+	);
38
+
39
+	/**
40
+	 * {@inheritdoc}
41
+	 */
42
+	public function check(&$value = null, $schema = null, JsonPointer $path = null, $i = null)
43
+	{
44
+		$type = isset($schema->type) ? $schema->type : null;
45
+		$isValid = false;
46
+		$wording = array();
47
+
48
+		if (is_array($type)) {
49
+			$this->validateTypesArray($value, $type, $wording, $isValid, $path);
50
+		} elseif (is_object($type)) {
51
+			$this->checkUndefined($value, $type, $path);
52
+
53
+			return;
54
+		} else {
55
+			$isValid = $this->validateType($value, $type);
56
+		}
57
+
58
+		if ($isValid === false) {
59
+			if (!is_array($type)) {
60
+				$this->validateTypeNameWording($type);
61
+				$wording[] = self::$wording[$type];
62
+			}
63
+			$this->addError($path, ucwords(gettype($value)) . ' value found, but ' .
64
+				$this->implodeWith($wording, ', ', 'or') . ' is required', 'type');
65
+		}
66
+	}
67
+
68
+	/**
69
+	 * Validates the given $value against the array of types in $type. Sets the value
70
+	 * of $isValid to true, if at least one $type mateches the type of $value or the value
71
+	 * passed as $isValid is already true.
72
+	 *
73
+	 * @param mixed $value             Value to validate
74
+	 * @param array $type              TypeConstraints to check agains
75
+	 * @param array $validTypesWording An array of wordings of the valid types of the array $type
76
+	 * @param bool  $isValid           The current validation value
77
+	 * @param $path
78
+	 */
79
+	protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path)
80
+	{
81
+		foreach ($type as $tp) {
82
+			// $tp can be an object, if it's a schema instead of a simple type, validate it
83
+			// with a new type constraint
84
+			if (is_object($tp)) {
85
+				if (!$isValid) {
86
+					$validator = $this->factory->createInstanceFor('type');
87
+					$subSchema = new \stdClass();
88
+					$subSchema->type = $tp;
89
+					$validator->check($value, $subSchema, $path, null);
90
+					$error = $validator->getErrors();
91
+					$isValid = !(bool) $error;
92
+					$validTypesWording[] = self::$wording['object'];
93
+				}
94
+			} else {
95
+				$this->validateTypeNameWording($tp);
96
+				$validTypesWording[] = self::$wording[$tp];
97
+				if (!$isValid) {
98
+					$isValid = $this->validateType($value, $tp);
99
+				}
100
+			}
101
+		}
102
+	}
103
+
104
+	/**
105
+	 * Implodes the given array like implode() with turned around parameters and with the
106
+	 * difference, that, if $listEnd isn't false, the last element delimiter is $listEnd instead of
107
+	 * $delimiter.
108
+	 *
109
+	 * @param array  $elements  The elements to implode
110
+	 * @param string $delimiter The delimiter to use
111
+	 * @param bool   $listEnd   The last delimiter to use (defaults to $delimiter)
112
+	 *
113
+	 * @return string
114
+	 */
115
+	protected function implodeWith(array $elements, $delimiter = ', ', $listEnd = false)
116
+	{
117
+		if ($listEnd === false || !isset($elements[1])) {
118
+			return implode($delimiter, $elements);
119
+		}
120
+		$lastElement  = array_slice($elements, -1);
121
+		$firsElements = join($delimiter, array_slice($elements, 0, -1));
122
+		$implodedElements = array_merge(array($firsElements), $lastElement);
123
+
124
+		return join(" $listEnd ", $implodedElements);
125
+	}
126
+
127
+	/**
128
+	 * Validates the given $type, if there's an associated self::$wording. If not, throws an
129
+	 * exception.
130
+	 *
131
+	 * @param string $type The type to validate
132
+	 *
133
+	 * @throws StandardUnexpectedValueException
134
+	 */
135
+	protected function validateTypeNameWording($type)
136
+	{
137
+		if (!isset(self::$wording[$type])) {
138
+			throw new StandardUnexpectedValueException(
139
+				sprintf(
140
+					'No wording for %s available, expected wordings are: [%s]',
141
+					var_export($type, true),
142
+					implode(', ', array_filter(self::$wording)))
143
+			);
144
+		}
145
+	}
146
+
147
+	/**
148
+	 * Verifies that a given value is of a certain type
149
+	 *
150
+	 * @param mixed  $value Value to validate
151
+	 * @param string $type  TypeConstraint to check against
152
+	 *
153
+	 * @throws InvalidArgumentException
154
+	 *
155
+	 * @return bool
156
+	 */
157
+	protected function validateType(&$value, $type)
158
+	{
159
+		//mostly the case for inline schema
160
+		if (!$type) {
161
+			return true;
162
+		}
163
+
164
+		if ('any' === $type) {
165
+			return true;
166
+		}
167
+
168
+		if ('object' === $type) {
169
+			return $this->getTypeCheck()->isObject($value);
170
+		}
171
+
172
+		if ('array' === $type) {
173
+			return $this->getTypeCheck()->isArray($value);
174
+		}
175
+
176
+		$coerce = $this->factory->getConfig(Constraint::CHECK_MODE_COERCE_TYPES);
177
+
178
+		if ('integer' === $type) {
179
+			if ($coerce) {
180
+				$value = $this->toInteger($value);
181
+			}
182
+
183
+			return is_int($value);
184
+		}
185
+
186
+		if ('number' === $type) {
187
+			if ($coerce) {
188
+				$value = $this->toNumber($value);
189
+			}
190
+
191
+			return is_numeric($value) && !is_string($value);
192
+		}
193
+
194
+		if ('boolean' === $type) {
195
+			if ($coerce) {
196
+				$value = $this->toBoolean($value);
197
+			}
198
+
199
+			return is_bool($value);
200
+		}
201
+
202
+		if ('string' === $type) {
203
+			return is_string($value);
204
+		}
205
+
206
+		if ('email' === $type) {
207
+			return is_string($value);
208
+		}
209
+
210
+		if ('null' === $type) {
211
+			return is_null($value);
212
+		}
213
+
214
+		throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type);
215
+	}
216
+
217
+	/**
218
+	 * Converts a value to boolean. For example, "true" becomes true.
219
+	 *
220
+	 * @param $value The value to convert to boolean
221
+	 *
222
+	 * @return bool|mixed
223
+	 */
224
+	protected function toBoolean($value)
225
+	{
226
+		if ($value === 'true') {
227
+			return true;
228
+		}
229
+
230
+		if ($value === 'false') {
231
+			return false;
232
+		}
233
+
234
+		return $value;
235
+	}
236
+
237
+	/**
238
+	 * Converts a numeric string to a number. For example, "4" becomes 4.
239
+	 *
240
+	 * @param mixed $value the value to convert to a number
241
+	 *
242
+	 * @return int|float|mixed
243
+	 */
244
+	protected function toNumber($value)
245
+	{
246
+		if (is_numeric($value)) {
247
+			return $value + 0; // cast to number
248
+		}
249
+
250
+		return $value;
251
+	}
252
+
253
+	protected function toInteger($value)
254
+	{
255
+		if (is_numeric($value) && (int) $value == $value) {
256
+			return (int) $value; // cast to number
257
+		}
258
+
259
+		return $value;
260
+	}
261 261
 }
Please login to merge, or discard this patch.