993 lines
44 KiB
HTML
993 lines
44 KiB
HTML
<html>
|
|
<head>
|
|
<title>Coverage for Doctrine_Validator</title>
|
|
<style type="text/css">
|
|
.covered{ background: green;}
|
|
.normal{ background: white;}
|
|
.red{ background: red;}
|
|
.orange{ background: #f90;}
|
|
</style>
|
|
</head>
|
|
<body><h1>Coverage for Doctrine_Validator</h1><p><a href="index.php">Back to coverage report</a></p><table>
|
|
<tr><td>1</td>
|
|
<td class="normal"><?php
|
|
</td></tr>
|
|
<tr><td>2</td>
|
|
<td class="normal">/*
|
|
</td></tr>
|
|
<tr><td>3</td>
|
|
<td class="normal"> * $Id: Validator.php 2965 2007-10-21 08:16:31Z romanb $
|
|
</td></tr>
|
|
<tr><td>4</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>5</td>
|
|
<td class="normal"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
</td></tr>
|
|
<tr><td>6</td>
|
|
<td class="normal"> * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
</td></tr>
|
|
<tr><td>7</td>
|
|
<td class="normal"> * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
</td></tr>
|
|
<tr><td>8</td>
|
|
<td class="normal"> * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
</td></tr>
|
|
<tr><td>9</td>
|
|
<td class="normal"> * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
</td></tr>
|
|
<tr><td>10</td>
|
|
<td class="normal"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
</td></tr>
|
|
<tr><td>11</td>
|
|
<td class="normal"> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
</td></tr>
|
|
<tr><td>12</td>
|
|
<td class="normal"> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
</td></tr>
|
|
<tr><td>13</td>
|
|
<td class="normal"> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
</td></tr>
|
|
<tr><td>14</td>
|
|
<td class="normal"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
</td></tr>
|
|
<tr><td>15</td>
|
|
<td class="normal"> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
</td></tr>
|
|
<tr><td>16</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>17</td>
|
|
<td class="normal"> * This software consists of voluntary contributions made by many individuals
|
|
</td></tr>
|
|
<tr><td>18</td>
|
|
<td class="normal"> * and is licensed under the LGPL. For more information, see
|
|
</td></tr>
|
|
<tr><td>19</td>
|
|
<td class="normal"> * <http://www.phpdoctrine.com>.
|
|
</td></tr>
|
|
<tr><td>20</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>21</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>22</td>
|
|
<td class="normal">/**
|
|
</td></tr>
|
|
<tr><td>23</td>
|
|
<td class="normal"> * Doctrine_Validator
|
|
</td></tr>
|
|
<tr><td>24</td>
|
|
<td class="normal"> * Doctrine_Validator performs validations in record properties
|
|
</td></tr>
|
|
<tr><td>25</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>26</td>
|
|
<td class="normal"> * @package Doctrine
|
|
</td></tr>
|
|
<tr><td>27</td>
|
|
<td class="normal"> * @subpackage Validator
|
|
</td></tr>
|
|
<tr><td>28</td>
|
|
<td class="normal"> * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
</td></tr>
|
|
<tr><td>29</td>
|
|
<td class="normal"> * @link www.phpdoctrine.com
|
|
</td></tr>
|
|
<tr><td>30</td>
|
|
<td class="normal"> * @since 1.0
|
|
</td></tr>
|
|
<tr><td>31</td>
|
|
<td class="normal"> * @version $Revision: 2965 $
|
|
</td></tr>
|
|
<tr><td>32</td>
|
|
<td class="normal"> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
</td></tr>
|
|
<tr><td>33</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>34</td>
|
|
<td class="covered">class Doctrine_Validator extends Doctrine_Locator_Injectable
|
|
</td></tr>
|
|
<tr><td>35</td>
|
|
<td class="normal">{
|
|
</td></tr>
|
|
<tr><td>36</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>37</td>
|
|
<td class="normal"> * @var array $validators an array of validator objects
|
|
</td></tr>
|
|
<tr><td>38</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>39</td>
|
|
<td class="normal"> private static $validators = array();
|
|
</td></tr>
|
|
<tr><td>40</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>41</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>42</td>
|
|
<td class="normal"> * returns a validator object
|
|
</td></tr>
|
|
<tr><td>43</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>44</td>
|
|
<td class="normal"> * @param string $name
|
|
</td></tr>
|
|
<tr><td>45</td>
|
|
<td class="normal"> * @return Doctrine_Validator_Interface
|
|
</td></tr>
|
|
<tr><td>46</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>47</td>
|
|
<td class="normal"> public static function getValidator($name)
|
|
</td></tr>
|
|
<tr><td>48</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>49</td>
|
|
<td class="covered"> if ( ! isset(self::$validators[$name])) {
|
|
</td></tr>
|
|
<tr><td>50</td>
|
|
<td class="covered"> $class = 'Doctrine_Validator_' . ucwords(strtolower($name));
|
|
</td></tr>
|
|
<tr><td>51</td>
|
|
<td class="covered"> if (class_exists($class)) {
|
|
</td></tr>
|
|
<tr><td>52</td>
|
|
<td class="covered"> self::$validators[$name] = new $class;
|
|
</td></tr>
|
|
<tr><td>53</td>
|
|
<td class="covered"> } else {
|
|
</td></tr>
|
|
<tr><td>54</td>
|
|
<td class="normal"> throw new Doctrine_Exception("Validator named '$name' not available.");
|
|
</td></tr>
|
|
<tr><td>55</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>56</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>57</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>58</td>
|
|
<td class="covered"> return self::$validators[$name];
|
|
</td></tr>
|
|
<tr><td>59</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>60</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>61</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>62</td>
|
|
<td class="normal"> * validates a given record and saves possible errors
|
|
</td></tr>
|
|
<tr><td>63</td>
|
|
<td class="normal"> * in Doctrine_Validator::$stack
|
|
</td></tr>
|
|
<tr><td>64</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>65</td>
|
|
<td class="normal"> * @param Doctrine_Record $record
|
|
</td></tr>
|
|
<tr><td>66</td>
|
|
<td class="normal"> * @return void
|
|
</td></tr>
|
|
<tr><td>67</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>68</td>
|
|
<td class="normal"> public function validateRecord(Doctrine_Record $record)
|
|
</td></tr>
|
|
<tr><td>69</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>70</td>
|
|
<td class="covered"> $columns = $record->getTable()->getColumns();
|
|
</td></tr>
|
|
<tr><td>71</td>
|
|
<td class="covered"> $component = $record->getTable()->getComponentName();
|
|
</td></tr>
|
|
<tr><td>72</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>73</td>
|
|
<td class="covered"> $errorStack = $record->getErrorStack();
|
|
</td></tr>
|
|
<tr><td>74</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>75</td>
|
|
<td class="normal"> // if record is transient all fields will be validated
|
|
</td></tr>
|
|
<tr><td>76</td>
|
|
<td class="normal"> // if record is persistent only the modified fields will be validated
|
|
</td></tr>
|
|
<tr><td>77</td>
|
|
<td class="covered"> $data = ($record->exists()) ? $record->getModified() : $record->getData();
|
|
</td></tr>
|
|
<tr><td>78</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>79</td>
|
|
<td class="covered"> $err = array();
|
|
</td></tr>
|
|
<tr><td>80</td>
|
|
<td class="covered"> foreach ($data as $key => $value) {
|
|
</td></tr>
|
|
<tr><td>81</td>
|
|
<td class="covered"> if ($value === self::$_null) {
|
|
</td></tr>
|
|
<tr><td>82</td>
|
|
<td class="covered"> $value = null;
|
|
</td></tr>
|
|
<tr><td>83</td>
|
|
<td class="covered"> } else if ($value instanceof Doctrine_Record) {
|
|
</td></tr>
|
|
<tr><td>84</td>
|
|
<td class="covered"> $value = $value->getIncremented();
|
|
</td></tr>
|
|
<tr><td>85</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>86</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>87</td>
|
|
<td class="covered"> $column = $columns[$key];
|
|
</td></tr>
|
|
<tr><td>88</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>89</td>
|
|
<td class="covered"> if ($column['type'] == 'enum') {
|
|
</td></tr>
|
|
<tr><td>90</td>
|
|
<td class="red"> $value = $record->getTable()->enumIndex($key, $value);
|
|
</td></tr>
|
|
<tr><td>91</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>92</td>
|
|
<td class="red"> if ($value === false) {
|
|
</td></tr>
|
|
<tr><td>93</td>
|
|
<td class="red"> $errorStack->add($key, 'enum');
|
|
</td></tr>
|
|
<tr><td>94</td>
|
|
<td class="red"> continue;
|
|
</td></tr>
|
|
<tr><td>95</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>96</td>
|
|
<td class="red"> }
|
|
</td></tr>
|
|
<tr><td>97</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>98</td>
|
|
<td class="covered"> if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) {
|
|
</td></tr>
|
|
<tr><td>99</td>
|
|
<td class="covered"> if ( ! $this->validateLength($column, $key, $value)) {
|
|
</td></tr>
|
|
<tr><td>100</td>
|
|
<td class="covered"> $errorStack->add($key, 'length');
|
|
</td></tr>
|
|
<tr><td>101</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>102</td>
|
|
<td class="covered"> continue;
|
|
</td></tr>
|
|
<tr><td>103</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>104</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>105</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>106</td>
|
|
<td class="covered"> foreach ($column as $name => $args) {
|
|
</td></tr>
|
|
<tr><td>107</td>
|
|
<td class="covered"> if (empty($name)
|
|
</td></tr>
|
|
<tr><td>108</td>
|
|
<td class="red"> || $name == 'primary'
|
|
</td></tr>
|
|
<tr><td>109</td>
|
|
<td class="covered"> || $name == 'protected'
|
|
</td></tr>
|
|
<tr><td>110</td>
|
|
<td class="covered"> || $name == 'autoincrement'
|
|
</td></tr>
|
|
<tr><td>111</td>
|
|
<td class="covered"> || $name == 'default'
|
|
</td></tr>
|
|
<tr><td>112</td>
|
|
<td class="covered"> || $name == 'values'
|
|
</td></tr>
|
|
<tr><td>113</td>
|
|
<td class="covered"> || $name == 'sequence'
|
|
</td></tr>
|
|
<tr><td>114</td>
|
|
<td class="covered"> || $name == 'zerofill'
|
|
</td></tr>
|
|
<tr><td>115</td>
|
|
<td class="covered"> || $name == 'scale') {
|
|
</td></tr>
|
|
<tr><td>116</td>
|
|
<td class="covered"> continue;
|
|
</td></tr>
|
|
<tr><td>117</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>118</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>119</td>
|
|
<td class="covered"> if (strtolower($name) === 'notnull' && isset($column['autoincrement'])) {
|
|
</td></tr>
|
|
<tr><td>120</td>
|
|
<td class="covered"> continue;
|
|
</td></tr>
|
|
<tr><td>121</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>122</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>123</td>
|
|
<td class="covered"> if (strtolower($name) == 'length') {
|
|
</td></tr>
|
|
<tr><td>124</td>
|
|
<td class="covered"> if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS)) {
|
|
</td></tr>
|
|
<tr><td>125</td>
|
|
<td class="covered"> if ( ! $this->validateLength($column, $key, $value)) {
|
|
</td></tr>
|
|
<tr><td>126</td>
|
|
<td class="covered"> $errorStack->add($key, 'length');
|
|
</td></tr>
|
|
<tr><td>127</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>128</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>129</td>
|
|
<td class="covered"> continue;
|
|
</td></tr>
|
|
<tr><td>130</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>131</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>132</td>
|
|
<td class="covered"> if (strtolower($name) == 'type') {
|
|
</td></tr>
|
|
<tr><td>133</td>
|
|
<td class="covered"> if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES)) {
|
|
</td></tr>
|
|
<tr><td>134</td>
|
|
<td class="covered"> if ( ! self::isValidType($value, $column['type'])) {
|
|
</td></tr>
|
|
<tr><td>135</td>
|
|
<td class="covered"> $errorStack->add($key, 'type');
|
|
</td></tr>
|
|
<tr><td>136</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>137</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>138</td>
|
|
<td class="covered"> continue;
|
|
</td></tr>
|
|
<tr><td>139</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>140</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>141</td>
|
|
<td class="covered"> $validator = self::getValidator($name);
|
|
</td></tr>
|
|
<tr><td>142</td>
|
|
<td class="covered"> $validator->invoker = $record;
|
|
</td></tr>
|
|
<tr><td>143</td>
|
|
<td class="covered"> $validator->field = $key;
|
|
</td></tr>
|
|
<tr><td>144</td>
|
|
<td class="covered"> $validator->args = $args;
|
|
</td></tr>
|
|
<tr><td>145</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>146</td>
|
|
<td class="covered"> if ( ! $validator->validate($value)) {
|
|
</td></tr>
|
|
<tr><td>147</td>
|
|
<td class="covered"> $errorStack->add($key, $name);
|
|
</td></tr>
|
|
<tr><td>148</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>149</td>
|
|
<td class="normal"> //$err[$key] = 'not valid';
|
|
</td></tr>
|
|
<tr><td>150</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>151</td>
|
|
<td class="normal"> // errors found quit validation looping for this column
|
|
</td></tr>
|
|
<tr><td>152</td>
|
|
<td class="normal"> //break;
|
|
</td></tr>
|
|
<tr><td>153</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>154</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>155</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>156</td>
|
|
<td class="covered"> if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) {
|
|
</td></tr>
|
|
<tr><td>157</td>
|
|
<td class="covered"> if ( ! self::isValidType($value, $column['type'])) {
|
|
</td></tr>
|
|
<tr><td>158</td>
|
|
<td class="red"> $errorStack->add($key, 'type');
|
|
</td></tr>
|
|
<tr><td>159</td>
|
|
<td class="red"> continue;
|
|
</td></tr>
|
|
<tr><td>160</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>161</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>162</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>163</td>
|
|
<td class="covered"> }
|
|
</td></tr>
|
|
<tr><td>164</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>165</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>166</td>
|
|
<td class="normal"> * Validates the length of a field.
|
|
</td></tr>
|
|
<tr><td>167</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>168</td>
|
|
<td class="normal"> private function validateLength($column, $key, $value)
|
|
</td></tr>
|
|
<tr><td>169</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>170</td>
|
|
<td class="covered"> if ($column['type'] == 'timestamp' || $column['type'] == 'integer' ||
|
|
</td></tr>
|
|
<tr><td>171</td>
|
|
<td class="covered"> $column['type'] == 'enum') {
|
|
</td></tr>
|
|
<tr><td>172</td>
|
|
<td class="covered"> return true;
|
|
</td></tr>
|
|
<tr><td>173</td>
|
|
<td class="covered"> } else if ($column['type'] == 'array' || $column['type'] == 'object') {
|
|
</td></tr>
|
|
<tr><td>174</td>
|
|
<td class="covered"> $length = strlen(serialize($value));
|
|
</td></tr>
|
|
<tr><td>175</td>
|
|
<td class="covered"> } else {
|
|
</td></tr>
|
|
<tr><td>176</td>
|
|
<td class="covered"> $length = strlen($value);
|
|
</td></tr>
|
|
<tr><td>177</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>178</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>179</td>
|
|
<td class="covered"> if ($length > $column['length']) {
|
|
</td></tr>
|
|
<tr><td>180</td>
|
|
<td class="covered"> return false;
|
|
</td></tr>
|
|
<tr><td>181</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>182</td>
|
|
<td class="covered"> return true;
|
|
</td></tr>
|
|
<tr><td>183</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>184</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>185</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>186</td>
|
|
<td class="normal"> * whether or not this validator has errors
|
|
</td></tr>
|
|
<tr><td>187</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>188</td>
|
|
<td class="normal"> * @return boolean
|
|
</td></tr>
|
|
<tr><td>189</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>190</td>
|
|
<td class="normal"> public function hasErrors()
|
|
</td></tr>
|
|
<tr><td>191</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>192</td>
|
|
<td class="red"> return (count($this->stack) > 0);
|
|
</td></tr>
|
|
<tr><td>193</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>194</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>195</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>196</td>
|
|
<td class="normal"> * phpType
|
|
</td></tr>
|
|
<tr><td>197</td>
|
|
<td class="normal"> * converts a doctrine type to native php type
|
|
</td></tr>
|
|
<tr><td>198</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>199</td>
|
|
<td class="normal"> * @param $portableType portable doctrine type
|
|
</td></tr>
|
|
<tr><td>200</td>
|
|
<td class="normal"> * @return string
|
|
</td></tr>
|
|
<tr><td>201</td>
|
|
<td class="normal"> *//*
|
|
</td></tr>
|
|
<tr><td>202</td>
|
|
<td class="normal"> public static function phpType($portableType)
|
|
</td></tr>
|
|
<tr><td>203</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>204</td>
|
|
<td class="normal"> switch ($portableType) {
|
|
</td></tr>
|
|
<tr><td>205</td>
|
|
<td class="normal"> case 'enum':
|
|
</td></tr>
|
|
<tr><td>206</td>
|
|
<td class="normal"> return 'integer';
|
|
</td></tr>
|
|
<tr><td>207</td>
|
|
<td class="normal"> case 'blob':
|
|
</td></tr>
|
|
<tr><td>208</td>
|
|
<td class="normal"> case 'clob':
|
|
</td></tr>
|
|
<tr><td>209</td>
|
|
<td class="normal"> case 'mbstring':
|
|
</td></tr>
|
|
<tr><td>210</td>
|
|
<td class="normal"> case 'timestamp':
|
|
</td></tr>
|
|
<tr><td>211</td>
|
|
<td class="normal"> case 'date':
|
|
</td></tr>
|
|
<tr><td>212</td>
|
|
<td class="normal"> case 'gzip':
|
|
</td></tr>
|
|
<tr><td>213</td>
|
|
<td class="normal"> return 'string';
|
|
</td></tr>
|
|
<tr><td>214</td>
|
|
<td class="normal"> break;
|
|
</td></tr>
|
|
<tr><td>215</td>
|
|
<td class="normal"> default:
|
|
</td></tr>
|
|
<tr><td>216</td>
|
|
<td class="normal"> return $portableType;
|
|
</td></tr>
|
|
<tr><td>217</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>218</td>
|
|
<td class="normal"> }*/
|
|
</td></tr>
|
|
<tr><td>219</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>220</td>
|
|
<td class="normal"> * returns whether or not the given variable is
|
|
</td></tr>
|
|
<tr><td>221</td>
|
|
<td class="normal"> * valid type
|
|
</td></tr>
|
|
<tr><td>222</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>223</td>
|
|
<td class="normal"> * @param mixed $var
|
|
</td></tr>
|
|
<tr><td>224</td>
|
|
<td class="normal"> * @param string $type
|
|
</td></tr>
|
|
<tr><td>225</td>
|
|
<td class="normal"> * @return boolean
|
|
</td></tr>
|
|
<tr><td>226</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>227</td>
|
|
<td class="normal"> /*
|
|
</td></tr>
|
|
<tr><td>228</td>
|
|
<td class="normal"> public static function isValidType($var, $type)
|
|
</td></tr>
|
|
<tr><td>229</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>230</td>
|
|
<td class="normal"> if ($type == 'boolean') {
|
|
</td></tr>
|
|
<tr><td>231</td>
|
|
<td class="normal"> return true;
|
|
</td></tr>
|
|
<tr><td>232</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>233</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>234</td>
|
|
<td class="normal"> $looseType = self::gettype($var);
|
|
</td></tr>
|
|
<tr><td>235</td>
|
|
<td class="normal"> $type = self::phpType($type);
|
|
</td></tr>
|
|
<tr><td>236</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>237</td>
|
|
<td class="normal"> switch ($looseType) {
|
|
</td></tr>
|
|
<tr><td>238</td>
|
|
<td class="normal"> case 'float':
|
|
</td></tr>
|
|
<tr><td>239</td>
|
|
<td class="normal"> case 'double':
|
|
</td></tr>
|
|
<tr><td>240</td>
|
|
<td class="normal"> case 'integer':
|
|
</td></tr>
|
|
<tr><td>241</td>
|
|
<td class="normal"> if ($type == 'string' || $type == 'float') {
|
|
</td></tr>
|
|
<tr><td>242</td>
|
|
<td class="normal"> return true;
|
|
</td></tr>
|
|
<tr><td>243</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>244</td>
|
|
<td class="normal"> case 'string':
|
|
</td></tr>
|
|
<tr><td>245</td>
|
|
<td class="normal"> case 'array':
|
|
</td></tr>
|
|
<tr><td>246</td>
|
|
<td class="normal"> case 'object':
|
|
</td></tr>
|
|
<tr><td>247</td>
|
|
<td class="normal"> return ($type === $looseType);
|
|
</td></tr>
|
|
<tr><td>248</td>
|
|
<td class="normal"> break;
|
|
</td></tr>
|
|
<tr><td>249</td>
|
|
<td class="normal"> case 'NULL':
|
|
</td></tr>
|
|
<tr><td>250</td>
|
|
<td class="normal"> return true;
|
|
</td></tr>
|
|
<tr><td>251</td>
|
|
<td class="normal"> break;
|
|
</td></tr>
|
|
<tr><td>252</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>253</td>
|
|
<td class="normal"> }*/
|
|
</td></tr>
|
|
<tr><td>254</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>255</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>256</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>257</td>
|
|
<td class="normal"> * returns whether or not the given variable is
|
|
</td></tr>
|
|
<tr><td>258</td>
|
|
<td class="normal"> * valid type
|
|
</td></tr>
|
|
<tr><td>259</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>260</td>
|
|
<td class="normal"> * @param mixed $var
|
|
</td></tr>
|
|
<tr><td>261</td>
|
|
<td class="normal"> * @param string $type
|
|
</td></tr>
|
|
<tr><td>262</td>
|
|
<td class="normal"> * @return boolean
|
|
</td></tr>
|
|
<tr><td>263</td>
|
|
<td class="normal"> */
|
|
</td></tr>
|
|
<tr><td>264</td>
|
|
<td class="normal"> public static function isValidType($var, $type)
|
|
</td></tr>
|
|
<tr><td>265</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>266</td>
|
|
<td class="covered"> if ($var === null) {
|
|
</td></tr>
|
|
<tr><td>267</td>
|
|
<td class="covered"> return true;
|
|
</td></tr>
|
|
<tr><td>268</td>
|
|
<td class="covered"> } else if (is_object($var)) {
|
|
</td></tr>
|
|
<tr><td>269</td>
|
|
<td class="covered"> return $type == 'object';
|
|
</td></tr>
|
|
<tr><td>270</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>271</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>272</td>
|
|
<td class="normal"> switch ($type) {
|
|
</td></tr>
|
|
<tr><td>273</td>
|
|
<td class="covered"> case 'float':
|
|
</td></tr>
|
|
<tr><td>274</td>
|
|
<td class="covered"> case 'double':
|
|
</td></tr>
|
|
<tr><td>275</td>
|
|
<td class="covered"> return (String)$var == strval(floatval($var));
|
|
</td></tr>
|
|
<tr><td>276</td>
|
|
<td class="covered"> case 'integer':
|
|
</td></tr>
|
|
<tr><td>277</td>
|
|
<td class="covered"> return (String)$var == strval(intval($var));
|
|
</td></tr>
|
|
<tr><td>278</td>
|
|
<td class="covered"> case 'string':
|
|
</td></tr>
|
|
<tr><td>279</td>
|
|
<td class="covered"> return is_string($var) || is_int($var) || is_float($var);
|
|
</td></tr>
|
|
<tr><td>280</td>
|
|
<td class="covered"> case 'blob':
|
|
</td></tr>
|
|
<tr><td>281</td>
|
|
<td class="covered"> case 'clob':
|
|
</td></tr>
|
|
<tr><td>282</td>
|
|
<td class="covered"> case 'gzip':
|
|
</td></tr>
|
|
<tr><td>283</td>
|
|
<td class="red"> return is_string($var);
|
|
</td></tr>
|
|
<tr><td>284</td>
|
|
<td class="covered"> case 'array':
|
|
</td></tr>
|
|
<tr><td>285</td>
|
|
<td class="covered"> return is_array($var);
|
|
</td></tr>
|
|
<tr><td>286</td>
|
|
<td class="covered"> case 'object':
|
|
</td></tr>
|
|
<tr><td>287</td>
|
|
<td class="covered"> return is_object($var);
|
|
</td></tr>
|
|
<tr><td>288</td>
|
|
<td class="covered"> case 'boolean':
|
|
</td></tr>
|
|
<tr><td>289</td>
|
|
<td class="red"> return is_bool($var);
|
|
</td></tr>
|
|
<tr><td>290</td>
|
|
<td class="covered"> case 'timestamp':
|
|
</td></tr>
|
|
<tr><td>291</td>
|
|
<td class="normal"> // todo: validate the timestamp is in YYYY-MM-DD HH:MM:SS format
|
|
</td></tr>
|
|
<tr><td>292</td>
|
|
<td class="red"> return true;
|
|
</td></tr>
|
|
<tr><td>293</td>
|
|
<td class="covered"> case 'date':
|
|
</td></tr>
|
|
<tr><td>294</td>
|
|
<td class="covered"> $validator = self::getValidator('date');
|
|
</td></tr>
|
|
<tr><td>295</td>
|
|
<td class="covered"> return $validator->validate($var);
|
|
</td></tr>
|
|
<tr><td>296</td>
|
|
<td class="red"> case 'enum':
|
|
</td></tr>
|
|
<tr><td>297</td>
|
|
<td class="red"> return is_string($var) || is_int($var);
|
|
</td></tr>
|
|
<tr><td>298</td>
|
|
<td class="red"> default:
|
|
</td></tr>
|
|
<tr><td>299</td>
|
|
<td class="red"> return false;
|
|
</td></tr>
|
|
<tr><td>300</td>
|
|
<td class="red"> }
|
|
</td></tr>
|
|
<tr><td>301</td>
|
|
<td class="orange"> }
|
|
</td></tr>
|
|
<tr><td>302</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>303</td>
|
|
<td class="normal">
|
|
</td></tr>
|
|
<tr><td>304</td>
|
|
<td class="normal"> /**
|
|
</td></tr>
|
|
<tr><td>305</td>
|
|
<td class="normal"> * returns the type of loosely typed variable
|
|
</td></tr>
|
|
<tr><td>306</td>
|
|
<td class="normal"> *
|
|
</td></tr>
|
|
<tr><td>307</td>
|
|
<td class="normal"> * @param mixed $var
|
|
</td></tr>
|
|
<tr><td>308</td>
|
|
<td class="normal"> * @return string
|
|
</td></tr>
|
|
<tr><td>309</td>
|
|
<td class="normal"> *//*
|
|
</td></tr>
|
|
<tr><td>310</td>
|
|
<td class="normal"> public static function gettype($var)
|
|
</td></tr>
|
|
<tr><td>311</td>
|
|
<td class="normal"> {
|
|
</td></tr>
|
|
<tr><td>312</td>
|
|
<td class="normal"> $type = gettype($var);
|
|
</td></tr>
|
|
<tr><td>313</td>
|
|
<td class="normal"> switch ($type) {
|
|
</td></tr>
|
|
<tr><td>314</td>
|
|
<td class="normal"> case 'string':
|
|
</td></tr>
|
|
<tr><td>315</td>
|
|
<td class="normal"> if (preg_match("/^[0-9]+$/",$var)) {
|
|
</td></tr>
|
|
<tr><td>316</td>
|
|
<td class="normal"> return 'integer';
|
|
</td></tr>
|
|
<tr><td>317</td>
|
|
<td class="normal"> } elseif (is_numeric($var)) {
|
|
</td></tr>
|
|
<tr><td>318</td>
|
|
<td class="normal"> return 'float';
|
|
</td></tr>
|
|
<tr><td>319</td>
|
|
<td class="normal"> } else {
|
|
</td></tr>
|
|
<tr><td>320</td>
|
|
<td class="normal"> return $type;
|
|
</td></tr>
|
|
<tr><td>321</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>322</td>
|
|
<td class="normal"> break;
|
|
</td></tr>
|
|
<tr><td>323</td>
|
|
<td class="normal"> default:
|
|
</td></tr>
|
|
<tr><td>324</td>
|
|
<td class="normal"> return $type;
|
|
</td></tr>
|
|
<tr><td>325</td>
|
|
<td class="normal"> }
|
|
</td></tr>
|
|
<tr><td>326</td>
|
|
<td class="normal"> }*/
|
|
</td></tr>
|
|
<tr><td>327</td>
|
|
<td class="normal">}
|
|
</td></tr>
|
|
</table></body></html> |