/** * When a property or method is requested through the presenter, an attempt * is * made to wrap the return value in it's own presenter. * * If the attempt fails due to no presenter existing for the presentable * object, or if the object simply isn't presentable, the raw value will * instead be returned. * * Support is available for IteratorAggregate instances, looping through the * collection and attempting to wrap each object in it's own presenter. * * @param mixed $value * * @return mixed * * @throws Exception */ protected function decorate($value) { // Laravel relation if (is_a($value, Relation::class)) { return $this->decorate($value->getResults()); } if (!$value instanceof IteratorAggregate && !$value instanceof Presentable) { return $value; } if ($value instanceof IteratorAggregate) { $collection = get_class($value); $items = []; foreach ($value as $item) { $items[] = $this->decorate($item); } return new $collection($items); } $presenter = $this->_factory->make($value, 'presenter'); if (is_null($presenter) || !$presenter instanceof Presenter) { return $value; } return new $presenter($value); }
/** * @inheritdoc */ public function produce($what) { $factory = new Factory(); return $factory->makeOrFail($this, $what); }
/** * Derives a presenter for the passed object. If the presenter does not * exist an exception is thrown. * * @throws NotProducibleException * @param Producer $object * @return Presenter */ public function makeOrFail(Presentable $object) { return $this->productionFactory->makeOrFail($object, 'presenter'); }