/**
  * @param array $filters
  *
  * @return $this
  */
 public function setFilters($filters)
 {
     Collection::cast($filters)->each(function ($filter) {
         $this->addFilter($filter);
     });
     return $this;
 }
Example #2
0
 /**
  * @param $options
  *
  * @return $this
  * @throws \ObjectivePHP\Primitives\Exception
  */
 public function addOptions($options)
 {
     Collection::cast($options)->each(function ($value, $key) {
         $this->addOption($key, $value);
     });
     return $this;
 }
 public function testCallbacksAreSetUsingAnArrayAsConstructorParam()
 {
     $aggregate = new CallbacksAggregate('aggreagate', [$lambda = function () {
     }, $otherLambda = function () {
     }]);
     $this->assertEquals(Collection::cast([$lambda, $otherLambda]), $aggregate->getCallbacks());
 }
 /**
  * @param ClassServiceSpecs $serviceSpecs
  * @param array $params
  * @return mixed
  * @throws Exception
  */
 public function build(ServiceSpecsInterface $serviceSpecs, $params = [])
 {
     // check compatibility with the service definition
     if (!$this->doesHandle($serviceSpecs)) {
         throw new Exception(sprintf('"%s" service definition is not handled by this builder.', get_class($serviceSpecs)), Exception::INCOMPATIBLE_SERVICE_DEFINITION);
     }
     $serviceClassName = $serviceSpecs->getClass();
     // check class existence
     if (!class_exists($serviceClassName)) {
         throw new Exception(sprintf('Unable to build service: class "%s" is unknown', $serviceClassName), Exception::INVALID_SERVICE_SPECS);
     }
     // merge service defined and runtime params
     $constructorParams = clone Collection::cast($params);
     $constructorParams->add($serviceSpecs->getParams());
     // substitute params with referenced services
     $this->substituteReferences($constructorParams);
     $service = new $serviceClassName(...$constructorParams->values());
     // call setters if any
     if ($setters = $serviceSpecs->getSetters()) {
         foreach ($setters as $setter => $setterParams) {
             $instanceSetterParams = clone Collection::cast($setterParams);
             $this->substituteReferences($instanceSetterParams);
             $service->{$setter}(...$instanceSetterParams->values());
         }
     }
     return $service;
 }
 /**
  * @param ApplicationInterface $app
  *
  * @throws \Doctrine\ORM\ORMException
  * @throws \ObjectivePHP\Primitives\Exception
  * @throws \ObjectivePHP\ServicesFactory\Exception
  */
 public function buildEntityManagers(ApplicationInterface $app)
 {
     $entityManagers = $app->getConfig()->subset(Config\EntityManager::class);
     foreach ($entityManagers as $connection => $params) {
         if (isset($params['db'])) {
             $params = $params['db'];
         }
         // normalize if needed
         $entitiesPaths = $params['entities.locations'];
         Collection::cast($entitiesPaths)->each(function (&$path) {
             if (strpos($path, '/') !== 0) {
                 $path = getcwd() . '/' . $path;
             }
         });
         // TODO: handle isDev depending on app config
         $emConfig = Setup::createAnnotationMetadataConfiguration((array) $entitiesPaths, true);
         $emConfig->setNamingStrategy(new UnderscoreNamingStrategy());
         $em = EntityManager::create($params, $emConfig);
         if (!empty($params['mapping_types']) && is_array($params['mapping_types'])) {
             $platform = $em->getConnection()->getDatabasePlatform();
             foreach ($params['mapping_types'] as $type => $mapping) {
                 if (!Type::hasType($type) && class_exists($mapping)) {
                     Type::addType($type, $mapping);
                     $mapping = $type;
                 }
                 $platform->registerDoctrineTypeMapping($type, $mapping);
             }
         }
         // register entity manager as a service
         $emServiceId = 'doctrine.em.' . Str::cast($connection)->lower();
         $app->getServicesFactory()->registerService(['id' => $emServiceId, 'instance' => $em]);
         $app->getServicesFactory()->registerService(['id' => 'db.connection.' . $connection, 'instance' => $em->getConnection()->getWrappedConnection()]);
     }
 }
Example #6
0
 /**
  * MatchedRoute constructor.
  * @param RouterInterface $router
  * @param string $name
  * @param $action
  * @param array $params
  */
 public function __construct(RouterInterface $router, string $name, $action, $params = [])
 {
     $this->router = $router;
     $this->name = $name;
     $this->action = $action;
     $this->params = Collection::cast($params);
 }
 /**
  * @param $value
  */
 public function __invoke(&$value)
 {
     $className = $this->className;
     if (!$value instanceof $className) {
         $value = new $className(...Collection::cast($value)->values()->getInternalValue());
     }
 }
 public function testNativeMerging()
 {
     $merger = new ValueMerger(MergePolicy::NATIVE);
     $firstCollection = new Collection(['x' => 'a', 'y' => 'b']);
     $secondCollection = new Collection(['x' => 'a was replaced', 'z' => 'c']);
     $mergedValue = $merger->merge($firstCollection, $secondCollection);
     $this->assertEquals(Collection::cast(['x' => 'a was replaced', 'y' => 'b', 'z' => 'c']), $mergedValue);
 }
 public static function factory($rawDefinition)
 {
     $rawDefinition = Collection::cast($rawDefinition);
     if (!$rawDefinition->has('instance')) {
         throw new Exception('Missing \'instance\' parameter', Exception::INCOMPLETE_SERVICE_SPECS);
     }
     $serviceDefinition = new PrefabServiceSpecs($rawDefinition['id'], $rawDefinition['instance']);
     return $serviceDefinition;
 }
 /**
  * BeanstalkServer constructor.
  *
  * @param $identifier
  * @param $value array Server configuration
  *
  * @throws Exception
  */
 public function __construct($identifier, $value)
 {
     // check value
     $value = Collection::cast($value)->toArray() + ['port' => PheanstalkInterface::DEFAULT_PORT, 'timeout' => null, 'persistent' => false];
     $mandatory = ['host', 'tube'];
     $missing = array_diff($mandatory, array_keys($value));
     if ($missing) {
         throw new Exception(sprintf('Missing %s keys in beanstalk configuration', implode(', ', $missing)));
     }
     parent::__construct($identifier, $value);
 }
 /**
  * @param ApplicationInterface $app
  * @return mixed
  * @throws \ObjectivePHP\Primitives\Exception
  * @internal param ApplicationInterface $application
  *
  */
 public function run(ApplicationInterface $app)
 {
     $result = parent::run($app);
     if ($result instanceof Response) {
         $app->setResponse($result);
     } else {
         // set default content type
         $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html'));
         Collection::cast($result)->each(function ($value, $var) {
             Vars::set($var, $value);
         });
     }
 }
Example #12
0
 public function run(ApplicationInterface $app)
 {
     $matchedRoute = $app->getRequest()->getMatchedRoute();
     $action = Invokable::cast($matchedRoute->getAction());
     $app->getServicesFactory()->injectDependencies($action->getCallable());
     $app->setParam('runtime.action.middleware', $action);
     $result = $action->getCallable()($app);
     if ($result instanceof Response) {
         $app->setResponse($result);
     } else {
         // set default content type
         $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html'));
         Collection::cast($result)->each(function ($value, $var) {
             Vars::set($var, $value);
         });
     }
 }
 public function testContextCanBeReplacedByAnArray()
 {
     $event = new Event();
     $event->setContext(['a' => 'value']);
     $event->setContext(['b' => 'other value']);
     $this->assertEquals(Collection::cast(['b' => 'other value']), $event->getContext());
     $this->expectsException(function () use($event) {
         $event->setContext('wrong type - should be an array or an ArrayObject');
     }, Exception::class, null, Exception::EVENT_INVALID_CONTEXT);
     $this->expectsException(function () use($event) {
         $event->setContext($this);
     }, Exception::class, null, Exception::EVENT_INVALID_CONTEXT);
     $this->expectsException(function () use($event) {
         $newContext = new \stdClass();
         $newContext->property = 'wrong type - should be an array or an ArrayObject';
         $event->setContext($newContext);
     }, Exception::class, null, Exception::EVENT_INVALID_CONTEXT);
 }
Example #14
0
 public function __toString()
 {
     $flattenedAttributes = [];
     $this->each(function ($value, $attribute) use(&$flattenedAttributes) {
         // skip empty collections
         if ($value instanceof Collection && $value->isEmpty()) {
             return;
         }
         if (is_int($attribute)) {
             $flattenedAttributes[] = $value;
         } else {
             if (is_bool($value)) {
                 if ($value === true) {
                     $flattenedAttributes[] = $attribute;
                 }
             } else {
                 $flattenedAttributes[] = $attribute . '="' . Collection::cast($value)->join(' ') . '"';
             }
         }
     });
     return trim(implode(' ', $flattenedAttributes));
 }
Example #15
0
 /**
  * Merge two values according to the defined policy
  *
  * @param $first
  * @param $second
  *
  * @return mixed
  */
 public function merge($first, $second)
 {
     switch ($this->policy) {
         case MergePolicy::COMBINE:
             if ($first instanceof Collection) {
                 // Modify the first collection
                 return $first->append($second);
             } else {
                 return new Collection([$first, $second]);
             }
             break;
         case MergePolicy::REPLACE:
             return $second;
             break;
         case MergePolicy::ADD:
             return Collection::cast($first)->add(Collection::cast($second));
             break;
         case MergePolicy::NATIVE:
             return Collection::cast($first)->merge(Collection::cast($second));
             break;
         default:
             throw new Exception(sprintf('Policy "%s" does not exist', $this->policy), Exception::INVALID_PARAMETER);
     }
 }
Example #16
0
 public function __construct($routers = [])
 {
     $this->routers = Collection::cast($routers);
 }
 public function __construct($arg1 = null, $arg2 = null)
 {
     $this->args = Collection::cast($this->args);
     $this->args['arg1'] = $arg1;
     $this->args['arg2'] = $arg2;
 }
 public function setRoute($routeVars)
 {
     $this->params['route'] = Collection::cast($routeVars);
     return $this;
 }
Example #19
0
 /**
  * @param $params
  *
  * @return $this
  */
 public function setParams($params)
 {
     $this->params = Collection::cast($params);
     return $this;
 }
 public function testSingleAliasSetting()
 {
     $this->instance->setAliases('service.alias');
     $this->assertAttributeEquals(Collection::cast(['service.alias']), 'aliases', $this->instance);
 }
 static function factory($rawDefinition)
 {
     $rawDefinition = Collection::cast($rawDefinition);
     // first check an id has been provided
     if ($rawDefinition->lacks('id')) {
         throw new Exception('Missing mandatory \'id\' parameter in service definition', Exception::INCOMPLETE_SERVICE_SPECS);
     }
     // try to guess service type if not provided
     if ($rawDefinition->lacks('type')) {
         $matchingTypes = [];
         foreach (['instance' => PrefabServiceSpecs::class, 'class' => ClassServiceSpecs::class] as $key => $type) {
             if ($rawDefinition->has($key)) {
                 $matchingTypes[] = $type;
             }
         }
         if (!$matchingTypes) {
             // throw new Exception('The service specs factory has not been able to guess what type of service has been passed. Please check your syntax, or explicitly define the "type" key in your service specifications', Exception::INCOMPLETE_SERVICE_SPECS);
             // default to UndefinedService
             $matchingTypes[] = UndefinedServiceSpecs::class;
         }
         if (count($matchingTypes) > 1) {
             throw new Exception('Service specifications are ambiguous: they contain both "instance" and "class" key. Please remove the unneeded oneor explicitly define the "type" key in your service specifications ', Exception::AMBIGUOUS_SERVICE_SPECS);
         }
         // only one match
         $rawDefinition['type'] = array_pop($matchingTypes);
     }
     $serviceDefinition = call_user_func([$rawDefinition['type'], 'factory'], $rawDefinition);
     // static
     if ($rawDefinition->has('static')) {
         $serviceDefinition->setStatic($rawDefinition['static']);
     }
     // aliases
     if ($rawDefinition->has('alias') || $rawDefinition->has('aliases')) {
         $aliases = new Collection();
         if ($rawDefinition->has('alias')) {
             $aliases[] = $rawDefinition['alias'];
         }
         if ($rawDefinition->has('aliases')) {
             $aliases->merge($rawDefinition['aliases']);
         }
         $serviceDefinition->setAliases($aliases);
     }
     return $serviceDefinition;
 }
Example #22
0
 public function testCastWithAnArrayObject()
 {
     $value = new \ArrayObject(['a', 'b', 'c']);
     $castedCollection = Collection::cast($value);
     $this->assertInstanceOf(Collection::class, $castedCollection);
     $this->assertEquals($value->getArrayCopy(), $castedCollection->getInternalValue());
     $this->assertSame($castedCollection, Collection::cast($castedCollection));
 }
Example #23
0
 /**
  * @param $reference
  *
  * @return \ObjectivePHP\Primitives\Collection\Collection
  */
 public static function collection($reference)
 {
     return Collection::cast(self::get($reference));
 }
 /**
  * Trigger an event
  *
  * @param string $eventName
  * @param mixed $origin
  * @param mixed $context
  *
  * @param EventInterface $event
  * @return EventInterface
  * @throws Exception
  * @throws \ObjectivePHP\Primitives\Exception
  * @throws \ObjectivePHP\ServicesFactory\Exception\ServiceNotFoundException
  */
 public function trigger($eventName, $origin = null, $context = [], EventInterface $event = null)
 {
     if (is_null($origin)) {
         $origin = $this;
     }
     // cast context to Collection
     $context = Collection::cast($context);
     // cannot get event from factory
     // because of injection process
     // which triggers an event causing
     // an infinite loop...
     if (is_null($event)) {
         $event = new Event();
     }
     $event->setName($eventName)->setOrigin($origin)->setContext($context);
     // add reference to previous event if any
     if ($previous = \current($this->currentEventsQueue)) {
         $event->setPrevious($previous);
     }
     $this->currentEventsQueue[(string) $event->getName()] = $event;
     $listeners = $this->getListeners($eventName);
     // TODO sort listeners according to their priority or other mechanism
     $i = 0;
     foreach ($listeners as $listenersGroup) {
         $callbacks = [];
         // handle callbacks aggregate
         foreach ($listenersGroup as $alias => $callback) {
             if ($callback instanceof CallbacksAggregate) {
                 $callback->getCallbacks()->each(function ($callback, $callbackAlias) use(&$callbacks, $alias) {
                     $callbackAlias = implode('.', [$alias, $callbackAlias]);
                     $callbacks[$callbackAlias] = $callback;
                 });
             } else {
                 $callbacks[$alias] = $callback;
             }
         }
         foreach ($callbacks as $alias => $callback) {
             // handle service references
             if ($callback instanceof ServiceReference) {
                 $callback = $this->getServicesFactory()->get($callback->getId());
             }
             // if listener is a class name, instantiate it
             if (!is_callable($callback) && class_exists($callback)) {
                 $className = $callback;
                 $callback = new $className();
                 if (!is_callable($callback)) {
                     throw new Exception(sprintf('Class "%s" does not implement __invoke(), thus cannot be used as a callback', $className), Exception::EVENT_INVALID_CALLBACK);
                 }
             }
             $result = $callback($event);
             // gather exceptions
             if ($result instanceof \Exception) {
                 $event->setException($i, $result);
             }
             $event->setResult(is_string($alias) ? $alias : $i, $result);
             if ($event->isHalted()) {
                 // yes, this is a goto...
                 // I know that it was not absolutely needed, but it's a quite long story
                 // so please keep it as is.
                 // ping @EmmanuelJacoby :)
                 //
                 // @gdelamarre
                 goto shunt;
             }
             if (!is_string($alias)) {
                 $i++;
             }
         }
     }
     // target reached from within the triggering loop
     // if the event was halted by a listener
     shunt:
     // event has been triggered, it is now removed from events queue
     array_pop($this->currentEventsQueue);
     end($this->currentEventsQueue);
     return $event;
 }
 /**
  * Return all defined messages
  *
  * @return Collection
  */
 public function getMessages()
 {
     return Collection::cast($this->messages);
 }
Example #26
0
 /**
  * @param mixed $data
  */
 public static function setData($data)
 {
     self::$data = Collection::cast($data);
 }
Example #27
0
 /**
  * Merge two values according to the defined policy
  *
  * @param $first
  * @param $second
  *
  * @return mixed
  * @throws Exception
  */
 public function merge($first, $second)
 {
     $policy = $this->policy;
     if ($policy == MergePolicy::AUTO) {
         if (is_array($first) || $first instanceof \ArrayObject) {
             $policy = MergePolicy::NATIVE;
         } else {
             $policy = MergePolicy::REPLACE;
         }
     }
     switch ($policy) {
         case MergePolicy::SKIP:
             return is_null($first) ? $second : $first;
             break;
         case MergePolicy::COMBINE:
             if ($first instanceof Collection) {
                 if ($second instanceof Collection) {
                     return $first->merge($second);
                 } elseif (is_array($second)) {
                     return $first->append(...$second);
                 } else {
                     // Modify the first collection
                     return $first->append($second);
                 }
             } else {
                 if ($second instanceof Collection) {
                     if (is_array($first)) {
                         return $second->append(...$first);
                     } else {
                         // Modify the first collection
                         return $second->append($first);
                     }
                 } else {
                     // neither are Collection instance
                     $mergedValue = new Collection(array_filter(array_merge((array) $first, (array) $second), function ($value) {
                         return !is_null($value);
                     }));
                     return $mergedValue->toArray();
                 }
             }
             break;
         case MergePolicy::REPLACE:
             return $second;
             break;
         case MergePolicy::ADD:
             $mergedValue = Collection::cast($first)->add(Collection::cast($second));
             if (!$first instanceof Collection && !$second instanceof Collection) {
                 $mergedValue = $mergedValue->toArray();
             }
             return $mergedValue;
             break;
         case MergePolicy::NATIVE:
             $mergedValue = Collection::cast($first)->merge(Collection::cast($second));
             if (!$first instanceof Collection && !$second instanceof Collection) {
                 $mergedValue = $mergedValue->toArray();
             }
             return $mergedValue;
             break;
         default:
             throw new Exception(sprintf('Policy "%s" does not exist', $this->policy), Exception::INVALID_PARAMETER);
     }
 }
 public function getFilter()
 {
     return Collection::cast($this->filter);
 }
Example #29
0
 public function getMergers()
 {
     return Collection::cast($this->mergers);
 }
Example #30
0
 /**
  * @param $key
  *
  * @return $this
  */
 public function remove($reference)
 {
     $reference = $this->computeKeyFQN($reference);
     $matcher = $this->getMatcher();
     Collection::cast(self::$data)->each(function (&$value, $key) use($matcher, $reference) {
         if ($matcher->match($reference, $key)) {
             unset(self::$data[$key]);
         }
     });
     return $this;
 }