5use InvalidArgumentException;
6use Psr\Http\Message\UploadedFileInterface;
7use UnexpectedValueException;
36 public function getValue( $name, array $settings, array $options ) {
37 $ret = $this->callbacks->getUploadedFile( $name, $options );
39 if ( $ret && $ret->getError() === UPLOAD_ERR_NO_FILE &&
40 !$this->callbacks->hasParam( $name, $options )
45 } elseif ( !$ret && $this->callbacks->hasParam( $name, $options ) ) {
70 return ini_get(
'upload_max_filesize' );
73 public function validate( $name, $value, array $settings, array $options ) {
76 UPLOAD_ERR_FORM_SIZE =>
'formsize',
77 UPLOAD_ERR_PARTIAL =>
'partial',
78 UPLOAD_ERR_NO_FILE =>
'nofile',
79 UPLOAD_ERR_NO_TMP_DIR =>
'notmpdir',
80 UPLOAD_ERR_CANT_WRITE =>
'cantwrite',
81 UPLOAD_ERR_EXTENSION =>
'phpext',
84 if ( !$value instanceof UploadedFileInterface ) {
86 $type = is_object( $value ) ? get_class( $value ) : gettype( $value );
87 throw new InvalidArgumentException(
"\$value must be UploadedFileInterface, got $type" );
90 $err = $value->getError();
91 if ( $err === UPLOAD_ERR_OK ) {
93 } elseif ( $err === UPLOAD_ERR_INI_SIZE ) {
100 $last = strtolower( substr( $size, -1 ) );
101 $size = intval( $size, 10 ) * ( $prefixes[$last] ?? 1 );
106 ],
'inisize' )->sizeParams( $size ),
107 $name,
'', $settings, $options
109 } elseif ( isset( $codemap[$err] ) ) {
111 $this->
failureMessage(
'badupload', [
'code' => $codemap[$err] ], $codemap[$err] ),
112 $name,
'', $settings, $options
116 foreach ( get_defined_constants() as $c => $v ) {
117 if ( $v === $err && substr( $c, 0, 11 ) ===
'UPLOAD_ERR_' ) {
118 $constant =
" ($c?)";
121 throw new UnexpectedValueException(
"Unrecognized PHP upload error value $err$constant" );
125 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
126 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
130 'Cannot specify a default for upload-type parameters';
137 'PARAM_ISMULTI cannot be used for upload-type parameters';
143 public function stringifyValue( $name, $value, array $settings, array $options ) {
148 public function getHelpInfo( $name, array $settings, array $options ) {
149 $info = parent::getHelpInfo( $name, $settings, $options );