/** * Gets the form elements code to add * * @param array $fields * @return string */ protected function _getFormElements($fields) { $upperFirst = new UCFirst(); $camelCaseToSpace = new CamelCaseToSeparator(); $elements = ''; $fields = explode(',', $fields); foreach ($fields as $field) { list($name, $type) = strpos($field, ':') ? explode(':', $field) : array($field, 'string'); $ucFirst = $upperFirst->filter($name); $label = $camelCaseToSpace->filter($ucFirst); switch ($type) { case 'text': $elements .= " \$this->addElement('textarea', " . "'{$name}', array('label' => '{$label}'));" . PHP_EOL; break; default: $elements .= " \$this->addElement('text'," . " '{$name}', array('label' => " . "'{$label}'));" . PHP_EOL; break; } } if (!empty($elements)) { $elements .= " \$this->addElement('submit', 'Save');" . PHP_EOL; } return $elements; }
/** * Creates the show view * * @param string $entity * @param array $fields * @return string */ protected static function createShowView($entity, $fields) { $upperFirst = new UCFirst(); $lowerFirst = new LCFirst(); $camelCaseToSpace = new CamelCaseToSeparator(); $pluralize = new Pluralize(); $entityPlural = $pluralize->filter($entity); $lowerEntity = $lowerFirst->filter($entity); $data = $rendered = ''; $fields = explode(',', $fields); foreach ($fields as $field) { $fieldAndType = explode(':', $field); list($field, $type) = count($fieldAndType) === 2 ? $fieldAndType : array($field, 'string'); $ucFirst = $upperFirst->filter($field); $field = $camelCaseToSpace->filter($ucFirst); $data .= "'get{$ucFirst}' => 'some {$field}'," . PHP_EOL . " "; $rendered .= '$this->rendered->should->haveSelector(\'p>b\', ' . 'array(\'text\' => \'' . $field . ':\')); $this->rendered->should->contain(\'some ' . $field . '\');'; } return <<<SHOWVIEW <?php namespace {$entityPlural}; use \\PHPSpec\\Context\\Zend\\View as ViewContext; class DescribeShow extends ViewContext { function before() { \$this->assign('{$lowerEntity}', \$this->stub('{$entity}', array( 'getId' => '1', {$data} ))); } function itRendersThe{$entity}() { \$this->render(); {$rendered} } } SHOWVIEW; }
/** * Gets the content of scaffolded show view * * @param string $entity * @param array $fields * @return string */ protected static function _getShowViewContent($entity, $fields) { $upperFirst = new UCFirst(); $lowerFirst = new LCFirst(); $camelCaseToSpace = new CamelCaseToSeparator(); $camelCaseToDash = new CamelCaseToDash(); $pluralize = new Pluralize(); $lowerEntity = $lowerFirst->filter($entity); $lowerEntityPlural = $pluralize->filter($lowerEntity); $dashedEntityPlural = $camelCaseToDash->filter($lowerEntityPlural); $properties = ''; foreach ($fields as $field) { $upper = $upperFirst->filter($field); $field = $camelCaseToSpace->filter($field); $field = $upperFirst->filter($field); $properties .= " <p>\n <b>{$field}:</b>\n <?php echo \$this->escape(\$this->{$lowerEntity}->get{$upper}()) ?></h3>\n </p>" . PHP_EOL . PHP_EOL; } return "<h1>Show {$entity}</h1>\n\n{$properties}\n\n<a href=\"<?php echo \$this->baseUrl(" . "'{$dashedEntityPlural}/edit/id/' ." . " (int)\$this->{$lowerEntity}->getId()) ?>\">Edit</a> |\n<a href=\"<?php echo \$this->baseUrl(" . "'{$dashedEntityPlural}') ?>\">Back</a>"; }