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 ) {
118 if ( $v === $err && str_starts_with( $c,
'UPLOAD_ERR_' ) ) {
119 $constant =
" ($c?)";
122 throw new UnexpectedValueException(
"Unrecognized PHP upload error value $err$constant" );
126 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
127 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
131 'Cannot specify a default for upload-type parameters';
138 'PARAM_ISMULTI cannot be used for upload-type parameters';
144 public function stringifyValue( $name, $value, array $settings, array $options ) {
149 public function getHelpInfo( $name, array $settings, array $options ) {
150 $info = parent::getHelpInfo( $name, $settings, $options );