/** * @param mixed $value */ public function toString($value, Type $type = NULL) { if (isset($type)) { // wenn wir den typ checken statt der value geht auch (int) $timestamp, new DateTimeType() // das ist cooler if ($type instanceof Types\DateTimeType) { if ($value) { return $value->i18n_format($this->dateTimeFormat); } } if ($type instanceof Types\DateType) { if ($value) { return $value->i18n_format($this->dateFormat); } } if ($type instanceof Types\EntityType && $value != NULL) { return $value->getContextLabel(); } $that = $this; if ($type instanceof Types\CollectionType) { return \Psc\FE\Helper::listObjects($value, ', ', function ($innerValue) use($that, $type) { // ersetzt den getter try { return $that->toString($innerValue, $type->getType()); } catch (NotTypedException $e) { // was tun wenn wir den inner type nicht kennen? return $that->toString($innerValue); } }); } if ($type instanceof Types\I18nType) { return \Psc\FE\Helper::listObjects($value, "<br />", function ($innerValue, $key) use($that, $type) { // ersetzt den getter try { return '[' . $key . '] ' . $that->toString($innerValue, $type->getType()); } catch (NotTypedException $e) { // was tun wenn wir den inner type nicht kennen? return $that->toString($innerValue); } }); } if ($type instanceof Types\BooleanType) { if ($value == TRUE) { return '<span class="ui-icon ui-icon-check"></span>'; } else { return ''; } } if ($type instanceof Types\MarkupTextType) { return TPL::MiniMarkup($value); } } return (string) $value; }
public function generateData(Type $type) { switch ($type->getName()) { case 'String': return $this->generateString(15); case 'Integer': return mt_rand(0, 99999) * (mt_rand(0, 1) === 0 ? -1 : 1); // negativ, positiv, 0-xxxx // negativ, positiv, 0-xxxx case 'Array': // wir füllen den array mit random strings, oder whatever, numerische schlüssel $array = array(); if ($type->isTyped()) { for ($i = 1; $i <= 15; $i++) { $array[] = $this->generateData($type->getType()); } } else { $randomTypes = array(new StringType(), new IntegerType(), new BooleanType()); for ($i = 1; $i <= 15; $i++) { $array[] = $this->generateDataRandomType($randomTypes); } } return $array; case 'Boolean': return mt_rand(0, 1) === 1; } throw new \Psc\Code\NotImplementedException('Kein switch für randomData für: ' . $type->getName()); }
public function walkListValue(array $list, Type $type) { $walkedList = array(); foreach ($list as $value) { $walkedList[] = $this->walkPHPValue($value, $type instanceof EnclosingType && $type->isTyped() ? $type->getType() : NULL, 'list'); } return $this->codeWriter->writeList($walkedList, $type); }
public function walkCollection($collection, Type $collectionType = NULL) { $walkedItems = array(); /* 1. Fall: wir kennen den Inhalt des Arrays (er hat einen Typ) */ if (isset($collectionType) && $collectionType->isTyped()) { $itemType = $collectionType->getType(); foreach ($collection->toArray() as $key => $item) { $walkedItems[$key] = $this->walkCollectionItem($item, $itemType, $key, $collection, $collectionType); } } else { /* 2. Fall wir kennen den Inhalt nicht */ if (!isset($collectionType)) { $collectionType = $this->inferType($collection); } foreach ($collection->toArray() as $key => $item) { $itemType = $this->inferCollectionItemType($item, $key, $collection, $collectionType); // damit walkArrayEntry hier volle power hat rufen wir direkt mit $entry auf nicht mit $this->walk($entry) $walkedItems[$key] = $this->walkCollectionItem($item, $itemType, $key, $collection, $collectionType); } } return $this->decorateCollection($walkedItems, $collectionType); }