/** * Connect events to dispatcher * @param EventDispatcherInterface $dispatcher * @param object|array $connector * <br><u>Available formats</u>:<br> * Associative array: event to methods * <p><pre>[{event_name} => {method_name}, ...]</pre></p> * List of events * <p><pre>[{event_name1}, {event_name2}, ...]</pre></p> * Object with constants containing 'event' keyword * <p>Search list of class constants and matches any name or value that contains 'event' keyword<br> * If value is matched it will be trimmed from 'event' keyword otherwise raw value is used</p> * * @return self */ protected function connect(EventDispatcherInterface $eventDispatcher, $connector) : self { $methods = get_class_methods($this); $methodsMap = []; $mapper = []; $normalizedEventName = function ($event) { $eventName = strtr($event, '_', ''); if (($pos = mb_stripos($eventName, 'event')) !== false) { $event = mb_substr($eventName, $pos + 5); } return mb_strtolower($event); }; foreach ($methods as $method) { $methodsMap[$method] = mb_strtolower($method); } if (is_object($connector) || is_string($connector)) { $reflection = new \ReflectionClass($connector); $constants = $reflection->getConstants(); foreach ($constants as $const => $event) { if (($method = array_search($normalizedEventName($event), $methodsMap)) !== false || ($method = array_search($normalizedEventName($const), $methodsMap)) !== false) { $mapper[$event][] = $method; } } } elseif (is_array($connector)) { if (ArrayHelper::isAssoc($connector)) { foreach ($connector as $event => $method) { $mapper[$event] = ArrayHelper::forceArray($method); } } else { foreach ($connector as $event) { if (($method = array_search($normalizedEventName($event), $methodsMap)) !== false) { $mapper[$event][] = $method; } } } } foreach ($mapper as $eventName => $methods) { foreach ($methods as $method) { $eventDispatcher->addListener($eventName, [$this, $method]); } } return $this; }
/** * * {@inheritdoc} * * @see \Framework\Validation\Interfaces\ValidatorInterface::validate() */ public function validate($data) : ValidatorInterface { $valid = !empty($data); if (is_array($data)) { foreach ($this->config as $field => $rule) { $rules = ArrayHelper::forceArray($rule); foreach ($rules as $r) { /* @var $r RuleInterface */ if (!is_object($r) || !$r instanceof RuleInterface) { throw new \InvalidArgumentException("Rule must be an object implementing RuleInterface"); } if (!$r->check($data[$field])) { $valid = false; break 2; } } } } $this->valid = $valid; return $this; }
/** * Read operation * * @param array|string|null $filter * If filter is string then single data element is returned otherwise filtered data array * @return mixed */ public function read($filter = null) { if (is_null($this->id)) { return null; } $fields = $this->getStorage()->getFields(); $getData = true; if (is_null($this->data)) { $this->data = []; } if (is_null($filter)) { // If we have partialy data and need full if (count($this->data + ArrayHelper::forceArray($this->id)) < count($fields)) { $filter = array_diff($fields, array_keys($this->data)); } } $filterArray = is_null($filter) ? $fields : ArrayHelper::forceArray($filter); // Check if we have needed data loaded $neededData = array_diff($filterArray, array_keys($this->data)); if (count($neededData) > 0) { $filter = $neededData; } else { // If we have needed data skip storage get $getData = false; } if ($getData) { $this->getDataFromStorage($filterArray); } return !is_null($filter) && (is_string($filter) || is_int($filter)) ? $this->data[$filter] : (is_null($filter) ? $this->data : array_intersect_key($this->data, array_flip($filter))); }
/** * Prepare columns query part<br> * If columns are associative array then they should be in form of: [{column_name} => {column_alias}, ...] * @param array|string $columns */ protected function prepareColumnsList($columns) { if (is_array($columns)) { if (ArrayHelper::isAssoc($columns)) { array_walk($columns, function (&$v, $k) { $v = "{$this->getDatabase()->escapeColumnName($k)} as {$v}"; }); return implode(', ', $columns); } else { return implode(', ', array_map([$this->getDatabase(), 'escapeColumnName'], $columns)); } } return $columns; }
/** * * {@inheritdoc} * * @see \Minwork\Storage\Interfaces\DatabaseStorageInterface::getFields() */ public function getFields() : array { return array_values(array_diff($this->getColumns(), ArrayHelper::forceArray($this->getIdField()))); }
/** * * {@inheritDoc} * * @see \Minwork\Storage\Interfaces\StorageInterface::isset($key) */ public function isset($key) : bool { return !is_null(ArrayHelper::handleElementByKeys($this->array, ArrayHelper::forceArray($key))); }
/** * Check if there were any errors added * * @return boolean */ public function hasErrors() : bool { return !ArrayHelper::isEmpty($this->list); }
public function __construct($routing) { $this->reset()->setRouting(ArrayHelper::forceArray($routing)); }