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 OnDemand(function () use($obj, $name) {
                     return ArrayHelper::objectsToArray($obj->{'get' . $name}(), null, array('Curry\\Util\\Propel', 'toTwig'));
                 });
             } else {
                 $name = $relation->getName();
                 $p[lcfirst($name)] = new OnDemand(function () use($obj, $name) {
                     $rel = $obj->{'get' . $name}();
                     return $rel ? Propel::toTwig($rel) : null;
                 });
             }
         }
     }
     // Automatic URL
     $p['Url'] = new 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;
 }