Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DomCrawler\Field; Chris@0: Chris@0: /** Chris@0: * FileFormField represents a file form field (an HTML file input tag). Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class FileFormField extends FormField Chris@0: { Chris@0: /** Chris@0: * Sets the PHP error code associated with the field. Chris@0: * Chris@0: * @param int $error The error code (one of UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, or UPLOAD_ERR_EXTENSION) Chris@0: * Chris@0: * @throws \InvalidArgumentException When error code doesn't exist Chris@0: */ Chris@0: public function setErrorCode($error) Chris@0: { Chris@17: $codes = [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION]; Chris@17: if (!\in_array($error, $codes)) { Chris@0: throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error)); Chris@0: } Chris@0: Chris@17: $this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the field. Chris@0: * Chris@0: * @param string $value The value of the field Chris@0: */ Chris@0: public function upload($value) Chris@0: { Chris@0: $this->setValue($value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the field. Chris@0: * Chris@0: * @param string $value The value of the field Chris@0: */ Chris@0: public function setValue($value) Chris@0: { Chris@0: if (null !== $value && is_readable($value)) { Chris@0: $error = UPLOAD_ERR_OK; Chris@0: $size = filesize($value); Chris@0: $info = pathinfo($value); Chris@0: $name = $info['basename']; Chris@0: Chris@0: // copy to a tmp location Chris@12: $tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_'); Chris@18: if (\array_key_exists('extension', $info)) { Chris@0: $tmp .= '.'.$info['extension']; Chris@0: } Chris@0: if (is_file($tmp)) { Chris@0: unlink($tmp); Chris@0: } Chris@0: copy($value, $tmp); Chris@0: $value = $tmp; Chris@0: } else { Chris@0: $error = UPLOAD_ERR_NO_FILE; Chris@0: $size = 0; Chris@0: $name = ''; Chris@0: $value = ''; Chris@0: } Chris@0: Chris@17: $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets path to the file as string for simulating HTTP request. Chris@0: * Chris@0: * @param string $path The path to the file Chris@0: */ Chris@0: public function setFilePath($path) Chris@0: { Chris@0: parent::setValue($path); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the form field. Chris@0: * Chris@0: * @throws \LogicException When node type is incorrect Chris@0: */ Chris@0: protected function initialize() Chris@0: { Chris@0: if ('input' !== $this->node->nodeName) { Chris@0: throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName)); Chris@0: } Chris@0: Chris@0: if ('file' !== strtolower($this->node->getAttribute('type'))) { Chris@0: throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $this->node->getAttribute('type'))); Chris@0: } Chris@0: Chris@0: $this->setValue(null); Chris@0: } Chris@0: }