/** * Remove instance from pool * * @param $instance * * @return bool */ public function remove(EntityAbstract $instance) { $entityPool = $this->pool->key(get_class($instance), $this->arr(), true); $entityPool->removeKey($instance->getId()->getValue()); unset($instance); return true; }
/** * This test runs a simple populate on entity with no validators. * * It tests: * - all attribute types * - all reference attributes: many2one, one2many and many2many * - it tests both on-fly creation and linking with existing entities */ public function testPopulateNoValidationAllNew() { $many2one = new Many2One(); $many2one->char = 'many2oneExisting'; $many2one->save(); $many2many = new Many2Many(); $many2many->char = 'many2many1'; $many2many->save(); $many2many2 = new Many2Many(); $many2many2->char = 'many2many2'; $many2many2->save(); $data = ['boolean' => true, 'char' => 'char', 'skip' => 'this value will not be set', 'integer' => 12, 'calculation' => 5, 'float' => 20.35, 'date' => '2016-03-14', 'datetime' => '2016-03-14T13:45:20+0000', 'arr' => [1, 2, 3], 'object' => ['key1' => 'value', 'key2' => 12], 'geoPoint' => ['lat' => 50, 'lng' => 100, 'stripThisKey' => 'whatever'], 'many2oneNew' => ['char' => 'many2oneNew'], 'many2oneExisting' => $many2one, 'one2many' => [['char' => 'one2many1'], ['char' => 'one2many2']], 'many2many' => [$many2many->id, $many2many2]]; $class = Lib\Classes::ENTITY_NO_VALIDATION; $entity = new $class(); $entity->populate($data)->save(); // Test current $entity state $this->assertEntityStateNoValidation($entity); // Load entity from DB and test state again $id = $entity->id; Entity::getInstance()->remove($entity); $entity = $class::findById($id); $this->assertEntityStateNoValidation($entity); // Test toArray conversion $array = new ArrayObject($entity->toArray('*,float:2,arr,object[key1],dynamicWithParams:4,many2oneNew[char,relations.integer],one2many,many2many', 2)); $this->assertEquals('char', $array->keyNested('char')); $this->assertEquals(4, $array->keyNested('float')); $this->assertArrayNotHasKey('boolean', $array->val()); $this->assertArrayNotHasKey('skip', $array->val()); $this->assertEquals([1, 2, 3], $array->keyNested('arr')); $this->assertEquals('value', $array->keyNested('object.key1')); $this->assertNull($array->keyNested('object.key2')); // GeoPoint should return an array of lat/lng values $this->assertEquals(50, $array->keyNested('geoPoint.lat')); $this->assertEquals(100, $array->keyNested('geoPoint.lng')); // If return value of dynamic attribute is AbstractEntity or EntityCollection, // EntityDataExtractor should call toArray() an those objects $this->assertEquals(24, $array->key('dynamicWithDefaultParams')); $this->assertEquals(48, $array->key('dynamicWithParams')); $this->assertInternalType('array', $array->key('dynamicEntity')); $this->assertInternalType('array', $array->key('dynamicEntityCollection')); $this->assertCount(2, $array->key('dynamicEntityCollection')); $this->assertEquals('many2oneExisting', $array->keyNested('dynamicEntityCollection.0.char')); $this->assertEquals('many2oneNew', $array->keyNested('dynamicEntityCollection.1.char')); // GeoPoint attribute should strips all values not related to mongo Point $this->assertArrayNotHasKey('stripThisKey', $array->keyNested('geoPoint')); $this->assertEquals('many2oneNew', $array->keyNested('many2oneNew.char')); $this->assertEquals(12, $array->keyNested('many2oneNew.relations.0.integer')); $this->assertEquals('one2many1', $array->keyNested('one2many.0.char')); $this->assertEquals('one2many2', $array->keyNested('one2many.1.char')); $this->assertEquals('many2many1', $array->keyNested('many2many.0.char')); $this->assertEquals('many2many2', $array->keyNested('many2many.1.char')); }
/** * Constructor * * @param array|ArrayObject $config * * @throws StorageException */ public function __construct($config) { if (is_array($config)) { $config = new ArrayObject($config); } if (!$config instanceof ArrayObject) { throw new StorageException('Storage driver config must be an array or ArrayObject!'); } $this->helper = LocalHelper::getInstance(); $this->directory = $this->helper->normalizeDirectoryPath($config->key('Directory', '', true)); $this->publicUrl = $config->key('PublicUrl', '', true); $this->dateFolderStructure = $config->key('DateFolderStructure', false, true); $this->create = $config->key('Create', false, true); }
/** * Checks if $format is a valid format for $dateElement. * * @param string $dateElement Possible values are: date, year, month, day, time, hour, minutes, seconds, meridiem. * @param string $format For list of possible formats check: http://php.net/manual/en/function.date.php * * @return mixed * @throws DateTimeObjectException */ private function validateFormatFor($dateElement, $format) { if (!self::$formatters->key($dateElement)->inArray($format)) { throw new DateTimeObjectException(DateTimeObjectException::MSG_INVALID_FORMAT_FOR_ELEMENT, [$format, "get" . ucfirst($dateElement)]); } return $format; }
/** * Remove instance from pool * * @param $instance * * @return bool */ public function remove(AbstractEntity $instance) { $entityPool = $this->pool->key(get_class($instance), $this->arr(), true); $entityPool->removeKey($instance->id); unset($instance); return true; }
/** * Instantiate service using given service name * * @param string $serviceName * * @return object * @throws ServiceManagerException */ private function instantiateService($serviceName) { // Make sure service is registered if (!$this->registeredServices->keyExists($serviceName)) { throw new ServiceManagerException(ServiceManagerException::SERVICE_DEFINITION_NOT_FOUND, [$serviceName]); } // Get service config from registered services array $config = $this->registeredServices->key($serviceName); // Check circular referencing if ($this->references->keyExists($serviceName)) { throw new ServiceManagerException(ServiceManagerException::SERVICE_CIRCULAR_REFERENCE, [$serviceName]); } // Set service name reference for circular referencing checks $this->references->key($serviceName, $serviceName); // Compile ConfigObject into ServiceConfig $configCompiler = new ConfigCompiler($serviceName, $config, $this->parameters); $this->compiledConfig->key($serviceName, $configCompiler->compile()); /** * @var $config ServiceConfig */ $config = $this->compiledConfig->key($serviceName); // Construct service container and get service instance $serviceCreator = new ServiceCreator($config); $service = $serviceCreator->getService(); // Unset service name reference $this->references->removeKey($serviceName); // Store instance if this service has a CONTAINER scope if ($config->getScope() == ServiceScope::CONTAINER) { $this->instantiatedServices->key($serviceName, $service); } return $service; }
/** * Sets or adds an entry to cache control headers. * * @param string $key Cache control header name. * @param string $value Cache control header value. * * @throws ResponseException * @return $this */ public function setCacheControlEntry($key, $value) { if (!$this->validateCacheControlHeader($key)) { throw new ResponseException('Invalid cache control header "' . $key . '".'); } $this->cacheControl->key($key, $value); return $this; }
/** * Build a UrlObject from array parts. * * @param ArrayObject|array $parts Url parts, possible keys are: 'scheme', 'host', 'port', 'path' and 'query' * * @throws UrlObjectException * @return UrlObject */ static function buildUrl($parts) { $parts = new ArrayObject($parts); ################### ### PARSE PARTS ### ################### // scheme $scheme = $parts->key('scheme', '', true); // host $host = $parts->key('host', '', true); // port $port = $parts->key('port', '', true); // path $path = $parts->key('path', '', true); // parse query string $query = ''; if ($parts->keyExists('query')) { if (self::isString($parts->key('query'))) { parse_str($parts->key('query'), $queryData); } else { $queryData = $parts->key('query'); } if (self::isArray($queryData)) { $query = $queryData; } } ################### ### BUILD URL ### ################### $url = ''; // scheme if ($scheme && $scheme != '') { $url .= $scheme . '://'; } // host if ($host && $host != '') { $url .= $host; } // port if ($port != '') { $url .= ':' . $port; } // path if ($path != '') { $url .= $path; } // query if (self::isArray($query)) { $query = http_build_query($query); if ($query != "") { $url .= '?' . $query; } } try { return new UrlObject($url); } catch (\Exception $e) { throw new UrlObjectException($e->getMessage()); } }
/** * 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); } } }
/** * Private function that parses the hierarchy array and builds up a hierarchy map * * @param array $hierarchy Role hierarchy array from system configuration. */ private function buildRoleMap($hierarchy) { $this->map = $this->arr(); foreach ($hierarchy as $main => $roles) { $hierarchy[$main] = $this->arr((array) $roles); } $hierarchy = $this->arr($hierarchy); foreach ($hierarchy as $main => $roles) { $this->map->append($main, $roles->val()); $additionalRoles = clone $roles; $parsed = $this->arr(); $role = ''; while ($additionalRoles->count() > 0 && $additionalRoles->removeFirst($role)) { if (!$hierarchy->keyExists($role)) { continue; } $parsed->append($role); $innerRole = $this->arr($this->map->key($main)); $innerRole->merge($hierarchy[$role]->val()); $this->map->append($main, $innerRole->val()); $additionalRoles->merge($hierarchy->key($role)->diff($parsed->val())->val()); } } }
/** * Constructor * * @param array|ArrayObject $config * * @throws StorageException */ public function __construct($config) { if (is_array($config)) { $config = new ArrayObject($config); } if (!$config instanceof ArrayObject) { throw new StorageException('Storage driver config must be an array or ArrayObject!'); } $bridge = Storage::getConfig()->get('Bridges.AmazonS3', '\\Webiny\\Component\\Amazon\\S3'); $accessKeyId = $config->key('AccessKeyId'); $secretAccessKey = $config->key('SecretAccessKey'); $region = $config->key('Region'); $endpoint = $config->key('Endpoint'); $this->s3Client = new $bridge($accessKeyId, $secretAccessKey, $region, $endpoint); $this->bucket = $config->key('Bucket'); $this->cdnDomain = $config->key('CdnDomain'); $this->meta = $config->key('Meta'); }
public function testStdClassConversion() { $obj = new \stdClass(); $obj->name = 'Webiny'; $obj->language = 'PHP'; $arr = new ArrayObject($obj); $this->assertEquals('Webiny', $arr->key('name')); $this->assertEquals('PHP', $arr->key('language')); $obj2 = new \stdClass(); $obj2->url = 'webiny.com'; $obj->nested = [$obj2]; $arr = new ArrayObject($obj); $this->assertEquals('webiny.com', $arr->keyNested('nested.0.url')); }
/** * Return the attribute value under the defined $key. * * @param string $key Attribute key. * @param mixed $defaultValue Default value that the method should return if $key is not found among the attributes. * * @return mixed */ public function getAttribute($key, $defaultValue = false) { return $this->attributes->key($key, $defaultValue, true); }
/** * Get a session value for the given $key. * If key doesn't not exist, $value will be returned and assigned under that key. * * @param string $key Key for which you wish to get the value. * @param mixed $value Default value that will be returned if $key doesn't exist. * * @return string Value of the given $key. */ public function get($key, $value = null) { $return = $this->sessionBag->key($key, $value, true); $_SESSION[$key] = $return; return $return; }
/** * Sets or adds a header. * * @param string $key Header name. * @param string $value Header value. * * @return $this */ public function setHeader($key, $value) { $this->headers->key($key, $value); return $this; }
/** * Returns the attribute value. * * @param string $name Name of the attribute for which you wish to get the value. * @param null|mixed $default If attribute is not found, what to return. Default is null. * * @return string */ public function getAttribute($name, $default = null) { return $this->attributes->key($name, $default, true); }
/** * Returns the route under the given name. * * @param string $name Name of the route. * * @return Route */ public function get($name) { return $this->routes->key($name, null, true); }
/** * (PHP 5 >= 5.0.0)<br/> * Offset to set * @link http://php.net/manual/en/arrayaccess.offsetset.php * * @param mixed $offset <p> * The offset to assign the value to. * </p> * @param mixed $value <p> * The value to set. * </p> * * @return void */ public function offsetSet($offset, $value) { $this->data->key($offset, $value); }
public function testSetNestedKeys() { $array = ['k1' => 'test', 'k2' => ['k3' => ['k4' => 'deepest']]]; $a = new ArrayObject($array); $a->key('k2.k3.k4', 'webiny'); $a->key('k2.k3.k5', 'anotherElement'); $this->assertEquals('webiny', $a->key('k2.k3.k4')); $this->assertEquals('anotherElement', $a->key('k2.k3.k5')); }
/** * Extend $config with $parentConfig * * @param ArrayObject $config Child config object * @param ArrayObject $parentConfig Parent config object * * @return ArrayObject */ private function extendConfig(ArrayObject $config, ArrayObject $parentConfig) { $configCalls = null; $overrideCalls = false; // Get calls arrays if ($config->keyExists('Calls')) { $configCalls = $config->key('Calls'); } elseif ($config->keyExists('!Calls')) { $configCalls = $config->key('!Calls'); $overrideCalls = true; } $parentCalls = $parentConfig->key('Calls', [], true); // Merge basic values $config = $parentConfig->merge($config); // Remove unnecessary keys $config->removeKey('Parent')->removeKey('Abstract')->removeKey('Calls'); // Merge calls if (!$this->isNull($configCalls) && !$this->isNull($parentCalls)) { if ($overrideCalls) { $config->key('!Calls', $configCalls); return; } foreach ($configCalls as $call) { $call = $this->arr($call); if ($call->keyExists(2)) { $parentCalls[$call[2]] = $call->val(); } else { $parentCalls[] = $call->val(); } } $config->key('Calls', $parentCalls); } elseif ($this->isNull($configCalls) && !$this->isNull($parentCalls)) { $config->key('Calls', $parentCalls); } elseif (!$this->isNull($configCalls) && $this->isNull($parentCalls)) { $config->key('Calls', $configCalls); } return $config; }
/** * Get array of event listeners * * @param $eventName * * @return array */ public function getEventListeners($eventName) { return $this->events->key($eventName, [], true); }