Passed
Push — master ( 17602b...453007 )
by Chris
04:37
created

Form   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 5
dl 0
loc 82
c 0
b 0
f 0
rs 10
1
<?php
2
/*
3
This project is Licenced under The MIT License (MIT)
4
5
Copyright (c) 2014 Christopher Seufert
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
THE SOFTWARE.
24
25
 */
26
namespace Seufert\Hamle;
27
28
/**
29
 * HAMLE Form Class
30
 * 
31
 * @package hamle
32
 * @author Chris Seufert <[email protected]>
33
 */
34
class Form {
35
  /**
36
   * @var Field[] Form Fields
37
   */
38
  protected $_fields;
39
  protected $_name;
40
  public $hint;
41
  protected $_data;
42
  /**
43
   * Setup Method
44
   * Fills the $this->fields array with hamleFields
45
   */
46
  function setup() {
47
    throw new Exception\RunTime("You must configure the form in the setup ".
48
      "function, by adding fields to the \$this->fields array");
49
  }
50
  
51
  function __construct() {
52
    $this->_data = func_get_args();
53
    $this->_name = get_called_class();
54
    $this->setup();
55
    $fields = $this->_fields;
56
    $this->_fields = array();
57
    foreach($fields as $v) {
58
      $this->_fields[$v->name] = $v;
59
      $v->form($this->_name);
60
    }
61
    $this->postInit();
62
  }
63
  function postInit() {}
64
  
65
  function process() {
66
    $clicked = "";
67
    foreach($this->_fields as $f)
68
      if($f instanceOf Field\Button)
69
        if($f->isClicked())
70
          $clicked = $f;
71
    foreach($this->_fields as $f)
72
      $f->doProcess($clicked?true:false);
73
    if($clicked)
74
      try {
75
        $this->onSubmit($clicked);
76
      } catch(Exception\FormInvalid $e) {
77
        $this->hint = $e->getMessage();
78
      }
79
  }
80
  
81
  function isValid() {
82
    $valid = true;
83
    foreach($this->_fields as $f)
84
      $valid = $f->valid && $valid;
85
    return $valid;
86
  }
87
  /**
88
   * Called upon form submission, $button will be assigned to the button that was clicked
89
   * @param Field\Button $button
90
   * @throws Exception\NoKey
91
   */
92
  function onSubmit($button) { }
93
  
94
  function getFields() {
95
    return $this->_fields;
96
  }
97
  function getField($n) {
98
    if(!isset($this->_fields[$n]))
99
      throw new Exception\NoKey("unable to find form field ($n)");
100
    return $this->_fields[$n];
101
  }
102
  function __get($n) {
103
    return $this->getField($n);
104
  }
105
  
106
  function getHTMLProp() {
107
    return array('action'=>'','method'=>'post','name'=>$this->_name,
108
                                        'enctype'=>'multipart/form-data');
109
  }
110
111
  function preEndTag() {
112
    echo "<input type='hidden' name='{$this->_name}__submit' value='submit' />";
113
  }
114
  
115
}
116