/** * Returns format for defined $dateElement. * * @param string $dateElement Possible values are: date, year, month, day, time, hour, minutes, seconds, meridiem. * * @return mixed */ private function getFormatFor($dateElement) { if ($this->dateTimeFormat->keyExists($dateElement)) { return $this->dateTimeFormat->key($dateElement); } return self::$formatters->key($dateElement)->first(); }
/** * Instantiate service using given service name * * @param string $serviceName * * @return object * @throws ServiceManagerException */ private function instantiateService($serviceName) { // Make sure service is registered if (!$this->registeredServices->keyExists($serviceName)) { throw new ServiceManagerException(ServiceManagerException::SERVICE_DEFINITION_NOT_FOUND, [$serviceName]); } // Get service config from registered services array $config = $this->registeredServices->key($serviceName); // Check circular referencing if ($this->references->keyExists($serviceName)) { throw new ServiceManagerException(ServiceManagerException::SERVICE_CIRCULAR_REFERENCE, [$serviceName]); } // Set service name reference for circular referencing checks $this->references->key($serviceName, $serviceName); // Compile ConfigObject into ServiceConfig $configCompiler = new ConfigCompiler($serviceName, $config, $this->parameters); $this->compiledConfig->key($serviceName, $configCompiler->compile()); /** * @var $config ServiceConfig */ $config = $this->compiledConfig->key($serviceName); // Construct service container and get service instance $serviceCreator = new ServiceCreator($config); $service = $serviceCreator->getService(); // Unset service name reference $this->references->removeKey($serviceName); // Store instance if this service has a CONTAINER scope if ($config->getScope() == ServiceScope::CONTAINER) { $this->instantiatedServices->key($serviceName, $service); } return $service; }
/** * Get entity attribute * * @param string $attribute * * @throws EntityException * @return AbstractAttribute */ public function getAttribute($attribute) { if (!$this->attributes->keyExists($attribute)) { throw new EntityException(EntityException::ATTRIBUTE_NOT_FOUND, [$attribute, get_class($this)]); } return $this->attributes[$attribute]; }
/** * Build a UrlObject from array parts. * * @param ArrayObject|array $parts Url parts, possible keys are: 'scheme', 'host', 'port', 'path' and 'query' * * @throws UrlObjectException * @return UrlObject */ static function buildUrl($parts) { $parts = new ArrayObject($parts); ################### ### PARSE PARTS ### ################### // scheme $scheme = $parts->key('scheme', '', true); // host $host = $parts->key('host', '', true); // port $port = $parts->key('port', '', true); // path $path = $parts->key('path', '', true); // parse query string $query = ''; if ($parts->keyExists('query')) { if (self::isString($parts->key('query'))) { parse_str($parts->key('query'), $queryData); } else { $queryData = $parts->key('query'); } if (self::isArray($queryData)) { $query = $queryData; } } ################### ### BUILD URL ### ################### $url = ''; // scheme if ($scheme && $scheme != '') { $url .= $scheme . '://'; } // host if ($host && $host != '') { $url .= $host; } // port if ($port != '') { $url .= ':' . $port; } // path if ($path != '') { $url .= $path; } // query if (self::isArray($query)) { $query = http_build_query($query); if ($query != "") { $url .= '?' . $query; } } try { return new UrlObject($url); } catch (\Exception $e) { throw new UrlObjectException($e->getMessage()); } }
/** * Fire event * * @param string $eventName Event to fire. You can also use wildcards to fire multiple events at once, ex: 'event.*' * @param array|Event $data Array or Event object * @param null $resultType If specified, the event results will be filtered using given class/interface name * @param null|int $limit Number of results to return * * @return array Array of results from EventListeners */ public function fire($eventName, $data = null, $resultType = null, $limit = null) { if ($this->suppressEvents) { return null; } if ($this->str($eventName)->endsWith('*')) { return $this->fireWildcardEvents($eventName, $data, $resultType); } if (!$this->events->keyExists($eventName)) { return null; } $eventListeners = $this->events->key($eventName); if (!$this->isInstanceOf($data, '\\Webiny\\Component\\EventManager\\Event')) { $data = new Event($data); } return $this->eventProcessor->process($eventListeners, $data, $resultType, $limit); }
/** * Merge current config with given config * * @param array|ArrayObject|ConfigObject $config ConfigObject or array of ConfigObject to merge with * * @throws ConfigException * @return $this */ public function mergeWith($config) { if ($this->isArray($config) || $this->isArrayObject($config)) { $configs = StdObjectWrapper::toArray($config); // Make sure it's an array of ConfigObject if (!$this->isInstanceOf($this->arr($configs)->first()->val(), $this)) { $configs = [Config::getInstance()->php($configs)]; } } elseif ($this->isInstanceOf($config, $this)) { $configs = [$config]; } else { throw new ConfigException('Invalid parameter passed to ConfigObject mergeWith($config) method! Expecting a ConfigObject or array.'); } /** @var ConfigObject $value */ foreach ($configs as $config) { // If it's a PHP array or ArrayObject, convert it to ConfigObject if ($this->isArray($config) || $this->isArrayObject($config)) { $config = Config::getInstance()->php($config); } foreach ($config as $key => $value) { if ($this->data->keyExists($key)) { if ($this->isNumber($key)) { $this->data[] = $value; } elseif ($this->isInstanceOf($value, $this) && $this->isInstanceOf($this->data[$key], $this)) { $this->data[$key]->mergeWith($value); } else { if ($this->isInstanceOf($value, $this)) { $this->data[$key] = new static($value->toArray(), false); } else { $this->data[$key] = $value; } } } else { if ($this->isInstanceOf($value, $this)) { $this->data[$key] = new static($value->toArray(), false); } else { $this->data[$key] = $value; } } } } return $this; }
/** * Extend $config with $parentConfig * * @param ArrayObject $config Child config object * @param ArrayObject $parentConfig Parent config object * * @return ArrayObject */ private function extendConfig(ArrayObject $config, ArrayObject $parentConfig) { $configCalls = null; $overrideCalls = false; // Get calls arrays if ($config->keyExists('Calls')) { $configCalls = $config->key('Calls'); } elseif ($config->keyExists('!Calls')) { $configCalls = $config->key('!Calls'); $overrideCalls = true; } $parentCalls = $parentConfig->key('Calls', [], true); // Merge basic values $config = $parentConfig->merge($config); // Remove unnecessary keys $config->removeKey('Parent')->removeKey('Abstract')->removeKey('Calls'); // Merge calls if (!$this->isNull($configCalls) && !$this->isNull($parentCalls)) { if ($overrideCalls) { $config->key('!Calls', $configCalls); return; } foreach ($configCalls as $call) { $call = $this->arr($call); if ($call->keyExists(2)) { $parentCalls[$call[2]] = $call->val(); } else { $parentCalls[] = $call->val(); } } $config->key('Calls', $parentCalls); } elseif ($this->isNull($configCalls) && !$this->isNull($parentCalls)) { $config->key('Calls', $parentCalls); } elseif (!$this->isNull($configCalls) && $this->isNull($parentCalls)) { $config->key('Calls', $configCalls); } return $config; }
/** * @dataProvider arraySet1 */ public function testKey($array) { $a = new ArrayObject($array); $searchResult = $a->keyExists('NonExistingKey'); $this->assertSame(false, $searchResult); }
/** * (PHP 5 >= 5.0.0)<br/> * Whether a offset exists * @link http://php.net/manual/en/arrayaccess.offsetexists.php * * @param mixed $offset <p> * An offset to check for. * </p> * * @return boolean true on success or false on failure. * </p> * <p> * The return value will be casted to boolean if non-boolean was returned. */ public function offsetExists($offset) { return $this->data->keyExists($offset); }
/** * Checks if current option contains the given attribute. * * @param string $name Attribute name. * * @return bool */ public function hasAttribute($name) { return $this->attributes->keyExists($name); }