Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
name: Tests

on: [push, pull_request]
on:
push:
pull_request:

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1', '8.2']
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1', '8.2']
fail-fast: false

fail-fast: false
name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v4

name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, tokenizer, sqlite3
coverage: none
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, tokenizer, sqlite3
coverage: none

- run: composer install --no-interaction --prefer-dist
- run: vendor/bin/tester tests -s -C
- if: failure()
uses: actions/upload-artifact@v2
with:
name: output
path: tests/**/output
- run: composer install --no-interaction --prefer-dist
- run: vendor/bin/tester tests -s -C

- if: failure()
uses: actions/upload-artifact@v4
with:
name: output
path: tests/**/output
1 change: 0 additions & 1 deletion LeanMapperQuery/Caller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery;

/**
Expand Down
14 changes: 5 additions & 9 deletions LeanMapperQuery/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery;

use LeanMapper;
use LeanMapper\Filtering;
use LeanMapper\Fluent;
use LeanMapper\Reflection\Property;
use LeanMapper\Relationship;
use LeanMapper\Result;
use LeanMapperQuery\Caller;
use LeanMapperQuery\Exception\InvalidArgumentException;
use LeanMapperQuery\Exception\InvalidMethodCallException;
use LeanMapperQuery\Exception\InvalidRelationshipException;
use LeanMapperQuery\Exception\InvalidStateException;
use LeanMapperQuery\Exception\MemberAccessException;
use LeanMapperQuery\IQuery;
use LeanMapper\Filtering;
use LeanMapper\Fluent;
use LeanMapper\Reflection\Property;
use LeanMapper\Relationship;
use LeanMapper\Result;

/**
* @author Michal Bohuslávek
Expand Down Expand Up @@ -83,7 +82,6 @@ public static function queryEntityProperty(LeanMapper\Entity $entity, $field, IQ
$targetTable = $relationship->getTargetTable();
$referencingColumn = $relationship->getColumnReferencingSourceTable();
$rows = $entity->row->referencing($targetTable, $referencingColumn, new Filtering($filters), $strategy);

} elseif ($relationship instanceof Relationship\HasMany) {
$filters[] = function (Fluent $fluent) use ($mapper, $query) {
$query->applyQuery($fluent, $mapper, new QueryTarget\HasManyTargetTable);
Expand Down Expand Up @@ -148,10 +146,8 @@ public function __call($name, array $arguments)
list(, $method, $field) = $matches;
$field = lcfirst($field);
return $this->$method($field, $query);

} else {
return parent::__call($name, $arguments);
}
}

}
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/InvalidMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/InvalidRelationshipException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/InvalidStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/MemberAccessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/NonExistingMethodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\Exception;

/**
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This file is part of the LeanMapperQuery extension
* for the Lean Mapper library (http://leanmapper.com)
*/

namespace LeanMapperQuery\Exception;

class NotImplementedException extends Exception
Expand Down
2 changes: 0 additions & 2 deletions LeanMapperQuery/ICaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery;

use LeanMapper\Fluent;

interface ICaster
{

/**
* @param Fluent $fluent
* @param string $entityClass
Expand Down
2 changes: 0 additions & 2 deletions LeanMapperQuery/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery;

use LeanMapper\Fluent;
Expand All @@ -19,7 +18,6 @@
*/
interface IQuery
{

/**
* @param Fluent $fluent
* @param IMapper $mapper
Expand Down
47 changes: 22 additions & 25 deletions LeanMapperQuery/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery;

use LeanMapper;
use LeanMapperQuery\Exception\InvalidArgumentException;
use LeanMapperQuery\Exception\InvalidRelationshipException;
use LeanMapperQuery\Exception\InvalidStateException;
use LeanMapperQuery\Exception\MemberAccessException;
use LeanMapperQuery\Exception\NonExistingMethodException;
use LeanMapper\Fluent;
use LeanMapper\IMapper;
use LeanMapper\ImplicitFilters;
use LeanMapper\Reflection\Property;
use LeanMapper\Relationship;
use LeanMapper\Result;
use LeanMapperQuery\Exception\InvalidArgumentException;
use LeanMapperQuery\Exception\InvalidRelationshipException;
use LeanMapperQuery\Exception\InvalidStateException;
use LeanMapperQuery\Exception\MemberAccessException;
use LeanMapperQuery\Exception\NonExistingMethodException;

/**
* @author Michal Bohuslávek
*
* @template T of \LeanMapper\Entity
*
* @method $this where($cond, ...$args)
* @method $this orderBy($field)
* @method $this where(string $cond, mixed ...$args)
* @method $this orderBy(string $field, mixed ...$args)
* @method $this asc(bool $asc = true)
* @method $this desc(bool $desc = true)
* @method $this limit(int $limit)
Expand Down Expand Up @@ -231,7 +230,7 @@ private function traverseToRelatedEntity(&$currentTable, &$currentTableAlias, Pr
$entityClass = $this->mapper->getEntityClass($currentTable);
throw new InvalidRelationshipException("Property '{$property->getName()}' in entity '$entityClass' doesn't have any relationship.");
}
$implicitFilters= [];
$implicitFilters = [];
$propertyType = $property->getType();
if (is_subclass_of($propertyType, 'LeanMapper\\Entity')) {
$caller = new Caller($this, $property);
Expand All @@ -245,14 +244,12 @@ private function traverseToRelatedEntity(&$currentTable, &$currentTableAlias, Pr
$referencingColumn = $relationship->getColumnReferencingTargetTable();
// Join table.
$targetTableAlias = $this->joinRelatedTable($currentTableAlias, $referencingColumn, $targetTable, $targetTablePrimaryKey, $implicitFilters, false);

} elseif ($relationship instanceof Relationship\BelongsTo) { // BelongsToOne, BelongsToMany
$targetTable = $relationship->getTargetTable();
$sourceTablePrimaryKey = $this->mapper->getPrimaryKey($currentTable);
$referencingColumn = $relationship->getColumnReferencingSourceTable();
// Join table.
$targetTableAlias = $this->joinRelatedTable($currentTableAlias, $sourceTablePrimaryKey, $targetTable, $referencingColumn, $implicitFilters);

} elseif ($relationship instanceof Relationship\HasMany) {
$sourceTablePrimaryKey = $this->mapper->getPrimaryKey($currentTable);
$relationshipTable = $relationship->getRelationshipTable();
Expand All @@ -265,7 +262,6 @@ private function traverseToRelatedEntity(&$currentTable, &$currentTableAlias, Pr
// Don't apply filters on relationship table.
$relationshipTableAlias = $this->joinRelatedTable($currentTableAlias, $sourceTablePrimaryKey, $relationshipTable, $sourceReferencingColumn);
$targetTableAlias = $this->joinRelatedTable($relationshipTableAlias, $targetReferencingColumn, $targetTable, $targetTablePrimaryKey, $implicitFilters, false);

} else {
throw new InvalidRelationshipException('Unknown relationship type in property {$property->getName()}.');
}
Expand All @@ -287,7 +283,7 @@ private function replacePlaceholder(Property $property)
if ($type === 'DateTime' || is_subclass_of($type, 'DateTime')) {
if ($property->hasCustomFlag(self::$typeFlagName)
&& preg_match('#^(DATE|Date|date)$#', $property->getCustomFlagValue(self::$typeFlagName))) {
return self::$placeholders['Date'];
return self::$placeholders['Date'];
} else {
return self::$placeholders['DateTime'];
}
Expand Down Expand Up @@ -327,7 +323,6 @@ protected function parseStatement($statement, $replacePlaceholders = null)

if ($this->castedEntityClass) {
list($rootEntityClass, $rootProperties) = $this->getPropertiesByEntity($this->castedEntityClass);

} else {
list($rootEntityClass, $rootProperties) = $this->getPropertiesByTable($rootTableName);
}
Expand Down Expand Up @@ -405,7 +400,6 @@ protected function parseStatement($statement, $replacePlaceholders = null)
$properties = $rootProperties;
$tableNameAlias = $tableName = $rootTableName;
$entityClass = $rootEntityClass;

} elseif ($replacePlaceholders && $ch === self::$defaultPlaceholder && $switches["'"] === false && $switches['"'] === false) {
if ($property === null) {
$output .= $ch;
Expand Down Expand Up @@ -479,14 +473,12 @@ public function applyQuery(Fluent $fluent, IMapper $mapper, QueryTarget\ITarget
$relationship->getRelationshipTable(),
$relationship->getColumnReferencingTargetTable(),
$targetTable,
$mapper->getPrimaryKey($targetTable)
$mapper->getPrimaryKey($targetTable),
);
}

} elseif ($target instanceof QueryTarget\HasManyTargetTable) {
$fluent->removeClause('LIMIT');
$fluent->removeClause('OFFSET');

} elseif ($target !== null) {
throw new InvalidArgumentException('Unsupported query target.');
}
Expand Down Expand Up @@ -591,7 +583,7 @@ public function __call($name, array $args)

/////////////// basic commands //////////////////////

private function commandWhere($cond)
private function commandWhere($cond) // @phpstan-ignore-line
{
if (is_array($cond)) {
if (func_num_args() > 1) {
Expand Down Expand Up @@ -655,7 +647,7 @@ private function replaceEntitiesForItsPrimaryKeyValues(array $entities)
return $entities;
}

private function commandOrderBy($field)
private function commandOrderBy($field) // @phpstan-ignore-line
{
if (is_array($field)) {
foreach ($field as $key => $value) {
Expand All @@ -666,8 +658,13 @@ private function commandOrderBy($field)
}
}
} else {
$field = $this->parseStatement($field);
$this->fluent->orderBy($field);
$args = func_get_args();
// Only first argument is parsed. Other arguments will be maintained
// as parameters.
$statement = &$args[0];
$statement = $this->parseStatement($statement, null);
$args = $this->replaceEntitiesForItsPrimaryKeyValues($args);
call_user_func_array([$this->fluent, 'orderBy'], $args);
}
}

Expand All @@ -676,17 +673,17 @@ private function commandAsc($asc = true)
$this->fluent->{$asc ? 'asc' : 'desc'}();
}

private function commandDesc($desc = true)
private function commandDesc($desc = true) // @phpstan-ignore-line
{
$this->commandAsc(!$desc);
}

private function commandLimit($limit)
private function commandLimit($limit) // @phpstan-ignore-line
{
$this->fluent->limit($limit);
}

private function commandOffset($offset)
private function commandOffset($offset) // @phpstan-ignore-line
{
$this->fluent->offset($offset);
}
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/QueryTarget/HasManyRelationshipTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\QueryTarget;

use LeanMapper\Relationship;
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/QueryTarget/HasManyTargetTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\QueryTarget;

class HasManyTargetTable implements ITarget
Expand Down
1 change: 0 additions & 1 deletion LeanMapperQuery/QueryTarget/ITarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* for the Lean Mapper library (http://leanmapper.com)
* Copyright (c) 2013 Michal Bohuslávek
*/

namespace LeanMapperQuery\QueryTarget;

interface ITarget
Expand Down
Loading