VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

Path using PropertyDirectoryNamer may not be '0'?

Open Nexotap opened this issue 4 years ago • 4 comments

Not sure if it's expected behaviour, but to me it looks like a bug.

Here's the function I use for retrieving the directory path:

public function getPath()
{
    $path = strval($this->getSupplier() ? $this->getSupplier()->getId() : 0);
    return $path;
}

Whereas the config looks like this:

mappings:
    invoice:
        uri_prefix: /documents
        upload_destination: '%kernel.project_dir%/public/documents'
        namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
        directory_namer:
            service: vich_uploader.namer_directory_property
            options: { property: 'path', transliterate: false} 

The idea is, returning the id of the Supplier within the entity and use it as path. In case no Supplier is defined, the path should be 0.

While every other number than 0 works, I get the following error when returning 0:

An exception has been thrown during the rendering of a template ("Directory name could not be generated: property path is empty.").

Why should a path containing a directory named '0' not be possible?

Nexotap avatar Mar 20 '20 12:03 Nexotap

Because zero is empty

garak avatar Mar 20 '20 13:03 garak

I know that zero is considered empty. But those numbers are getting converted to string.

Maybe you can enlighten me? Let's assume this scenario:

$path = "/documents/";
$number = strval(0);
$complete = $path .= $number;

echo strlen($number)."\n";
echo $complete;

Here, strlen shows me that $number has one character, which is not zero to me. Also $complete will print "/documents/0". Which should be a total eligible pathname.

I dont want to be rude, I just dont understand why 0, when converted to a string should be treated as zero.

Nexotap avatar Mar 20 '20 13:03 Nexotap

That's how PHP type juggling works. No matter if zero is 0 or '0', it will be empty anyway.

So, this is not a bug, it's just the way PropertyDirectoryNamer was implemented. Feel free to propose a PR to change it

garak avatar Mar 20 '20 13:03 garak

I see and am totally amazed. So many years php and only see this now. Thanks for the enlightenment :smile:

Will close this now and try propose a PR, since it still makes sense to me allowing a directory named "0".

Nexotap avatar Mar 20 '20 14:03 Nexotap

I guess this was not needed in the end. Anyway, feel free to open a PR if so.

garak avatar Feb 07 '23 15:02 garak