Skip to content

Commit

Permalink
Merge pull request #157 from dschoenbauer/develop
Browse files Browse the repository at this point in the history
1.4 Release
  • Loading branch information
dschoenbauer committed Aug 9, 2018
2 parents 7684c05 + 2f0f99b commit cc58b8f
Show file tree
Hide file tree
Showing 29 changed files with 660 additions and 265 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
"standards": "phpcs --standard=PSR2 --colors src/",
"inspect-repair": "phpcbf --standard=PSR2 src/"
}
}
}
471 changes: 248 additions & 223 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Orm/DataProvider/EntityDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getData()
$this->getSelect()
->setTable($this->getEntity()->getTable())
->setFields(array_merge([$this->getEntity()->getIdField() . ' idx'], $this->getEntity()->getAllFields()))
->setFetchStyle(\PDO::FETCH_ASSOC || \PDO::FETCH_UNIQUE)
->setFetchStyle(\PDO::FETCH_ASSOC | \PDO::FETCH_UNIQUE)
->setFetchFlat(false);
return $this->getSelect()->execute($this->getAdapter());
}
Expand Down
10 changes: 7 additions & 3 deletions src/Orm/Events/DataProvider/DataProviderEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use DSchoenbauer\Orm\Entity\EntityInterface;
use DSchoenbauer\Orm\Enum\EventPriorities as EventPriority;
use DSchoenbauer\Orm\Events\AbstractEvent;
use DSchoenbauer\Orm\VisitorInterface;
use Zend\EventManager\EventInterface;

/**
Expand All @@ -39,23 +40,26 @@ class DataProviderEvent extends AbstractEvent
{

private $dataProvider;

public function __construct(array $events, DataProviderInterface $dataProvider, $priority = EventPriority::ON_TIME)
{
parent::__construct($events, $priority);
$this->setDataProvider($dataProvider);
}

public function onExecute(EventInterface $event)
{
$model = $event->getTarget();
if (!$this->validateModel($model, EntityInterface::class)) {
return false;
}
if ($this->getDataProvider() instanceof VisitorInterface) {
$model->accept($this->getDataProvider());
}
$model->setData($this->getDataProvider()->getData());
return true;
}

/**
* @return DataProviderInterface
*/
Expand Down
112 changes: 112 additions & 0 deletions src/Orm/Events/Logger/DebugLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
* The MIT License
*
* Copyright 2018 David Schoenbauer.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace DSchoenbauer\Orm\Events\Logger;

use DSchoenbauer\Exception\Http\ClientError\NotFoundException;
use DSchoenbauer\Exception\Http\ServerError\ServerErrorException;
use DSchoenbauer\Orm\Enum\EventPriorities;
use DSchoenbauer\Orm\Events\AbstractEvent;
use Zend\EventManager\EventInterface;
use function GuzzleHttp\json_decode;
use function GuzzleHttp\json_encode;

/**
* Description of DebugLog
*
* @author David Schoenbauer
*/
class DebugLog extends AbstractEvent
{

private $file;
private $callback = null;

public function __construct(array $events, $logFile, $priority = EventPriorities::ON_TIME, $payloadCallback = null)
{
$this->setFile($logFile)->setCallback($payloadCallback);
parent::__construct($events, $priority);
}

public function onExecute(EventInterface $event)
{
$this->logAction($this->getPayload($event));
return true;
}

public function logAction($payload)
{
$data = [];
if (file_exists($this->getFile())) {
$data = json_decode(file_get_contents($this->getFile()), true) ?: [];
}
$data[] = $payload;
file_put_contents($this->getFile(), json_encode($data));
}

public function getPayload(EventInterface $event)
{
$callBack = $this->getCallback();
if (is_callable($callBack)) {
return $callBack($event);
}
return ['name' => $event->getName(), 'target' => $event->getTarget(), 'timestamp' => time()];
}

/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}

/**
* @param mixed $file
* @return DebugLog
*/
public function setFile($file)
{

if (!is_writable(dirname($file))) {
throw new NotFoundException("Path: " . dirname($file) . " not found");
}
$this->file = $file;
return $this;
}

public function getCallback()
{
return $this->callback;
}

public function setCallback($callback)
{
if ($callback !== null && !is_callable($callback)) {
throw new ServerErrorException('Code provided to debug log is not callable');
}
$this->callback = $callback;
return $this;
}
}
2 changes: 1 addition & 1 deletion src/Orm/Events/Logger/ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function convertToName($exc)
$inPieces = explode('\\', $fullName);
$file = array_pop($inPieces);
$matches = preg_split('/(?=[A-Z])/', $file);
return trim(implode(' ', $matches));
return trim(implode(' ', $matches ?: []));
}
}
9 changes: 8 additions & 1 deletion src/Orm/Events/Persistence/Http/DataExtract/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
namespace DSchoenbauer\Orm\Events\Persistence\Http\DataExtract;

use Zend\Http\Header\HeaderInterface;
use Zend\Http\Response;

/**
Expand All @@ -41,6 +42,12 @@ public function extract(Response $response)

public function match(Response $response)
{
return strpos(strtolower($response->getHeaders()->get('Content-type')->getFieldValue()), "json") !== false;

//Header\HeaderInterface
$header = $response->getHeaders()->get('Content-type');
if (!$header instanceof HeaderInterface) {
return false;
}
return strpos(strtolower($header->getFieldValue()), "json") !== false;
}
}
7 changes: 6 additions & 1 deletion src/Orm/Events/Persistence/Http/DataExtract/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use DSchoenbauer\Orm\Exception\InvalidXmlException;
use DSchoenbauer\Orm\Framework\XmlToArrayParser;
use Zend\Http\Header\HeaderInterface;
use Zend\Http\Response;

/**
Expand All @@ -48,6 +49,10 @@ public function extract(Response $response)

public function match(Response $response)
{
return strpos(strtolower($response->getHeaders()->get('Content-type')->getFieldValue()), "xml") !== false;
$header = $response->getHeaders()->get('Content-type');
if (!$header instanceof HeaderInterface) {
return false;
}
return strpos(strtolower($header->getFieldValue()), "xml") !== false;
}
}
2 changes: 1 addition & 1 deletion src/Orm/Events/Persistence/Pdo/AbstractPdoEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(array $events, PDO $adapter, $priority = EventPriori
public function onExecute(EventInterface $event)
{
if (!$this->validateModel($event->getTarget(), EntityInterface::class)) {
return;
return false;
}
return $this->commit($event);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/Events/Persistence/Pdo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Create extends AbstractPdoEvent
/**
* event action
* @param EventInterface $event object passed when event is fired
* @return void
* @return bool
* @since v1.0.0
*/
public function commit(EventInterface $event)
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Events/Persistence/Pdo/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Delete extends AbstractPdoEvent
/**
* event action
* @param EventInterface $event object passed when event is fired
* @return void
* @return bool
* @since v1.0.0
*/
public function commit(EventInterface $event)
Expand All @@ -41,6 +41,7 @@ public function commit(EventInterface $event)
->setTable($entity->getTable())
->setWhere(new ArrayWhere([$entity->getIdField() => $model->getId()]))
->execute($this->getAdapter());
return true;
} catch (NoRecordsAffectedException $exc) {
throw new RecordNotFoundException();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Events/Persistence/Pdo/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Select extends AbstractPdoEvent
/**
* event action
* @param EventInterface $event object passed when event is fired
* @return void
* @return bool
* @since v1.0.0
*/
public function commit(EventInterface $event)
Expand All @@ -63,6 +63,7 @@ public function commit(EventInterface $event)
->setFetchFlat()
->execute($this->getAdapter())
);
return true;
} catch (NoRecordsAffectedException $exc) {
throw new RecordNotFoundException();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Events/Persistence/Pdo/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Update extends AbstractPdoEvent
/**
* event action
* @param EventInterface $event object passed when event is fired
* @return void
* @return bool
* @since v1.0.0
*/
public function commit(EventInterface $event)
Expand All @@ -41,6 +41,7 @@ public function commit(EventInterface $event)
->setData($this->reduceFields($model->getData(), $entity->getAllFields()))
->setWhere(new ArrayWhere([$entity->getIdField() => $model->getId()]))
->execute($this->getAdapter());
return true;
} catch (NoRecordsAffectedException $exc) {
throw new RecordNotFoundException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/Events/Validate/AbstractValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract public function getFields($entity);

/**
* method is called when a given event is triggered
* @param Event $event EventInterface object passed at time of triggering
* @param EventInterface $event EventInterface object passed at time of triggering
* @throws InvalidDataTypeException thrown when value does not validate
* @return void
* @since v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function accept(VisitorInterface $visitor)

/**
* provides a unique identifier value for a given record
* @return integer
* @return string
* @since v1.0.0
*/
public function getId()
Expand All @@ -108,7 +108,7 @@ public function getData()

/**
* sets a unique identifier value for a given record
* @param integer $idx
* @param string $idx
* @return Model
* @since v1.0.0
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function accept(VisitorInterface $visitor);

/**
* provides a unique identifier value for a given record
* @return integer
* @return string
* @since v1.0.0
*/
public function getId();
Expand All @@ -81,7 +81,7 @@ public function getData();

/**
* sets a unique identifier value for a given record
* @param integer $idx
* @param string $idx
* @return ModelInterface
* @since v1.0.0
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<directory suffix=".php">../src</directory>
<exclude>
<directory suffix="Interface.php">../src</directory>
<directory>./vendor</directory>
<directory suffix=".php">../src/Orm/Enum</directory>
<directory>./vendor</directory>
<directory>./tests</directory>
</exclude>
</whitelist>
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Orm/DataProvider/EntityDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testGetData()
$select = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
$select->expects($this->atLeast(1))->method('setTable')->with($table)->willReturnSelf();
$select->expects($this->atLeast(1))->method('setFields')->with(['id idx', 'id', 'test'])->willReturnSelf();
$select->expects($this->atLeast(1))->method('setFetchStyle')->with(\PDO::FETCH_ASSOC || \PDO::FETCH_UNIQUE)->willReturnSelf();
$select->expects($this->atLeast(1))->method('setFetchStyle')->with(\PDO::FETCH_ASSOC | \PDO::FETCH_UNIQUE)->willReturnSelf();
$select->expects($this->atLeast(1))->method('setFetchFlat')->with(false)->willReturnSelf();
$select->expects($this->atLeast(1))->method('execute')->with($adapter)->willReturn($result);

Expand Down

0 comments on commit cc58b8f

Please sign in to comment.