for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/******************************************************************************
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
*
* Copyright (c) 2016 Richard Klees <[email protected]>
* This software is licensed under The MIT License. You should have received
* a copy of the license along with the code.
*/
namespace Lechimp\Dicto\Graph\Predicate;
use Lechimp\Dicto\Graph\Predicate;
use Lechimp\Dicto\Graph\Entity;
/**
* A predicate that is true if a property of the entity matches a regex.
class _PropertyMatches extends Predicate {
* @var string
protected $name;
protected $regexp;
public function __construct($name, $regexp) {
assert('is_string($name)');
if (!is_string($regexp) || @preg_match("%^$regexp\$%", "") === false) {
throw new \InvalidArgumentException("'%^$regexp\$%' is no valid regex.");
}
$this->name = $name;
$this->regexp = $regexp;
* @inheritdocs
public function _compile() {
$name = $this->name;
$regexp = $this->regexp;
return function(Entity $e) use ($name, $regexp) {
if (!$e->has_property($name)) {
return false;
return preg_match("%^$regexp\$%", $e->property($name)) == 1;
};
public function compile_to_source(array &$custom_closures) {
return
" \$value = \n".
" \$e->has_property(\"$name\")\n".
" && (preg_match(\"%^$regexp\\\$%\", \$e->property(\"$name\")) == 1);\n";
public function for_types(array $existing_types) {
return $existing_types;