Skip to content

Commit

Permalink
Merge pull request #9 from opcoding/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
acacopardo committed Mar 15, 2017
2 parents 072046f + 648f81e commit 92049cd
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 18 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ As you can see mutiple methods are available like `filename` and multiple operat
- `beginsWith` : to use the `LIKE` operator and the string has to start by the value specified
- `endsWith` : to use the `LIKE` operator and the string has to end by the value specified

Note that you can search on multiple categories by adding multiple categories filter :

```php
$builder->category()->equal(1);
$builder->category()->equal(2);
$builder->category()->equal(3);
```

### Upload a new file

You can upload a file instance with `Filer::upload($file, $flags)`:
Expand Down
7 changes: 5 additions & 2 deletions examples/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
use Fei\Service\Filer\Client\Builder\SearchBuilder;
use Fei\Service\Filer\Client\Filer;
use Fei\ApiClient\Transport\BasicTransport;
use Fei\Service\Filer\Entity\File;

$filer = new Filer([Filer::OPTION_BASEURL => 'http://172.17.0.1:8003']);
$filer = new Filer([Filer::OPTION_BASEURL => 'http://127.0.0.1:8020']);

$filer->setTransport(new BasicTransport());

try {
$searchBuilder = new SearchBuilder();
$searchBuilder->category()->equal(2);

$searchBuilder->category()->equal(File::CATEGORY_CLIENT);
$searchBuilder->category()->equal(File::CATEGORY_SUPPLIER);
$searchBuilder->context()->key('test 1')->equal('test 1');
$searchBuilder->filename()->equal('avatar.png');

Expand Down
12 changes: 4 additions & 8 deletions examples/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require __DIR__ . '/../vendor/autoload.php';

$filer = new Filer([Filer::OPTION_BASEURL => 'http://127.0.0.1:8080']);
$filer = new Filer([Filer::OPTION_BASEURL => 'http://127.0.0.1:8020']);

$filer->setTransport(new BasicTransport());

Expand All @@ -32,13 +32,9 @@

echo (is_string($uuid) ? $uuid : (string) $uuid->getBody()) . PHP_EOL;

$uuid = $filer->upload(
(new File())
->setCategory(File::CATEGORY_IMG)
->setUuid($uuid)
->setFile(new SplFileObject(__DIR__ . '/../tests/_data/capture.png'))
->setContexts(['test 3' => 'test 3', 'test 4' => 'test 4', 'test 5' => 'test 5'])
);
$file = $filer->retrieve($uuid);
$file->setContexts(['test 1' => 'New value']);
$filer->upload($file);

echo (is_string($uuid) ? $uuid : (string) $uuid->getBody()) . PHP_EOL;

Expand Down
7 changes: 6 additions & 1 deletion src/Builder/Fields/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ class Category extends OperatorBuilder
public function build($value, $operator = null)
{
$search = $this->builder->getParams();
$search['category'] = $value;

if (!isset($search['category'])) {
$search['category'] = [];
}

$search['category'][] = $value;

$this->builder->setParams($search);
}
Expand Down
16 changes: 12 additions & 4 deletions src/Filer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public function search(SearchBuilder $builder)
*/
public function upload(File $file, $flags = null)
{
$this->validateFile($file);

if ($flags & self::NEW_REVISION && $file->getUuid() === null) {
throw new ValidationException('UUID must be set when adding a new revision');
}
Expand All @@ -67,13 +65,22 @@ public function upload(File $file, $flags = null)
throw new FilerException('Asynchronous Transport has to be set');
}


$method = 'POST';
$checkData = true;
if ($flags & self::NEW_REVISION) {
$method = 'PUT';
} elseif ($file->getUuid() !== null && !$uuidCreated) {
$method = 'PATCH';
$checkData = false;

if ($file instanceof FileWrapper) {
$file->setSkipData(true);
}
}

$this->validateFile($file, $checkData);

$request = (new RequestDescriptor())
->setMethod($method)
->setUrl($this->buildUrl(self::API_FILER_PATH_INFO));
Expand Down Expand Up @@ -323,10 +330,11 @@ public static function embed($path)
* Validate a File entity
*
* @param File $file
* @param bool $checkData
*/
protected function validateFile(File $file)
protected function validateFile(File $file, $checkData = true)
{
$validator = new FileValidator();
$validator = new FileValidator($checkData);

if (!$validator->validate($file)) {
throw (new ValidationException(
Expand Down
31 changes: 30 additions & 1 deletion src/Service/FileWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class FileWrapper extends File
*/
protected $filer;

/**
* @var bool
*/
protected $skipData = false;

/**
* FileWrapper constructor.
*
Expand Down Expand Up @@ -55,10 +60,34 @@ public function getData()
$data = parent::getData();

// getting the data if they are not loaded yet
if (empty($data)) {
if (empty($data) && $this->isSkipData() === false) {
$data = $this->getFiler()->getFileBinary($this)->getData();
}

return $data;
}

/**
* Get SkipData
*
* @return bool
*/
public function isSkipData()
{
return $this->skipData;
}

/**
* Set SkipData
*
* @param bool $skipData
*
* @return $this
*/
public function setSkipData($skipData)
{
$this->skipData = $skipData;

return $this;
}
}
2 changes: 1 addition & 1 deletion tests/unit/Builder/Fields/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testBuild()
$category->build(1);

$this->assertAttributeEquals([
'category' => 1
'category' => [1]
], 'params', $builder);
}
}
9 changes: 9 additions & 0 deletions tests/unit/FileWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ public function testGetDataWhenDataAreNotAlreadyLoaded()

$this->assertEquals('fake-data', $filerWrapper->getData());
}

public function testSkipDataAccessors()
{
$fileWrapper = new FileWrapper(new Filer([]));
$fileWrapper->setSkipData(false);

$this->assertEquals(false, $fileWrapper->isSkipData());
$this->assertAttributeEquals($fileWrapper->isSkipData(), 'skipData', $fileWrapper);
}
}
2 changes: 1 addition & 1 deletion tests/unit/FilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testUploadAsyncTransportNoUuid()
->setConstructorArgs([[Filer::OPTION_BASEURL => 'http://url']])
->setMethods(['createUuid'])
->getMock();
$filer->expects($this->once())->method('createUuid')->willReturn('test-uuid');
$filer->expects($this->once())->method('createUuid')->willReturn('bck1:22b52127-7dbd-4294-81f4-8638906c646a');

$transport = $this->createMock(AsyncTransportInterface::class);
$transport->expects($this->once())->method('send')->willReturnCallback(
Expand Down

0 comments on commit 92049cd

Please sign in to comment.