Skip to content

Commit 0b2566f

Browse files
committed
refactor: rename constrain to scope
1 parent 7cfd773 commit 0b2566f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+531
-383
lines changed

src/Factory.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use Cycle\ORM\Exception\TypecastException;
1616
use Cycle\ORM\Mapper\Mapper;
1717
use Cycle\ORM\Relation\RelationInterface;
18-
use Cycle\ORM\Select\ConstrainInterface;
1918
use Cycle\ORM\Select\LoaderInterface;
2019
use Cycle\ORM\Select\Repository;
20+
use Cycle\ORM\Select\ScopeInterface;
2121
use Cycle\ORM\Select\Source;
2222
use Cycle\ORM\Select\SourceInterface;
2323
use Spiral\Core\Container;
@@ -39,9 +39,9 @@ final class Factory implements FactoryInterface
3939
/** @var array<string, string> */
4040
private $defaults = [
4141
Schema::REPOSITORY => Repository::class,
42-
Schema::SOURCE => Source::class,
43-
Schema::MAPPER => Mapper::class,
44-
Schema::CONSTRAIN => null,
42+
Schema::SOURCE => Source::class,
43+
Schema::MAPPER => Mapper::class,
44+
Schema::SCOPE => null,
4545
];
4646

4747
/**
@@ -86,8 +86,8 @@ public function mapper(
8686
return $this->factory->make(
8787
$class,
8888
[
89-
'orm' => $orm,
90-
'role' => $role,
89+
'orm' => $orm,
90+
'role' => $role,
9191
'schema' => $schema->define($role, Schema::SCHEMA)
9292
]
9393
);
@@ -107,8 +107,8 @@ public function loader(
107107
return $this->config->getLoader($schema[Relation::TYPE])->resolve(
108108
$this->factory,
109109
[
110-
'orm' => $orm,
111-
'name' => $relation,
110+
'orm' => $orm,
111+
'name' => $relation,
112112
'target' => $schema[Relation::TARGET],
113113
'schema' => $schema[Relation::SCHEMA]
114114
]
@@ -130,8 +130,8 @@ public function relation(
130130
return $this->config->getRelation($type)->resolve(
131131
$this->factory,
132132
[
133-
'orm' => $orm,
134-
'name' => $relation,
133+
'orm' => $orm,
134+
'name' => $relation,
135135
'target' => $relSchema[Relation::TARGET],
136136
'schema' => $relSchema[Relation::SCHEMA] + [Relation::LOAD => $relSchema[Relation::LOAD] ?? null],
137137
]
@@ -165,8 +165,8 @@ public function repository(
165165
$class,
166166
[
167167
'select' => $select,
168-
'orm' => $orm,
169-
'role' => $role,
168+
'orm' => $orm,
169+
'role' => $role,
170170
]
171171
);
172172
}
@@ -194,17 +194,17 @@ public function source(
194194
$schema->define($role, Schema::TABLE)
195195
);
196196

197-
$constrain = $schema->define($role, Schema::CONSTRAIN) ?? $this->defaults[Schema::CONSTRAIN];
197+
$scope = $schema->define($role, Schema::SCOPE) ?? $this->defaults[Schema::SCOPE];
198198

199-
if ($constrain === null) {
199+
if ($scope === null) {
200200
return $source;
201201
}
202202

203-
if (!is_subclass_of($constrain, ConstrainInterface::class)) {
204-
throw new TypecastException($constrain . ' does not implement ' . ConstrainInterface::class);
203+
if (!is_subclass_of($scope, ScopeInterface::class)) {
204+
throw new TypecastException($scope . ' does not implement ' . ScopeInterface::class);
205205
}
206206

207-
return $source->withConstrain(is_object($constrain) ? $constrain : $this->factory->make($constrain));
207+
return $source->withScope(is_object($scope) ? $scope : $this->factory->make($scope));
208208
}
209209

210210
/**

src/ORM.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function getRepository($entity): RepositoryInterface
265265

266266
if ($this->schema->define($role, Schema::TABLE) !== null) {
267267
$select = new Select($this, $role);
268-
$select->constrain($this->getSource($role)->getConstrain());
268+
$select->scope($this->getSource($role)->getScope());
269269
}
270270

271271
return $this->repositories[$role] = $this->factory->repository($this, $this->schema, $role, $select);

src/Relation/Pivoted/PivotedPromise.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ final class PivotedPromise implements PromiseInterface
4444

4545
/**
4646
* @param ORMInterface $orm
47-
* @param string $target
48-
* @param array $relationSchema
49-
* @param mixed $innerKey
47+
* @param string $target
48+
* @param array $relationSchema
49+
* @param mixed $innerKey
5050
*/
5151
public function __construct(ORMInterface $orm, string $target, array $relationSchema, $innerKey)
5252
{
@@ -113,9 +113,9 @@ public function __resolve()
113113

114114
/** @var ManyToManyLoader $loader */
115115
$loader = $loader->withContext($loader, [
116-
'constrain' => $this->orm->getSource($this->target)->getConstrain(),
117-
'as' => $this->target,
118-
'method' => JoinableLoader::POSTLOAD
116+
'scope' => $this->orm->getSource($this->target)->getScope(),
117+
'as' => $this->target,
118+
'method' => JoinableLoader::POSTLOAD
119119
]);
120120

121121
$query = $loader->configureQuery($query, [$this->innerKey]);

src/SchemaInterface.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@ interface SchemaInterface
1818
/*
1919
* Various segments of schema.
2020
*/
21-
public const ROLE = 0;
22-
public const ENTITY = 1;
23-
public const MAPPER = 2;
24-
public const SOURCE = 3;
25-
public const REPOSITORY = 4;
26-
public const DATABASE = 5;
27-
public const TABLE = 6;
28-
public const PRIMARY_KEY = 7;
21+
public const ROLE = 0;
22+
public const ENTITY = 1;
23+
public const MAPPER = 2;
24+
public const SOURCE = 3;
25+
public const REPOSITORY = 4;
26+
public const DATABASE = 5;
27+
public const TABLE = 6;
28+
public const PRIMARY_KEY = 7;
2929
public const FIND_BY_KEYS = 8;
30-
public const COLUMNS = 9;
31-
public const RELATIONS = 10;
32-
public const CHILDREN = 11;
33-
public const CONSTRAIN = 12;
34-
public const TYPECAST = 13;
35-
public const SCHEMA = 14;
30+
public const COLUMNS = 9;
31+
public const RELATIONS = 10;
32+
public const CHILDREN = 11;
33+
public const SCOPE = 12;
34+
public const TYPECAST = 13;
35+
public const SCHEMA = 14;
36+
37+
/**
38+
* @deprecated use {@see SchemaInterface::SCOPE} instead
39+
*/
40+
public const CONSTRAIN = self::SCOPE;
3641

3742
/**
3843
* Return all roles defined within the schema.
@@ -63,7 +68,7 @@ public function defines(string $role): bool;
6368
* Example: $schema->define(User::class, SchemaInterface::DATABASE);
6469
*
6570
* @param string $role
66-
* @param int $property See ORM constants.
71+
* @param int $property See ORM constants.
6772
* @return mixed
6873
*
6974
* @throws SchemaException

src/Select.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Cycle\ORM\Select\JoinableLoader;
1818
use Cycle\ORM\Select\QueryBuilder;
1919
use Cycle\ORM\Select\RootLoader;
20+
use Cycle\ORM\Select\ScopeInterface;
2021
use IteratorAggregate;
2122
use Spiral\Database\Query\SelectQuery;
2223
use Spiral\Pagination\PaginableInterface;
@@ -114,18 +115,28 @@ public function __clone()
114115
}
115116

116117
/**
117-
* Create new Selector with applied scope. By default no constrain used.
118+
* Create new Selector with applied scope. By default no scope used.
118119
*
119-
* @param ConstrainInterface|null $constrain
120+
* @param ScopeInterface|null $scope
120121
* @return Select
121122
*/
122-
public function constrain(ConstrainInterface $constrain = null): self
123+
public function scope(?ScopeInterface $scope): self
123124
{
124-
$this->loader->setConstrain($constrain);
125+
$this->loader->setScope($scope);
125126

126127
return $this;
127128
}
128129

130+
/**
131+
* @deprecated Will be dropped in next major release. Use {@see scope()} instead.
132+
* @param ConstrainInterface|null $constrain
133+
* @return Select
134+
*/
135+
public function constrain(?ConstrainInterface $constrain = null): self
136+
{
137+
return $this->scope($constrain);
138+
}
139+
129140
/**
130141
* Get Query proxy.
131142
*

src/Select/AbstractLoader.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Cycle\ORM\Schema;
2121
use Cycle\ORM\Select\Traits\AliasTrait;
2222
use Cycle\ORM\Select\Traits\ChainTrait;
23+
use Cycle\ORM\Select\Traits\ScopeTrait;
2324
use Spiral\Database\Query\SelectQuery;
2425

2526
/**
@@ -45,23 +46,24 @@ abstract class AbstractLoader implements LoaderInterface
4546
{
4647
use ChainTrait;
4748
use AliasTrait;
49+
use ScopeTrait;
4850

4951
// Loading methods for data loaders.
50-
public const INLOAD = 1;
51-
public const POSTLOAD = 2;
52-
public const JOIN = 3;
52+
public const INLOAD = 1;
53+
public const POSTLOAD = 2;
54+
public const JOIN = 3;
5355
public const LEFT_JOIN = 4;
5456

55-
/** @var ORMInterface|SourceProviderInterface @internal */
57+
/** @var ORMInterface @internal */
5658
protected $orm;
5759

5860
/** @var string */
5961
protected $target;
6062

6163
/** @var array */
6264
protected $options = [
63-
'load' => false,
64-
'constrain' => true,
65+
'load' => false,
66+
'scope' => true,
6567
];
6668

6769
/** @var LoaderInterface[] */
@@ -75,12 +77,14 @@ abstract class AbstractLoader implements LoaderInterface
7577

7678
/**
7779
* @param ORMInterface $orm
78-
* @param string $target
80+
* @param string $target
7981
*/
8082
public function __construct(ORMInterface $orm, string $target)
8183
{
8284
$this->orm = $orm;
8385
$this->target = $target;
86+
87+
$this->setScope($this->getSource()->getScope());
8488
}
8589

8690
/**
@@ -132,6 +136,8 @@ public function getSource(): SourceInterface
132136
*/
133137
public function withContext(LoaderInterface $parent, array $options = []): LoaderInterface
134138
{
139+
$options = $this->prepareOptions($options);
140+
135141
// check that given options are known
136142
if (!empty($wrong = array_diff(array_keys($options), array_keys($this->options)))) {
137143
throw new LoaderException(
@@ -154,9 +160,9 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade
154160
* Load the relation.
155161
*
156162
* @param string $relation Relation name, or chain of relations separated by.
157-
* @param array $options Loader options (to be applied to last chain element only).
158-
* @param bool $join When set to true loaders will be forced into JOIN mode.
159-
* @param bool $load Load relation data.
163+
* @param array $options Loader options (to be applied to last chain element only).
164+
* @param bool $join When set to true loaders will be forced into JOIN mode.
165+
* @param bool $load Load relation data.
160166
* @return LoaderInterface Must return loader for a requested relation.
161167
*
162168
* @throws LoaderException
@@ -279,7 +285,7 @@ abstract protected function initNode(): AbstractNode;
279285
*/
280286
protected function configureQuery(SelectQuery $query): SelectQuery
281287
{
282-
$query = $this->applyConstrain($query);
288+
$query = $this->applyScope($query);
283289

284290
foreach ($this->join as $loader) {
285291
$query = $loader->configureQuery($query);
@@ -294,12 +300,6 @@ protected function configureQuery(SelectQuery $query): SelectQuery
294300
return $query;
295301
}
296302

297-
/**
298-
* @param SelectQuery $query
299-
* @return SelectQuery
300-
*/
301-
abstract protected function applyConstrain(SelectQuery $query): SelectQuery;
302-
303303
/**
304304
* Define schema option associated with the entity.
305305
*
@@ -325,4 +325,14 @@ protected function getEagerRelations(): \Generator
325325
}
326326
}
327327
}
328+
329+
protected function prepareOptions(array $options): array
330+
{
331+
if (array_key_exists('constrain', $options) && !array_key_exists('scope', $options)) {
332+
$options['scope'] = $options['constrain'];
333+
}
334+
unset($options['constrain']);
335+
336+
return $options;
337+
}
328338
}

src/Select/ConstrainInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
namespace Cycle\ORM\Select;
1313

14-
/**
15-
* Provides the ability to modify the selector and/or entity loader. Can be used to implement multi-table inheritance.
16-
*/
17-
interface ConstrainInterface
18-
{
14+
use function class_alias;
15+
16+
class_alias(ScopeInterface::class, __NAMESPACE__ . '\ConstrainInterface');
17+
18+
if (false) {
1919
/**
20-
* Configure query and loader pair using proxy strategy.
21-
*
22-
* @param QueryBuilder $query
20+
* @deprecated Use {@see ScopeInterface} instead.
2321
*/
24-
public function apply(QueryBuilder $query);
22+
interface ConstrainInterface extends ScopeInterface
23+
{
24+
}
2525
}

0 commit comments

Comments
 (0)