/** * {@inheritdoc} */ public function __invoke(SQLTransformer $transformer, Expression $expression, Configuration $config) { $name = $expression->getArg(0); if (!$name->isValue()) { throw new \InvalidArgumentException(sprintf('Field name should be a value')); } return new Clause($config->getNameResolver()->resolve($name->getValue())); }
/** * {@inheritdoc} */ public function __invoke(FilterTransformer $transformer, Expression $expression, $element, Configuration $config) { $name = $expression->getArg(0); if (!$name->isValue()) { throw new \InvalidArgumentException(sprintf('Field name should be a value, expression "%s" given', $name->getExp())); } $name = $config->getNameResolver()->resolve($name->getValue()); return $this->propertyAccessor->getValue($element, $name); }
/** * @param SQLTransformer $transformer * @param Expression $expression * @param Configuration $config * * @return Clause */ public function __invoke(SQLTransformer $transformer, Expression $expression, Configuration $config) { switch ($config->getPlaceholderStyle()) { case self::STYLE_NAMED: $name = 'P' . spl_object_hash($expression); return new Clause(':' . $name, [$name => $expression->getValue()]); } return new Clause('?', [$expression->getValue()]); }
protected function setUp() { $this->transformer = FilterTransformerBuilder::make()->build(); $this->config = Configuration::make()->setNameResolver(new CallbackNameResolver(function ($name) { return "[{$name}]"; })); $this->data = ['john' => ['name' => 'John', 'age' => 11, 'email' => null], 'jane' => ['name' => 'Jane', 'age' => 21, 'email' => '*****@*****.**'], 'joey' => ['name' => 'Joey', 'age' => 31, 'email' => '']]; }
public function find(Expression $expression) { $config = Configuration::make()->setNameResolver(new PrefixedNameResolver('person.')); $clause = $this->transformer->transform($expression, $config); $people = $this->manager->createQueryBuilder()->select('person')->from(Person::class, 'person')->where($clause->getWhere())->setParameters($clause->getParameters())->getQuery()->getResult(); return array_map(function (Person $r) { return $r->getId(); }, $people); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Magento\Component\QueryLanguage\Expression\Transformer\Configuration; use Magento\Component\QueryLanguage\Expression\Transformer\SQLTransformerBuilder; use Magento\Component\QueryLanguage\Expression\Where; // Build or hydrate query $query = Where::all([Where::field('name')->contains('Snow'), Where::field('age')->between(25, 30)]); // Initiate database connection $pdo = new \PDO('sqlite::memory:'); // Load sample data array_map([$pdo, 'exec'], ['CREATE TABLE people (id INT, name VARCHAR(10), age INT);', 'INSERT INTO people VALUES(1, "John Snow", 30);', 'INSERT INTO people VALUES(2, "Ramsay Snow", 31);']); // Create transformer $transformer = SQLTransformerBuilder::make()->build(); // Transform query into SQL clause $clause = $transformer->transform($query, Configuration::make()); // Execute query $stmt = $pdo->prepare('SELECT * FROM people WHERE ' . $clause->getWhere()); $stmt->execute($clause->getParameters()); // Fetch results $people = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($people);
<?php require __DIR__ . '/../vendor/autoload.php'; use Magento\Component\QueryLanguage\Expression\Transformer\Configuration; use Magento\Component\QueryLanguage\Expression\Transformer\Field\CallbackNameResolver; use Magento\Component\QueryLanguage\Expression\Transformer\FilterTransformerBuilder; use Magento\Component\QueryLanguage\Expression\Where; // Build or hydrate query $query = Where::all([Where::field('name')->contains('Snow'), Where::field('age')->between(25, 30)]); // Sample data array $data = [['id' => 1, 'name' => 'John Snow', 'age' => 30], ['id' => 2, 'name' => 'Ramsay Snow', 'age' => 31]]; // Setup transformer $config = Configuration::make()->setNameResolver(new CallbackNameResolver(function ($name) { return '[' . $name . ']'; })); // Create transformer $transformer = FilterTransformerBuilder::make()->build(); // Transform query into filter callable $filter = $transformer->transform($query, $config); // Apply filter to initial set of data $people = array_filter($data, $filter); var_dump($people);
<?php require __DIR__ . '/../vendor/autoload.php'; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Tools\Setup; use Magento\Component\QueryLanguage\Expression\Transformer\Configuration; use Magento\Component\QueryLanguage\Expression\Transformer\Field\MapNameResolver; use Magento\Component\QueryLanguage\Expression\Transformer\SQLTransformerBuilder; use Magento\Component\QueryLanguage\Expression\Where; use Magento\Component\QueryLanguageTests\Entity\Person; // Build or hydrate query $query = Where::all([Where::field('name')->contains('Snow'), Where::field('age')->between(25, 30)]); // Setup doctrine $config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../tests/Entity/'], true, null, null, false); $manager = EntityManager::create(['driver' => 'pdo_sqlite', 'memory' => true], $config); (new SchemaTool($manager))->createSchema($manager->getMetadataFactory()->getAllMetadata()); // Add sample data $manager->persist(new Person(1, 'John Snow', 30)); $manager->persist(new Person(2, 'Ramsay Snow', 31)); $manager->flush(); $manager->clear(); // Setup transformer $config = Configuration::make()->setNameResolver(new MapNameResolver(['name' => 'person.name', 'age' => 'person.age'])); // Create transformer $clause = SQLTransformerBuilder::make()->build()->transform($query, $config); // Build DQL query $people = $manager->createQueryBuilder()->select('person')->from(Person::class, 'person')->where($clause->getWhere())->setParameters($clause->getParameters())->getQuery()->getResult(); var_dump($people);
protected function setUp() { $this->config = Configuration::make()->setNameResolver(new PrefixedNameResolver('phpunit.'))->setPlaceholderStyle(DefaultValueTransformer::STYLE_POSITION); $this->transformer = SQLTransformerBuilder::make()->build(); }
public function testUnsupportedExpressionException() { $this->expectException(\InvalidArgumentException::class); $this->transformer->transform($this->createMock(Expression::class), Configuration::make()); }