3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * This file is part of the Nette Framework (http://nette.org) * Copyright (c) 2004 David Grudl (http://davidgrudl.com) */ namespace Nette; use Nette; /** * The Nette Framework (http://nette.org) * * @author David Grudl */ class Framework { /** Nette Framework version identification */ const NAME = 'Nette Framework', VERSION = '2.3-dev', VERSION_ID = 20300, REVISION = '$WCREV$ released on $WCDATE$'; /** * Static class - cannot be instantiated. */ final public function __construct() { throw new StaticClassException; } } /** * This file is part of the Nette Framework (http://nette.org) * Copyright (c) 2004 David Grudl (http://davidgrudl.com) */ namespace Nette\Forms; use Nette; /** * Creates, validates and renders HTML forms. * * @author David Grudl * * @property mixed $action * @property string $method * @property-read array $groups * @property Nette\Localization\ITranslator|NULL $translator * @property-read bool $anchored * @property-read ISubmitterControl|FALSE $submitted * @property-read bool $success * @property-read array $httpData * @property-read array $errors * @property-read Nette\Utils\Html $elementPrototype * @property IFormRenderer $renderer */ class Form extends Container implements Nette\Utils\IHtmlString { /** validator */ const EQUAL = ':equal', IS_IN = self::EQUAL, NOT_EQUAL = ':notEqual', FILLED = ':filled', BLANK = ':blank', REQUIRED = self::FILLED, VALID = ':valid'; /** @deprecated CSRF protection */ const PROTECTION = Controls\CsrfProtection::PROTECTION; // button const SUBMITTED = ':submitted'; // text const MIN_LENGTH = ':minLength', MAX_LENGTH = ':maxLength', LENGTH = ':length', EMAIL = ':email', URL = ':url', PATTERN = ':pattern', INTEGER = ':integer', NUMERIC = ':integer', FLOAT = ':float', MIN = ':min', MAX = ':max', RANGE = ':range'; // multiselect const COUNT = self::LENGTH; // file upload const MAX_FILE_SIZE = ':fileSize', MIME_TYPE = ':mimeType', IMAGE = ':image', MAX_POST_SIZE = ':maxPostSize'; /** method */ const GET = 'get', POST = 'post'; /** submitted data types */ const DATA_TEXT = 1; const DATA_LINE = 2; const DATA_FILE = 3; const DATA_KEYS = 8; /** @internal tracker ID */ const TRACKER_ID = '_form_'; /** @internal protection token ID */ const PROTECTOR_ID = '_token_'; /** @var array of function(Form $sender); Occurs when the form is submitted and successfully validated */ public $onSuccess; /** @var array of function(Form $sender); Occurs when the form is submitted and is not valid */ public $onError; /** @var array of function(Form $sender); Occurs when the form is submitted */ public $onSubmit; /** @var mixed or NULL meaning: not detected yet */ private $submittedBy; /** @var array */ private $httpData; /** @var Nette\Utils\Html <form> element */ private $element; /** @var IFormRenderer */ private $renderer; /** @var Nette\Localization\ITranslator */ private $translator; /** @var ControlGroup[] */ private $groups = array(); /** @var array */ private $errors = array(); /** @var Nette\Http\IRequest used only by standalone form */ public $httpRequest; /** * Form constructor. * @param string */ public function __construct($name = NULL) { if ($name !== NULL) { $this->getElementPrototype()->id = 'frm-' . $name; $tracker = new Controls\HiddenField($name); $tracker->setOmitted(); $this[self::TRACKER_ID] = $tracker; } parent::__construct(NULL, $name); } /** * @return void */ protected function validateParent(Nette\ComponentModel\IContainer $parent) { parent::validateParent($parent); $this->monitor(__CLASS__); } /** * This method will be called when the component (or component's parent) * becomes attached to a monitored object. Do not call this method yourself. * @param Nette\ComponentModel\IComponent * @return void */ protected function attached($obj) { if ($obj instanceof self) { throw new Nette\InvalidStateException('Nested forms are forbidden.'); } } /** * Returns self. * @return Form */ public function getForm($need = TRUE) { return $this; } /** * Sets form's action. * @param mixed URI * @return self */ public function setAction($url) { $this->getElementPrototype()->action = $url; return $this; } /** * Returns form's action. * @return mixed URI */ public function getAction() { return $this->getElementPrototype()->action; } /** * Sets form's method. * @param string get | post * @return self */ public function setMethod($method) { if ($this->httpData !== NULL) { throw new Nette\InvalidStateException(__METHOD__ . '() must be called until the form is empty.'); } $this->getElementPrototype()->method = strtolower($method); return $this; } /** * Returns form's method. * @return string get | post */ public function getMethod() { return $this->getElementPrototype()->method; } /** * Cross-Site Request Forgery (CSRF) form protection. * @param string * @return Controls\CsrfProtection */ public function addProtection($message = NULL) { return $this[self::PROTECTOR_ID] = new Controls\CsrfProtection($message); } /** * Adds fieldset group to the form. * @param string caption * @param bool set this group as current * @return ControlGroup */ public function addGroup($caption = NULL, $setAsCurrent = TRUE) { $group = new ControlGroup; $group->setOption('label', $caption); $group->setOption('visual', TRUE); if ($setAsCurrent) { $this->setCurrentGroup($group); } if (!is_scalar($caption) || isset($this->groups[$caption])) { return $this->groups[] = $group; } else { return $this->groups[$caption] = $group; } } /** * Removes fieldset group from form. * @param string|ControlGroup * @return void */ public function removeGroup($name) { if (is_string($name) && isset($this->groups[$name])) { $group = $this->groups[$name]; } elseif ($name instanceof ControlGroup && in_array($name, $this->groups, TRUE)) { $group = $name; $name = array_search($group, $this->groups, TRUE); } else { throw new Nette\InvalidArgumentException("Group not found in form '$this->name'"); } foreach ($group->getControls() as $control) { $control->getParent()->removeComponent($control); } unset($this->groups[$name]); } /** * Returns all defined groups. * @return ControlGroup[] */ public function getGroups() { return $this->groups; } /** * Returns the specified group. * @param string name * @return ControlGroup */ public function getGroup($name) { return isset($this->groups[$name]) ? $this->groups[$name] : NULL; } /********************* translator ****************d*g**/ /** * Sets translate adapter. * @return self */ public function setTranslator(Nette\Localization\ITranslator $translator = NULL) { $this->translator = $translator; return $this; } /** * Returns translate adapter. * @return Nette\Localization\ITranslator|NULL */ public function getTranslator() { return $this->translator; } /********************* submission ****************d*g**/ /** * Tells if the form is anchored. * @return bool */ public function isAnchored() { return TRUE; } /** * Tells if the form was submitted. * @return ISubmitterControl|FALSE submittor control */ public function isSubmitted() { if ($this->submittedBy === NULL) { $this->getHttpData(); } return $this->submittedBy; } /** * Tells if the form was submitted and successfully validated. * @return bool */ public function isSuccess() { return $this->isSubmitted() && $this->isValid(); } /** * Sets the submittor control. * @return self */ public function setSubmittedBy(ISubmitterControl $by = NULL) { $this->submittedBy = $by === NULL ? FALSE : $by; return $this; } /** * Returns submitted HTTP data. * @return mixed */ public function getHttpData($type = NULL, $htmlName = NULL) { if ($this->httpData === NULL) { if (!$this->isAnchored()) { throw new Nette\InvalidStateException('Form is not anchored and therefore can not determine whether it was submitted.'); } $data = $this->receiveHttpData(); $this->httpData = (array) $data; $this->submittedBy = is_array($data); } if ($htmlName === NULL) { return $this->httpData; } return Helpers::extractHttpData($this->httpData, $htmlName, $type); } /** * Fires submit/click events. * @return void */ public function fireEvents() { if (!$this->isSubmitted()) { return; } $this->validate(); if ($this->submittedBy instanceof ISubmitterControl) { if ($this->isValid()) { $this->submittedBy->onClick($this->submittedBy); } else { $this->submittedBy->onInvalidClick($this->submittedBy); } } if ($this->onSuccess) { foreach ($this->onSuccess as $handler) { if (!$this->isValid()) { $this->onError($this); break; } $params = Nette\Utils\Callback::toReflection($handler)->getParameters(); $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL; Nette\Utils\Callback::invoke($handler, $this, $values); } } elseif (!$this->isValid()) { $this->onError($this); } $this->onSubmit($this); } /** * Internal: returns submitted HTTP data or NULL when form was not submitted. * @return array|NULL */ protected function receiveHttpData() { $httpRequest = $this->getHttpRequest(); if (strcasecmp($this->getMethod(), $httpRequest->getMethod())) { return; } if ($httpRequest->isMethod('post')) { $data = Nette\Utils\Arrays::mergeTree($httpRequest->getPost(), $httpRequest->getFiles()); } else { $data = $httpRequest->getQuery(); if (!$data) { return; } } if ($tracker = $this->getComponent(self::TRACKER_ID, FALSE)) { if (!isset($data[self::TRACKER_ID]) || $data[self::TRACKER_ID] !== $tracker->getValue()) { return; } } return $data; } /********************* validation ****************d*g**/ public function validate(array $controls = NULL) { $this->cleanErrors(); if ($controls === NULL && $this->submittedBy instanceof ISubmitterControl) { $controls = $this->submittedBy->getValidationScope(); } $this->validateMaxPostSize(); parent::validate($controls); } public function validateMaxPostSize() { if (!$this->submittedBy || strcasecmp($this->getMethod(), 'POST') || empty($_SERVER['CONTENT_LENGTH'])) { return; } $maxSize = ini_get('post_max_size'); $units = array('k' => 10, 'm' => 20, 'g' => 30); if (isset($units[$ch = strtolower(substr($maxSize, -1))])) { $maxSize <<= $units[$ch]; } if ($maxSize > 0 && $maxSize < $_SERVER['CONTENT_LENGTH']) { $this->addError(sprintf(Validator::$messages[self::MAX_FILE_SIZE], $maxSize)); } } /** * Adds global error message. * @param string error message * @return void */ public function addError($message) { $this->errors[] = $message; } /** * Returns global validation errors. * @return array */ public function getErrors() { return array_unique(array_merge($this->errors, parent::getErrors())); } /** * @return bool */ public function hasErrors() { return (bool) $this->getErrors(); } /** * @return void */ public function cleanErrors() { $this->errors = array(); } /** * Returns form's validation errors. * @return array */ public function getOwnErrors() { return array_unique($this->errors); } /** @deprecated */ public function getAllErrors() { trigger_error(__METHOD__ . '() is deprecated; use getErrors() instead.', E_USER_DEPRECATED); return $this->getErrors(); } /********************* rendering ****************d*g**/ /** * Returns form's HTML element template. * @return Nette\Utils\Html */ public function getElementPrototype() { if (!$this->element) { $this->element = Nette\Utils\Html::el('form'); $this->element->action = ''; // RFC 1808 -> empty uri means 'this' $this->element->method = self::POST; } return $this->element; } /** * Sets form renderer. * @return self */ public function setRenderer(IFormRenderer $renderer = NULL) { $this->renderer = $renderer; return $this; } /** * Returns form renderer. * @return IFormRenderer */ public function getRenderer() { if ($this->renderer === NULL) { $this->renderer = new Rendering\DefaultFormRenderer; } return $this->renderer; } /** * Renders form. * @return void */ public function render() { $args = func_get_args(); array_unshift($args, $this); echo call_user_func_array(array($this->getRenderer(), 'render'), $args); } /** * Renders form to string. * @return can throw exceptions? (hidden parameter) * @return string */ public function __toString() { try { return $this->getRenderer()->render($this); } catch (\Exception $e) { if (func_num_args()) { throw $e; } trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR); } } /********************* backend ****************d*g**/ /** * @return Nette\Http\IRequest */ private function getHttpRequest() { if (!$this->httpRequest) { $factory = new Nette\Http\RequestFactory; $this->httpRequest = $factory->createHttpRequest(); } return $this->httpRequest; } /** * @return array */ public function getToggles() { $toggles = array(); foreach ($this->getControls() as $control) { $toggles = $control->getRules()->getToggleStates($toggles); } return $toggles; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0090.00616.88
8.3.60.0070.01716.88
8.3.50.0070.00821.95
8.3.40.0030.01619.01
8.3.30.0120.00919.30
8.3.20.0050.00320.26
8.3.10.0060.00323.65
8.3.00.0040.00419.51
8.2.180.0110.00718.16
8.2.170.0030.01222.96
8.2.160.0000.01420.47
8.2.150.0050.00324.18
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0030.00619.43
8.2.110.0030.00719.39
8.2.100.0030.00918.03
8.2.90.0000.00919.21
8.2.80.0040.00418.15
8.2.70.0090.00017.63
8.2.60.0000.00917.93
8.2.50.0030.00618.07
8.2.40.0040.00419.88
8.2.30.0000.00818.45
8.2.20.0060.00317.96
8.2.10.0000.00818.25
8.2.00.0040.00417.82
8.1.280.0090.00625.92
8.1.270.0060.00923.76
8.1.260.0060.00326.35
8.1.250.0000.00828.09
8.1.240.0000.01022.64
8.1.230.0070.00417.64
8.1.220.0000.00817.91
8.1.210.0050.00518.77
8.1.200.0030.00617.22
8.1.190.0030.00617.38
8.1.180.0050.00518.10
8.1.170.0030.00618.68
8.1.160.0070.00022.15
8.1.150.0030.00618.59
8.1.140.0000.00817.53
8.1.130.0040.00417.79
8.1.120.0000.00817.39
8.1.110.0080.00017.54
8.1.100.0000.00717.57
8.1.90.0040.00417.49
8.1.80.0060.00317.53
8.1.70.0000.00817.48
8.1.60.0060.00317.59
8.1.50.0000.00817.60
8.1.40.0050.00317.61
8.1.30.0050.00817.68
8.1.20.0050.00317.67
8.1.10.0030.00617.69
8.1.00.0040.00417.48
8.0.300.0040.00420.73
8.0.290.0000.00817.41
8.0.280.0000.00718.59
8.0.270.0040.00417.28
8.0.260.0070.00017.40
8.0.250.0040.00417.18
8.0.240.0030.00617.21
8.0.230.0000.00817.06
8.0.220.0000.00817.07
8.0.210.0000.00717.10
8.0.200.0040.00417.09
8.0.190.0060.00317.18
8.0.180.0060.00317.12
8.0.170.0040.00417.17
8.0.160.0000.00817.11
8.0.150.0040.00416.98
8.0.140.0000.00717.04
8.0.130.0060.00013.54
8.0.120.0030.00617.13
8.0.110.0000.00817.05
8.0.100.0000.00817.05
8.0.90.0060.00316.95
8.0.80.0130.00317.09
8.0.70.0000.00916.95
8.0.60.0000.00917.12
8.0.50.0040.00417.06
8.0.30.0120.00817.33
8.0.20.0170.00617.40
8.0.10.0040.00417.00
8.0.00.0110.00916.82
7.4.330.0000.00615.08
7.4.320.0000.00716.62
7.4.300.0000.00816.75
7.4.290.0050.00316.70
7.4.280.0040.00416.71
7.4.270.0040.00416.63
7.4.260.0040.00416.61
7.4.250.0030.00516.50
7.4.240.0040.00416.64
7.4.230.0000.00816.66
7.4.220.0060.01316.69
7.4.210.0070.01316.76
7.4.200.0040.00416.50
7.4.190.0000.00816.52
7.4.160.0120.00616.76
7.4.150.0070.01117.40
7.4.140.0110.01317.86
7.4.130.0100.01016.73
7.4.120.0130.00716.68
7.4.110.0070.01016.73
7.4.100.0090.01616.61
7.4.90.0150.00316.87
7.4.80.0090.01619.39
7.4.70.0080.01116.54
7.4.60.0120.00616.52
7.4.50.0000.00616.59
7.4.40.0030.01416.64
7.4.30.0090.00816.64
7.4.00.0120.00714.96
7.3.330.0070.00013.20
7.3.320.0000.00613.51
7.3.310.0000.00816.50
7.3.300.0030.00316.41
7.3.290.0040.01116.39
7.3.280.0110.00816.43
7.3.270.0070.01317.40
7.3.260.0140.00316.59
7.3.250.0100.01116.60
7.3.240.0110.00716.67
7.3.230.0110.00616.53
7.3.210.0150.00916.56
7.3.200.0030.01519.39
7.3.190.0080.00816.61
7.3.180.0070.01316.70
7.3.170.0110.00716.65
7.3.160.0070.01016.55
7.3.120.0060.01114.86
7.3.110.0130.00314.79
7.3.100.0110.00715.00
7.3.90.0120.00314.86
7.3.80.0110.00315.01
7.3.70.0070.00314.97
7.3.60.0080.00814.95
7.3.50.0070.00414.88
7.3.40.0090.00614.82
7.3.30.0090.00614.98
7.3.20.0060.00916.51
7.3.10.0100.00716.34
7.3.00.0090.00316.57
7.2.330.0090.00916.65
7.2.320.0160.01116.51
7.2.310.0090.00916.56
7.2.300.0100.00716.84
7.2.290.0120.00616.67
7.2.250.0030.01615.02
7.2.240.0110.01114.97
7.2.230.0040.01514.63
7.2.220.0090.00315.04
7.2.210.0090.00615.22
7.2.200.0130.00015.11
7.2.190.0040.01214.82
7.2.180.0100.00715.08
7.2.170.0060.00914.87
7.2.00.0040.00819.25
7.1.330.0080.00815.70
7.1.320.0070.00715.70
7.1.310.0030.01015.73
7.1.300.0070.00715.72
7.1.290.0090.00015.86
7.1.280.0120.00315.53
7.1.270.0070.00715.58
7.1.260.0090.00615.75
7.1.100.0030.01017.98
7.1.70.0030.00616.95
7.1.60.0100.01019.46
7.1.50.0100.01416.68
7.1.00.0030.07722.55
7.0.200.0230.00716.80
7.0.140.0130.06722.17
7.0.100.0370.07321.67
7.0.90.0070.08021.71
7.0.80.0200.08020.05
7.0.70.0130.07020.10
7.0.60.0130.04720.05
7.0.50.0130.07722.20
7.0.40.0200.06720.10
7.0.30.0000.04720.04
7.0.20.0000.04720.11
7.0.10.0070.03720.14
7.0.00.0030.04320.09
5.6.280.0030.07021.03
5.6.250.0030.05320.67
5.6.240.0100.07320.71
5.6.230.0070.07720.78
5.6.220.0000.05320.69
5.6.210.0070.07720.75
5.6.200.0100.05721.10
5.6.190.0070.05021.20
5.6.180.0030.05021.14
5.6.170.0100.03721.11
5.6.160.0100.03721.11
5.6.150.0000.04321.05
5.6.140.0030.04021.20
5.6.130.0100.03721.24
5.6.120.0030.09321.13
5.6.110.0130.08021.21
5.6.100.0030.07021.14
5.6.90.0000.04721.05
5.6.80.0000.04320.47
5.6.70.0000.04320.52
5.6.60.0070.03720.62
5.6.50.0030.04020.44
5.6.40.0100.03320.64
5.6.30.0000.04020.57
5.6.20.0130.03720.64
5.6.10.0000.04320.50
5.6.00.0000.04720.62
5.5.380.0130.07720.57
5.5.370.0070.07020.49
5.5.360.0100.07720.54
5.5.350.0200.03720.52
5.5.340.0100.08320.99
5.5.330.0030.08021.00
5.5.320.0030.04021.03
5.5.310.0070.06021.04
5.5.300.0000.05020.88
5.5.290.0030.04321.01
5.5.280.0030.05320.95
5.5.270.0100.03721.03
5.5.260.0100.03721.04
5.5.250.0070.08020.79
5.5.240.0070.04020.44
5.5.230.0030.04020.41
5.5.220.0030.05020.38
5.5.210.0070.03720.30
5.5.200.0070.03720.19
5.5.190.0000.04720.11
5.5.180.0030.05020.38
5.5.160.0070.03720.33
5.5.150.0100.03320.26
5.5.140.0070.03720.37
5.5.130.0070.04320.23
5.5.120.0070.03720.29
5.5.110.0070.03720.36
5.5.100.0000.04320.17
5.5.90.0100.03320.06
5.5.80.0030.04020.21
5.5.70.0000.04320.27
5.5.60.0030.03320.23
5.5.50.0000.04020.31
5.5.40.0100.04020.23
5.5.30.0070.04320.07
5.5.20.0070.03020.21
5.5.10.0070.03720.22
5.5.00.0000.05720.09
5.4.450.0000.04319.47
5.4.440.0070.08019.51
5.4.430.0070.07319.42
5.4.420.0070.06719.40
5.4.410.0100.03019.27
5.4.400.0070.03318.95
5.4.390.0000.04019.16
5.4.380.0100.03019.10
5.4.370.0100.03019.13
5.4.360.0030.04019.09
5.4.350.0030.04019.14
5.4.340.0070.03718.89
5.4.320.0070.03719.27
5.4.310.0030.04019.13
5.4.300.0100.04019.00
5.4.290.0000.04019.06
5.4.280.0070.03719.15
5.4.270.0030.03719.12
5.4.260.0070.03719.00
5.4.250.0100.03319.22
5.4.240.0070.02718.93
5.4.230.0030.03019.16
5.4.220.0030.04019.23
5.4.210.0000.04318.90
5.4.200.0000.03718.99
5.4.190.0030.04019.07
5.4.180.0000.05318.98
5.4.170.0100.07319.13
5.4.160.0000.07018.88
5.4.150.0000.05319.24
5.4.140.0030.06716.48
5.4.130.0070.03316.43
5.4.120.0030.06316.43
5.4.110.0030.07716.55
5.4.100.0070.04716.57
5.4.90.0170.06316.48
5.4.80.0100.05716.46
5.4.70.0030.03316.45
5.4.60.0000.06316.52
5.4.50.0170.03316.42
5.4.40.0130.07016.53
5.4.30.0070.05716.53
5.4.20.0200.06016.53
5.4.10.0070.03716.39
5.4.00.0030.04315.73
5.3.290.0070.03714.85
5.3.280.0030.03014.79
5.3.270.0070.07714.66
5.3.260.0070.07314.77
5.3.250.0000.04314.71
5.3.240.0030.07014.77
5.3.230.0130.07014.72
5.3.220.0070.06714.73
5.3.210.0030.06014.84
5.3.200.0030.05314.68
5.3.190.0030.04714.67
5.3.180.0070.07714.70
5.3.170.0000.04014.70
5.3.160.0070.03714.67
5.3.150.0100.07014.70
5.3.140.0130.06714.69
5.3.130.0130.06714.75
5.3.120.0070.03714.64
5.3.110.0030.04014.80
5.3.100.0070.06014.30
5.3.90.0070.06314.08
5.3.80.0070.07314.20
5.3.70.0070.06014.18
5.3.60.0030.07314.27
5.3.50.0030.06714.10
5.3.40.0070.05714.19
5.3.30.0030.08014.10
5.3.20.0070.07013.82
5.3.10.0100.07313.88
5.3.00.0130.06313.77

preferences:
32.94 ms | 401 KiB | 5 Q