/** * Dynamically generates the object from the declared properties of the given object or array * * @param array|object $object * * @return \stdClass */ public static function toObject($object) { // If we can't iterate over the thing, we bail if (!is_object($object) && !is_array($object) && !$object instanceof \Traversable) { return null; } if (is_array($object)) { // Convert to an object $_properties = new \stdClass(); foreach ($object as $_key => $_value) { $_properties->{$_key} = $_value; } } else { $_me = new \ReflectionObject($object); $_properties = $_me->getProperties(); } // We'll return this $_obj = new \stdClass(); if (!empty($_properties)) { if (is_object($object)) { $_myClass = get_class($object); } else { $_myClass = '_array_'; } foreach ($_properties as $_property) { // Only want properties of $object hierarchy... if (isset($_property->class)) { $_class = new \ReflectionClass($_property->class); if (!empty($_class) && !$_class->isInstance($object) && !$_class->isSubclassOf($_myClass)) { unset($_class); continue; } unset($_class); } try { $_realPropertyName = $_propertyName = ltrim($_property->name, '_ '); if (false !== strpos($_propertyName, '_')) { $_propertyName = Inflector::tag($_propertyName); } $_getter = 'get' . $_propertyName; if (method_exists($object, $_getter)) { $_propertyValue = $object->{$_getter}(); if (!is_scalar($_propertyValue)) { $_propertyValue = self::toObject($_propertyValue); } $_obj->{$_realPropertyName} = $_propertyValue; } } catch (\Exception $_ex) { // Just ignore, not a valid property if we can't read it with a getter } } } return $_obj; }
/** * Merges settings to pre-constructed provider config * * @param array|ProviderConfigLike $settings * * @return $this */ public function mergeSettings($settings = array()) { foreach ($settings as $_key => $_value) { if (property_exists($this, $_key)) { try { Option::set($this, $_key, $_value); unset($settings, $_key); continue; } catch (\Exception $_ex) { // Ignore... } } $_setter = Inflector::tag('set_' . $_key); if (method_exists($this, $_setter)) { call_user_func(array($this, $_setter), $_value); unset($settings, $_key, $_setter); } } return $this; }
/** * Gets a guaranteed cache key * * @param string $class * * @return string */ protected static function _cacheKey($class = null) { static $_key = null; return $_key ?: Inflector::tag($class ?: \get_called_class(), true); }
/** * Load up with presents */ protected function _loadRequest() { $_goodies = array(); // Fill up the bag if (isset($_SERVER) && !empty($_SERVER)) { foreach ($_SERVER as $_key => $_value) { if (false !== stripos($_key, 'HTTP_', 0)) { if (!isset($_goodies['server.headers']) || !is_array($_goodies['server.headers'])) { $_goodies['server.headers'] = array(); } $_tag = Inflector::tag($_key, true, 'HTTP_'); $_goodies['server.headers'][$_tag] = $_value; } else { $_tag = 'server.' . Inflector::tag($_key, true); $_goodies[$_tag] = $_value; } } } if (isset($_REQUEST) && !empty($_REQUEST)) { foreach ($_REQUEST as $_key => $_value) { $_tag = 'request.' . Inflector::tag($_key, true, 'REQUEST_'); $_goodies[$_tag] = $_value; } } $this->merge($_goodies); return true; }