toArray() public static method

This function checks if $var is an array, ArrayObject or something else. This function tries to cast the element to array and return it.
public static toArray ( mixed $var ) : array
$var mixed
return array
Beispiel #1
0
 /**
  * Checks if a string contains any of the given $char.
  * If any of given $char is present, true is returned.
  *
  * @param array|ArrayObject $needle Array of characters you wish to check
  *
  * @return bool True if current string contains the $needle. Otherwise false is returned.
  */
 public function containsAny($needle)
 {
     $needle = StdObjectWrapper::toArray($needle);
     foreach ($needle as $char) {
         if ($this->contains($char)) {
             return true;
         }
     }
     return false;
 }
Beispiel #2
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)]);
 }
Beispiel #3
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;
     }
 }
Beispiel #4
0
 public function setInvalidAttributes($attributes)
 {
     $this->invalidAttributes = StdObjectWrapper::toArray($attributes);
     return $this;
 }
Beispiel #5
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);
         }
     }
 }
Beispiel #6
0
 /**
  * Set multiple headers
  *
  * @param array|ArrayObject $headers
  *
  * @return $this
  */
 public function setHeaders($headers)
 {
     $headers = StdObjectWrapper::toArray($headers);
     $this->message['headers'] = $headers;
     return $this;
 }
Beispiel #7
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);
 }
Beispiel #8
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);
 }
Beispiel #9
0
 private function normalizeData($data)
 {
     if ($this->isArray($data) || $this->isArrayObject($data)) {
         return StdObjectWrapper::toArray($data);
     }
     return $data;
 }