1: <?php
2: /*
3: * The MIT License
4: *
5: * Copyright 2017 David Schoenbauer.
6: *
7: * Permission is hereby granted, free of charge, to any person obtaining a copy
8: * of this software and associated documentation files (the "Software"), to deal
9: * in the Software without restriction, including without limitation the rights
10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11: * copies of the Software, and to permit persons to whom the Software is
12: * furnished to do so, subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included in
15: * all copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23: * THE SOFTWARE.
24: */
25: namespace DSchoenbauer\Orm\Events\Validate;
26:
27: use DSchoenbauer\Orm\Events\AbstractEvent;
28: use DSchoenbauer\Orm\Exception\InvalidDataTypeException;
29: use DSchoenbauer\Orm\Model;
30: use Zend\EventManager\Event;
31:
32: /**
33: * Framework for validating data types
34: *
35: * @author David Schoenbauer <dschoenbauer@gmail.com>
36: */
37: abstract class AbstractValidate extends AbstractEvent
38: {
39:
40: protected $model;
41: protected $params;
42:
43: /**
44: * full name space of an interface that defines a given field type
45: * @return string
46: * @since v1.0.0
47: */
48: abstract public function getTypeInterface();
49:
50: /**
51: * returns the fields affected by the entity interface
52: * @param mixed $entity an entity object implements the getTypeInterface
53: * @return array an array of fields that are relevant to the interface
54: * @since v1.0.0
55: */
56: abstract public function getFields($entity);
57:
58: /**
59: * method is called when a given event is triggered
60: * @param Event $event Event object passed at time of triggering
61: * @throws InvalidDataTypeException thrown when value does not validate
62: * @return void
63: * @since v1.0.0
64: */
65: public function onExecute(Event $event)
66: {
67: if (!$event->getTarget() instanceof Model) {
68: return;
69: }
70: $this->setModel($event->getTarget());
71: $this->setParams($event->getParams());
72:
73: $entity = $this->getModel()->getEntity();
74: if (!is_a($entity, $this->getTypeInterface())) {
75: return;
76: }
77: if (!$this->preExecuteCheck()) {
78: return;
79: }
80:
81: $this->validate($this->getModel()->getData(), $this->getFields($entity));
82: }
83:
84: /**
85: * used to check if they validate function is required or relevant
86: * @return boolean
87: */
88: public function preExecuteCheck()
89: {
90: return true;
91: }
92:
93: /**
94: * validates data against list of fields
95: * @param array $data associative array of data to be validated
96: * @param array $fields fields that are deemed a given type
97: * @return boolean
98: * @since v1.0.0
99: */
100: abstract public function validate(array $data, array $fields);
101:
102: /**
103: * provides model for which data type exists
104: * @return Model
105: * @since v1.0.0
106: */
107: public function getModel()
108: {
109: return $this->model;
110: }
111:
112: /**
113: * set model for which data type exists
114: * @param Model $model
115: * @return AbstractValidate
116: * @since v1.0.0
117: */
118: public function setModel(Model $model)
119: {
120: $this->model = $model;
121: return $this;
122: }
123:
124: /**
125: * Get all parameters
126: * @return array|object|ArrayAccess
127: * @since v1.0.0
128: */
129: public function getParams()
130: {
131: return $this->params;
132: }
133:
134: /**
135: * Set parameters
136: *
137: * @param array|ArrayAccess|object $params
138: * @since v1.0.0
139: */
140: public function setParams($params)
141: {
142: $this->params = $params;
143: return $this;
144: }
145: }
146: