/** * Get or update the given key inside current array. This method supports nested key access: 'level1.level2.level3' * * @param string|int|StringObject $key Array key Eg: 'level1.level2.level3' * @param null|mixed $value If set, the value under current $key will be updated and not returned. * @param bool $setOnlyIfDoesntExist Set the $value only in case if the $key doesn't exist. * * @return $this|mixed|StringObject The value of the given key. */ public function keyNested($key, $value = null, $setOnlyIfDoesntExist = false) { $key = StdObjectWrapper::toString($key); // If key does not exist, set default $value and return it if ($setOnlyIfDoesntExist && !$this->keyExistsNested($key)) { $this->handleNestedValue($key, $value, true); return $value; } elseif (!$value && !$setOnlyIfDoesntExist && !$this->keyExistsNested($key)) { // This means we are trying to get a value of nested key that does not exist return null; } else { // Set new $value into given $key if (!$setOnlyIfDoesntExist && !$this->isNull($value)) { $this->handleNestedValue($key, $value, false); return $this; } } $array = $this->val(); // Get value for given $key if (strpos($key, '.') !== false) { $keys = explode('.', trim($key, '.'), 2); if (!isset($array[$keys[0]])) { $array[$keys[0]] = []; } $targetArray = new ArrayObject($array[$keys[0]]); return $targetArray->keyNested($keys[1], $value, true); } return isset($array[$key]) ? $array[$key] : $value; }
/** * Set method to be called on handler.<br /> * If not set, default method will be called: * <code>handle(Event $event)</code> * * @param string $method Method to call on handler * * @throws EventManagerException * @return $this */ public function method($method) { if (!$this->isString($method) && !$this->isStringObject($method)) { throw new EventManagerException(EventManagerException::MSG_INVALID_ARG, ['$method', 'string|StringObject']); } $this->method = StdObjectWrapper::toString($method); return $this; }
/** * Checks if md5 string is valid * * @param String $md5 * * @return Boolean */ private static function isMd5($md5) { if (!self::isString($md5) && !self::isStringObject($md5)) { return false; } $md5 = StdObjectWrapper::toString($md5); return !empty($md5) && preg_match('/^[a-f0-9]{32}$/', $md5); }
/** * Checks if $key exists in current array as index. If it exists, true is returned. * If the $key doesn't exist, $default is returned. * This method supports nested keys access: 'level1.level2.level3' * * @param string|StringObject $key Array key. Eg: 'level1.level2.level3' * @param mixed $default If key is not found, $default is returned. * * @return bool|mixed True is returned if the key exists, otherwise $default is returned. */ public function keyExistsNested($key, $default = false) { $key = StdObjectWrapper::toString($key); if (strpos($key, '.') !== false) { $keys = explode('.', trim($key, '.'), 2); if (!isset($this->val()[$keys[0]])) { return $default; } $sourceArray = new ArrayObject($this->val()[$keys[0]]); return $sourceArray->keyExistsNested($keys[1], $default); } if (array_key_exists($key, $this->val())) { return true; } return $default; }
/** * Set url port. * * @param StringObject|string $port Url port. * * @return $this */ public function setPort($port) { $this->port = StdObjectWrapper::toString($port); $this->rebuildUrl(); return $this; }
/** * Get formatted config data as string * * @throws ConfigException * @return string Formatted config data */ public final function getString() { $res = $this->getStringInternal(); if (!$this->isString($res) && !$this->isStringObject($res)) { throw new ConfigException('AbstractDriver method _getString() must return string or StringObject.'); } return StdObjectWrapper::toString($res); }
/** * Set related entity class for this attribute<br> * You can either use absolute namespace path or <b>App.Component.Entity</b> notation:<br><br> * * <b>'Cms.Content.PageEntity'</b> will be translated to: <b>'\WebinyPlatform\Apps\Cms\Components\Content\Entities\PageEntity'</b> * * @param string $entityClass * * @return $this */ public function setEntity($entityClass) { $entityClass = $this->str($entityClass); if ($entityClass->contains('.')) { $parts = $entityClass->explode('.'); $entityClass = '\\WebinyPlatform\\Apps\\' . $parts[0] . '\\Components\\' . $parts[1] . '\\Entities\\' . $parts[2]; } $this->entityClass = StdObjectWrapper::toString($entityClass); return $this; }
/** * Get current Yaml value as string * * @param int $indent * @param bool $wordWrap * * @throws YamlException * @return string */ public function getString($indent = 2, $wordWrap = false) { $res = $this->driverInstance->getString($indent, $wordWrap); if (!$this->isString($res) && !$this->isStringObject($res)) { throw new YamlException('YamlInterface method _getString() must return a string or StringObject.'); } return StdObjectWrapper::toString($res); }
/** * Checks if the current string ends with the given $string. * * @param string|StringObject $string String to check. * * @return bool If current string ends with $string, true is returned, otherwise false. */ public function endsWith($string) { $string = StdObjectWrapper::toString($string); // calculate the end position $length = strlen($string); $endString = substr($this->val(), -$length); if ($string == $endString) { return true; } return false; }