This class is used when we need to return a standard object, but none of the current available standard objects fit the role.
Inheritance: extends AbstractStdObject, use trait Webiny\Component\StdLib\ValidatorTrait
Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * 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);
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * Parse given Yaml resource and build array
  * This method must support file paths (string or StringObject) and FileObject
  *
  * @throws SymfonyYamlException
  * @return string
  */
 private function parseResource()
 {
     if ($this->isArray($this->resource)) {
         return StdObjectWrapper::toArray($this->resource);
     } elseif ($this->isString($this->resource)) {
         return Yaml::parse($this->resource);
     } elseif ($this->isInstanceOf($this->resource, 'Webiny\\Component\\Config\\ConfigObject')) {
         return $this->resource->toArray();
     }
     throw new SymfonyYamlException(SymfonyYamlException::UNABLE_TO_PARSE, [gettype($this->resource)]);
 }
Example #5
0
 /**
  * Base constructor.
  *
  * @param string|array|ArrayObject $content     Json content.
  * @param array                    $headers     Headers to attach to the response.
  * @param bool                     $prettyPrint Should we use JSON_PRETTY_PRINT to nicely format the output.
  */
 public function __construct($content, $headers = [], $prettyPrint = false)
 {
     if (StdObjectWrapper::isArrayObject($content)) {
         $content = $this->jsonEncode($content->val(), $prettyPrint ? JSON_PRETTY_PRINT : 0);
     } else {
         if ($this->isArray($content) || $this->isObject($content)) {
             $content = $this->jsonEncode($content, $prettyPrint ? JSON_PRETTY_PRINT : 0);
         }
     }
     parent::__construct($content, 200, $headers);
     $this->setContentType('application/json');
 }
Example #6
0
 public function __construct($entityClass, $entityCollection, $conditions, $order, $limit, $offset)
 {
     // Convert boolean strings to boolean
     foreach ($conditions as &$condition) {
         if (is_scalar($condition) && (strtolower($condition) === 'true' || strtolower($condition) === 'false')) {
             $condition = StdObjectWrapper::toBool($condition);
         }
     }
     $this->entityClass = $entityClass;
     $this->collectionName = $entityCollection;
     $this->conditions = $conditions;
     $this->limit = $limit;
     $this->offset = $offset;
     $this->data = Entity::getInstance()->getDatabase()->find($entityCollection, $conditions, $order, $limit, $offset);
 }
Example #7
0
 protected function validate(&$value)
 {
     if ($this->isNull($value)) {
         return $this;
     }
     if (!$this->isArray($value) && !$this->isArrayObject($value)) {
         $this->expected('array or ArrayObject', gettype($value));
     }
     $value = StdObjectWrapper::toArray($value);
     if (!array_key_exists('lat', $value) || !array_key_exists('lng', $value)) {
         $ex = new ValidationException(ValidationException::VALIDATION_FAILED);
         $ex->addError($this->attribute, 'GeoPointAttribute value must contain `lat` and `lng`');
         throw $ex;
     }
 }
Example #8
0
 /**
  * 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;
 }
Example #9
0
 public function setInvalidAttributes($attributes)
 {
     $this->invalidAttributes = StdObjectWrapper::toArray($attributes);
     return $this;
 }
Example #10
0
 /**
  * Build internal object data using given $config
  *
  * @param array|ArrayObject $config
  */
 private function buildInternalData($config)
 {
     $this->data = $this->arr();
     $array = StdObjectWrapper::toArray($config);
     foreach ($array as $key => $value) {
         if ($this->isArray($value)) {
             $this->data->key($key, new static($value, false));
         } else {
             $this->data->key($key, $value, true);
         }
     }
 }
Example #11
0
 /**
  * Set multiple headers
  *
  * @param array|ArrayObject $headers
  *
  * @return $this
  */
 public function setHeaders($headers)
 {
     $headers = StdObjectWrapper::toArray($headers);
     $this->message['headers'] = $headers;
     return $this;
 }
Example #12
0
 /**
  * Get config data as array
  *
  * @throws ConfigException
  * @return array Parsed resource data array
  */
 public final function getArray()
 {
     $res = $this->getArrayInternal();
     if (!$this->isArray($res) && !$this->isArrayObject($res)) {
         $errorMessage = 'AbstractDriver method _getArray() must return array or ArrayObject.';
         $errorMessage .= ' Make sure you have provided a valid config file path with file extension.';
         throw new ConfigException($errorMessage);
     }
     return StdObjectWrapper::toArray($res);
 }
Example #13
0
 /**
  * Set url query param.
  *
  * @param StringObject|ArrayObject|string|array $query  Query params.
  * @param bool                                  $append Do you want to append or overwrite current query param.
  *                                                      In case when you are appending values, the values from $query,
  *                                                      that already exist in the current query, will be overwritten
  *                                                      by the ones inside the $query.
  *
  * @throws UrlObjectException
  * @return $this
  */
 public function setQuery($query, $append = false)
 {
     if ($this->isStdObject($query)) {
         $query = $query->val();
     }
     if ($append && $this->query != '') {
         if ($this->isString($this->query)) {
             $currentQuery = new StringObject($this->query);
             $currentQuery = $currentQuery->parseString();
         } else {
             $currentQuery = new ArrayObject($this->query);
         }
         if ($this->isStdObject($query)) {
             if (StdObjectWrapper::isArrayObject($append)) {
                 $query = $query->val();
             } else {
                 if (StdObjectWrapper::isStringObject($query)) {
                     $query = $query->parseString()->val();
                 } else {
                     throw new UrlObjectException(UrlObjectException::MSG_INVALID_ARG, ['$query', 'StringObject|ArrayObject|string|array']);
                 }
             }
         } else {
             if ($this->isString($query)) {
                 $query = new StringObject($query);
                 $query = $query->parseString()->val();
             }
         }
         $currentQuery->merge($query);
         $query = $currentQuery->val();
     }
     $this->query = $query;
     $this->rebuildUrl();
     return $this;
 }
 /**
  * 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;
 }
Example #15
0
 /**
  * Check if $instance is a UrlObject.
  *
  * @param mixed $instance
  *
  * @return bool
  */
 protected static function isUrlObject($instance)
 {
     return StdObjectWrapper::isUrlObject($instance);
 }
Example #16
0
 public function toArray($params = [])
 {
     return $this->processToArrayValue(StdObjectWrapper::toBool(parent::toArray($params)));
 }
Example #17
0
 private function normalizeData($data)
 {
     if ($this->isArray($data) || $this->isArrayObject($data)) {
         return StdObjectWrapper::toArray($data);
     }
     return $data;
 }
Example #18
0
 /**
  * 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;
 }
Example #19
0
 /**
  * Execute callback from MatchedRoute and return result.<br>
  * Required callback structure is:
  * <code>
  * Callback:
  *     Class: \Your\Class
  *     Method: handle
  *     Static: true // (Optional, "false" by default)
  *
  *</code>
  * @param MatchedRoute $route
  *
  * @return mixed
  *
  * @throws RouterException
  */
 public function execute(MatchedRoute $route)
 {
     $callback = $route->getCallback();
     if ($this->isString($callback)) {
         throw new RouterException(RouterException::STRING_CALLBACK_NOT_PARSABLE);
     }
     $callback = $this->arr($callback);
     $handlerClass = $callback->key('Class', false, true);
     $handlerMethod = $callback->key('Method', false, true);
     $staticMethod = StdObjectWrapper::toBool($callback->key('Static', false, true));
     if (!class_exists($handlerClass)) {
         throw new RouterException(RouterException::CALLBACK_CLASS_NOT_FOUND, [$handlerClass]);
     }
     if (!method_exists($handlerClass, $handlerMethod)) {
         throw new RouterException(RouterException::CALLBACK_CLASS_METHOD_NOT_FOUND, [$handlerMethod, $handlerClass]);
     }
     if ($staticMethod) {
         return forward_static_call_array([$handlerClass, $handlerMethod], $route->getParams());
     }
     return call_user_func_array([new $handlerClass(), $handlerMethod], $route->getParams());
 }
Example #20
0
 public function getToArrayValue()
 {
     return StdObjectWrapper::toBool(parent::getToArrayValue());
 }
Example #21
0
 /**
  * Get Yaml value as array
  *
  * @throws YamlException
  * @return array
  */
 public function getArray()
 {
     $res = $this->driverInstance->getArray();
     if (!$this->isArray($res) && !$this->isArrayObject($res)) {
         throw new YamlException('YamlInterface method getArray() must return an array or ArrayObject.');
     }
     return StdObjectWrapper::toArray($res);
 }
Example #22
0
 /**
  * Get the first element in the array.
  * If the element is array, ArrayObject is returned, else StringObject is returned.
  *
  * @return StringObject|ArrayObject|StdObjectWrapper The first element in the array.
  */
 public function first()
 {
     $arr = $this->val();
     $first = reset($arr);
     return StdObjectWrapper::returnStdObject($first);
 }