コード例 #1
0
 function testStaticGetterSetter()
 {
     $bar = new StdClass();
     $bar->bar = 'BarBar';
     $foo = \arc\prototype::create(['bar' => [':set' => static function ($self, $value) use($bar) {
         $bar->bar = $value . 'Bar';
     }, ':get' => static function ($self) use($bar) {
         return 'Foo' . $bar->bar;
     }]]);
     $result = $foo->bar;
     $this->assertEquals('FooBarBar', $result);
     $foo->bar = 'Foo';
     $result = $foo->bar;
     $this->assertEquals('FooFooBar', $result);
 }
コード例 #2
0
 /**
  *
  */
 public function __destruct()
 {
     \arc\prototype::_destroy($this);
     return $this->_tryToCall($this->__destruct);
 }
コード例 #3
0
 function testAssign()
 {
     $foo = \arc\prototype::create(['bar' => 'Bar']);
     $bar = \arc\prototype::extend($foo, ['foo' => 'Foo']);
     $zod = \arc\prototype::create(['zod' => 'Zod']);
     $zed = \arc\prototype::create(['zed' => 'Zed']);
     $zoom = \arc\prototype::assign($zod, $bar, $zed);
     $this->assertEquals($zoom->bar, $foo->bar);
     $this->assertEquals($zoom->zod, $zod->zod);
 }
コード例 #4
0
ファイル: html.php プロジェクト: potherca-contrib/arc-html
 public static function formBuilder($fields, $formAttributes = [])
 {
     if (!self::$formBuilder) {
         self::$formBuilder = \arc\prototype::create(['fields' => [], 'attributes' => [], ':parseField' => function ($self, $field, $key) {
             if (!is_array($field)) {
                 $field = ['name' => $field, 'label' => $field];
             }
             $defaults = ['type' => 'text', 'name' => is_numeric($key) ? 'field_' . $key : $key, 'label' => is_numeric($key) ? false : $key, 'value' => '', 'default' => null, 'attributes' => [], 'inputAttributes' => []];
             $defaults['label'] = $defaults['name'];
             return $field + $defaults;
         }, ':getValue' => function ($self, $field) {
             $selected = null;
             if ($field['value']) {
                 $selected = $field['value'];
             } else {
                 if ($field['default']) {
                     $selected = $field['default'];
                 }
             }
             return $selected;
         }, ':renderOptions' => function ($self, $field) {
             $selected = $self->getValue($field);
             $options = '';
             foreach ((array) $field['options'] as $key => $option) {
                 $attributes = ['value' => $key];
                 if ($key === $selected) {
                     $attributes['selected'] = true;
                 }
                 $options .= \arc\html::option($attributes, $option);
             }
             return $options;
         }, ':renderInputSelect' => function ($self, $field) {
             return \arc\html::select(['id' => $field['name'], 'name' => $field['name']] + (array) $field['inputAttributes'], $self->renderOptions($field));
         }, ':renderInputRadioGroup' => function ($self, $field) {
             $selected = $self->getValue($field);
             $radios = '';
             foreach ((array) $field['options'] as $key => $option) {
                 $attributes = $field['inputAttributes'];
                 if ($key === $selected) {
                     $attributes['checked'] = true;
                 }
                 $radios .= $self->renderInputRadio(['inputAttributes' => $attributes] + $field);
             }
             return \arc\html::div(['class' => 'arc-form-radiogroup'], $radios);
         }, ':renderInputRadio' => function ($self, $field) {
             return \arc\html::input(['type' => 'radio', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']);
         }, ':renderInputTextarea' => function ($self, $field) {
             return \arc\html::textarea(['id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']);
         }, ':renderInputPassword' => function ($self, $field) {
             return \arc\html::input(['type' => 'password', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']);
         }, ':renderInputText' => function ($self, $field) {
             return \arc\html::input(['type' => 'text', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']);
         }, ':renderInputHidden' => function ($self, $field) {
             return \arc\html::input(['type' => 'hidden', 'id' => $field['name'], 'name' => $field['name'], 'value' => $field['value']] + (array) $field['inputAttributes']);
         }, ':renderInput' => function ($self, $field) {
             $renderMethod = 'renderInput' . ucfirst($field['type']);
             if (!isset($self->{$renderMethod})) {
                 throw new \arc\ExceptionMethodNotFound('No render method for input type ' . $field['type'], 404);
             }
             return $self->{$renderMethod}($field);
         }, ':renderLabel' => function ($self, $field) {
             if ($field['label']) {
                 return \arc\html::label(['for' => $field['name']], $field['label']);
             } else {
                 return '';
             }
         }, ':renderField' => function ($self, $field, $key = null) {
             $field = $self->parseField($field, $key);
             $contents = $self->renderLabel($field);
             $contents .= $self->renderInput($field);
             $attributes = isset($field['attributes']) ? $field['attributes'] : [];
             if (isset($field['class'])) {
                 $attributes['class'] = $field['class'];
             }
             return \arc\html::div($attributes, $contents);
         }, ':__toString' => function ($self) {
             $fields = '';
             foreach ($self->fields as $key => $field) {
                 $fields .= $self->renderField($field, $key);
             }
             return \arc\html::form($self->attributes, $fields);
         }]);
     }
     return \arc\prototype::extend(self::$formBuilder, ['fields' => $fields, 'attributes' => $formAttributes]);
 }