Webiny\Component\StdLib\StdObject\StdObjectWrapper::toString PHP Method

toString() public static method

This function checks if $var is a string, StringObject or something else. In the end a string is returned.
public static toString ( mixed $var ) : string
$var mixed
return string
    public static function toString($var)
    {
        if (self::isString($var)) {
            return $var;
        } else {
            if (self::isObject($var)) {
                if (self::isStringObject($var) || self::isFileObject($var)) {
                    return $var->val();
                }
            }
        }
        return (string) $var;
    }

Usage Example

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;
 }
All Usage Examples Of Webiny\Component\StdLib\StdObject\StdObjectWrapper::toString