晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/cloudlinux/alt-php53/root/usr/share/pear/Symfony/Component/Form/ |
| Current File : //opt/cloudlinux/alt-php53/root/usr/share/pear/Symfony/Component/Form/FormBuilder.php |
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* A builder for creating {@link Form} instances.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormBuilder extends FormConfigBuilder implements \IteratorAggregate, FormBuilderInterface
{
/**
* The children of the form builder.
*
* @var FormBuilderInterface[]
*/
private $children = array();
/**
* The data of children who haven't been converted to form builders yet.
*
* @var array
*/
private $unresolvedChildren = array();
/**
* Creates a new form builder.
*
* @param string $name
* @param string $dataClass
* @param EventDispatcherInterface $dispatcher
* @param FormFactoryInterface $factory
* @param array $options
*/
public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, FormFactoryInterface $factory, array $options = array())
{
parent::__construct($name, $dataClass, $dispatcher, $options);
$this->setFormFactory($factory);
}
/**
* {@inheritdoc}
*/
public function add($child, $type = null, array $options = array())
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($child instanceof self) {
$this->children[$child->getName()] = $child;
// In case an unresolved child with the same name exists
unset($this->unresolvedChildren[$child->getName()]);
return $this;
}
if (!is_string($child) && !is_int($child)) {
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormBuilder');
}
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
}
// Add to "children" to maintain order
$this->children[$child] = null;
$this->unresolvedChildren[$child] = array(
'type' => $type,
'options' => $options,
);
return $this;
}
/**
* {@inheritdoc}
*/
public function create($name, $type = null, array $options = array())
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (null === $type && null === $this->getDataClass()) {
$type = 'text';
}
if (null !== $type) {
return $this->getFormFactory()->createNamedBuilder($name, $type, null, $options);
}
return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $name, null, $options);
}
/**
* {@inheritdoc}
*/
public function get($name)
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (isset($this->unresolvedChildren[$name])) {
return $this->resolveChild($name);
}
if (isset($this->children[$name])) {
return $this->children[$name];
}
throw new InvalidArgumentException(sprintf('The child with the name "%s" does not exist.', $name));
}
/**
* {@inheritdoc}
*/
public function remove($name)
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
unset($this->unresolvedChildren[$name]);
if (array_key_exists($name, $this->children)) {
unset($this->children[$name]);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function has($name)
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (isset($this->unresolvedChildren[$name])) {
return true;
}
if (isset($this->children[$name])) {
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
public function all()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->resolveChildren();
return $this->children;
}
/**
* {@inheritdoc}
*/
public function count()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return count($this->children);
}
/**
* {@inheritdoc}
*/
public function getFormConfig()
{
$config = parent::getFormConfig();
$config->children = array();
$config->unresolvedChildren = array();
return $config;
}
/**
* {@inheritdoc}
*/
public function getForm()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->resolveChildren();
$form = new Form($this->getFormConfig());
foreach ($this->children as $child) {
// Automatic initialization is only supported on root forms
$form->add($child->setAutoInitialize(false)->getForm());
}
if ($this->getAutoInitialize()) {
// Automatically initialize the form if it is configured so
$form->initialize();
}
return $form;
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return new \ArrayIterator($this->children);
}
/**
* Converts an unresolved child into a {@link FormBuilder} instance.
*
* @param string $name The name of the unresolved child.
*
* @return FormBuilder The created instance.
*/
private function resolveChild($name)
{
$info = $this->unresolvedChildren[$name];
$child = $this->create($name, $info['type'], $info['options']);
$this->children[$name] = $child;
unset($this->unresolvedChildren[$name]);
return $child;
}
/**
* Converts all unresolved children into {@link FormBuilder} instances.
*/
private function resolveChildren()
{
foreach ($this->unresolvedChildren as $name => $info) {
$this->children[$name] = $this->create($name, $info['type'], $info['options']);
}
$this->unresolvedChildren = array();
}
}
|