GraphQL\Utils::assign PHP Method

assign() public static method

public static assign ( object $obj, array $vars, array $requiredKeys = [] ) : array
$obj object
$vars array
$requiredKeys array
return array
    public static function assign($obj, array $vars, array $requiredKeys = [])
    {
        foreach ($requiredKeys as $key) {
            if (!isset($key, $vars)) {
                throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
            }
        }
        foreach ($vars as $key => $value) {
            if (!property_exists($obj, $key)) {
                $cls = get_class($obj);
                trigger_error("Trying to set non-existing property '{$key}' on class '{$cls}'");
            }
            $obj->{$key} = $value;
        }
        return $obj;
    }

Usage Example

Example #1
0
 public function __construct($config)
 {
     Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'values' => Config::arrayOf(['name' => Config::STRING | Config::REQUIRED, 'value' => Config::ANY, 'deprecationReason' => Config::STRING, 'description' => Config::STRING], Config::KEY_AS_NAME), 'description' => Config::STRING]);
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     $this->_values = [];
     if (!empty($config['values'])) {
         foreach ($config['values'] as $name => $value) {
             $this->_values[] = Utils::assign(new EnumValueDefinition(), $value + ['name' => $name]);
         }
     }
 }
All Usage Examples Of GraphQL\Utils::assign