Example #1
0
 public static function toTwig(BaseObject $obj, $checkToTwig = true, $includeRelated = true, $includeVirtual = true, $includeI18n = true)
 {
     if ($checkToTwig && method_exists($obj, 'toTwig')) {
         return $obj->toTwig();
     }
     $tableMap = PropelQuery::from(get_class($obj))->getTableMap();
     $p = $obj->toArray(BasePeer::TYPE_PHPNAME);
     if ($includeRelated) {
         foreach ($tableMap->getRelations() as $relation) {
             if (in_array($relation->getType(), array(RelationMap::ONE_TO_MANY, RelationMap::MANY_TO_MANY))) {
                 $name = $relation->getPluralName();
                 $p[lcfirst($name)] = new Curry_OnDemand(function () use($obj, $name) {
                     return Curry_Array::objectsToArray($obj->{'get' . $name}(), null, array('Curry_Propel', 'toTwig'));
                 });
             } else {
                 $name = $relation->getName();
                 $p[lcfirst($name)] = new Curry_OnDemand(function () use($obj, $name) {
                     $rel = $obj->{'get' . $name}();
                     return $rel ? Curry_Propel::toTwig($rel) : null;
                 });
             }
         }
     }
     // Automatic URL
     $p['Url'] = new Curry_OnDemand(function () use($obj, $tableMap) {
         $params = array();
         foreach ($tableMap->getPrimaryKeys() as $pk) {
             $params[$pk->getName()] = $obj->{'get' . $pk->getPhpName()}();
         }
         return url(L(get_class($obj) . 'Url'), $params);
     });
     // Virtual columns
     if ($includeVirtual) {
         $p = array_merge($p, $obj->getVirtualColumns());
     }
     // I18n behavior columns
     if ($includeI18n && self::hasBehavior('i18n', $tableMap)) {
         $translation = $obj->getCurrentTranslation();
         $p = array_merge($p, $translation->toArray());
     }
     return $p;
 }
Example #2
0
 public function templateToString($template, BaseObject $obj)
 {
     $tpl = Curry_Twig_Template::loadTemplateString($template);
     return $tpl->render(Curry_Propel::toTwig($obj));
 }
Example #3
0
 /**
  * Override formatter function for one item.
  *
  * @param PDOStatement $stmt
  * @return mixed
  */
 public function formatOne(PDOStatement $stmt)
 {
     $item = parent::format($stmt);
     if ($this->formatterFunction) {
         return call_user_func($this->formatterFunction, $item);
     }
     if (is_object($item) && $item instanceof BaseObject) {
         return Curry_Propel::toTwig($item);
     }
     return $item;
 }