1: <?php
2:
3: namespace NNV\OneSignal\Utils;
4:
5: use Symfony\Component\OptionsResolver\OptionsResolver;
6:
7: 8: 9:
10: class Validation
11: {
12: 13: 14: 15: 16:
17: private $optionsResolver;
18:
19: 20: 21:
22: public function __construct()
23: {
24: $this->optionsResolver = new OptionsResolver();
25: }
26:
27: 28: 29: 30: 31: 32: 33:
34: public function __call($methodName, $methodArgs)
35: {
36: $selfMethods = ['setMultiRequired', 'setMultiDefined', 'validate'];
37:
38: if (in_array($methodName, $selfMethods)) {
39: return call_user_func_array([$this, $methodName], $methodArgs);
40: }
41:
42: return call_user_func_array([$this->optionsResolver, $methodName], $methodArgs);
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function setMultiRequired(array $requiredOptions)
52: {
53: foreach ($requiredOptions as $requiredOption) {
54: $this->optionsResolver->setRequired($requiredOption);
55: }
56:
57: return $this;
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function setMultiDefined(array $definedOptions)
67: {
68: foreach ($definedOptions as $definedKey => $definedOption) {
69: $this->optionsResolver->setDefined($definedKey);
70: $allowedTypes = null;
71:
72: if ($definedOption && isset($definedOption['allowedTypes'])) {
73: $allowedTypes = $definedOption['allowedTypes'];
74: } elseif ($definedOption && !isset($definedOption['allowedValues'])) {
75: $allowedTypes = $definedOption;
76: }
77: if ($allowedTypes) {
78: $this->optionsResolver->setAllowedTypes($definedKey, $allowedTypes);
79: }
80: if (isset($definedOption['allowedValues'])) {
81: $this->optionsResolver->setAllowedValues($definedKey, $definedOption['allowedValues']);
82: }
83: }
84:
85: return $this;
86: }
87:
88: 89: 90: 91: 92: 93:
94: public function validate(array $data)
95: {
96: return $this->optionsResolver->resolve($data);
97: }
98: }
99: