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
51 changes: 51 additions & 0 deletions src/Database/PicoDatabasePersistenceExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@
*/
class PicoDatabasePersistenceExtended extends PicoDatabasePersistence
{
/**
* @var array An array of property-value pairs where each entry contains the name of a property and its corresponding value.
*/
private $map = array();

/**
* Sets a property value and adds it to the internal map.
*
* This method sets a value to a property of the associated object and adds the property name and value
* to an internal map for further processing.
*
* @param string $propertyName The name of the property to set.
* @param mixed $propertyValue The value to assign to the property.
* @return self Returns the current instance for method chaining.
*/
public function set($propertyName, $propertyValue)
{
$this->object->set($propertyName, $propertyValue);
$this->addToMap($propertyName, $propertyValue);
return $this;
}

/**
* Adds the property name and value to the internal map.
*
* This method adds the given property name and value as an entry in the internal `$map` array.
*
* @param string $propertyName The name of the property.
* @param mixed $propertyValue The value of the property.
* @return self Returns the current instance for method chaining.
*/
private function addToMap($propertyName, $propertyValue)
{
$this->map[] = array('property' => $propertyName, 'value' => $propertyValue);
return $this;
}

/**
* Magic method to handle undefined methods for setting properties.
*
Expand Down Expand Up @@ -46,6 +83,7 @@ public function __call($method, $params)
$params[0] = null;
}
$this->object->set($var, $params[0]);
$this->addToMap($var, $params[0]);
return $this;
}
}
Expand Down Expand Up @@ -124,4 +162,17 @@ public function selectAll()
}
return $collection;
}

/**
* Convert the object to a JSON string representation for debugging.
*
* This method is intended for debugging purposes only and provides
* a JSON representation of the object's state.
*
* @return string The JSON representation of the object.
*/
public function toString()
{
return json_encode($this->map);
}
}
137 changes: 89 additions & 48 deletions src/Database/PicoSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ public static function getInstance()
return new self;
}

/**
* Creates and returns an instance of the class with an optional PicoPredicate condition.
*
* This static method creates a new instance of the class and, if the provided parameters
* are set, adds a PicoPredicate condition using the given field and value.
*
* @param string|null $field The name of the field to be used in the predicate.
* If null, no predicate is added.
* @param mixed|null $value The value to compare against the field in the predicate.
* If null, no predicate is added.
*
* @return self A new instance of the class with the optionally added predicate.
*/
public static function getInstanceOf($field = null, $value = null)
{
$instance = new self;
if(isset($field))
{
$instance->addAnd(new PicoPredicate($field, $value));
}
return $instance;
}

/**
* Checks if a real join table is required based on the specifications.
*
Expand Down Expand Up @@ -88,12 +111,15 @@ public function add($predicate)
*/
public function addAnd($predicate)
{
if ($predicate instanceof PicoPredicate) {
$this->addFilter($predicate, self::LOGIC_AND);
} elseif ($predicate instanceof PicoSpecification) {
$this->addSubfilter($predicate, self::LOGIC_AND);
} elseif (is_array($predicate) && count($predicate) > 1 && is_string($predicate[0])) {
$this->addFilter(new PicoPredicate($predicate[0], $predicate[1]), self::LOGIC_AND);
if(isset($predicate))
{
if ($predicate instanceof PicoPredicate) {
$this->addFilter($predicate, self::LOGIC_AND);
} elseif ($predicate instanceof PicoSpecification) {
$this->addSubfilter($predicate, self::LOGIC_AND);
} elseif (is_array($predicate) && count($predicate) > 1 && is_string($predicate[0])) {
$this->addFilter(new PicoPredicate($predicate[0], $predicate[1]), self::LOGIC_AND);
}
}
return $this;
}
Expand All @@ -106,12 +132,15 @@ public function addAnd($predicate)
*/
public function addOr($predicate)
{
if ($predicate instanceof PicoPredicate) {
$this->addFilter($predicate, self::LOGIC_OR);
} elseif ($predicate instanceof PicoSpecification) {
$this->addSubfilter($predicate, self::LOGIC_OR);
} elseif (is_array($predicate) && count($predicate) > 1 && is_string($predicate[0])) {
$this->addFilter(new PicoPredicate($predicate[0], $predicate[1]), self::LOGIC_OR);
if(isset($predicate))
{
if ($predicate instanceof PicoPredicate) {
$this->addFilter($predicate, self::LOGIC_OR);
} elseif ($predicate instanceof PicoSpecification) {
$this->addSubfilter($predicate, self::LOGIC_OR);
} elseif (is_array($predicate) && count($predicate) > 1 && is_string($predicate[0])) {
$this->addFilter(new PicoPredicate($predicate[0], $predicate[1]), self::LOGIC_OR);
}
}
return $this;
}
Expand All @@ -125,21 +154,24 @@ public function addOr($predicate)
*/
private function addFilter($predicate, $logic)
{
if ($predicate instanceof PicoPredicate) {
$predicate->setFilterLogic($logic);
$this->specifications[count($this->specifications)] = $predicate;
if ($predicate->isRequireJoin()) {
$this->requireJoin = true;
}
} elseif ($predicate instanceof PicoSpecification) {
$specs = $predicate->getSpecifications();
if (!empty($specs)) {
foreach ($specs as $spec) {
$this->addFilter($spec, $spec->getParentFilterLogic());
if(isset($predicate))
{
if ($predicate instanceof PicoPredicate) {
$predicate->setFilterLogic($logic);
$this->specifications[count($this->specifications)] = $predicate;
if ($predicate->isRequireJoin()) {
$this->requireJoin = true;
}
} elseif ($predicate instanceof PicoSpecification) {
$specs = $predicate->getSpecifications();
if (!empty($specs)) {
foreach ($specs as $spec) {
$this->addFilter($spec, $spec->getParentFilterLogic());
}
}
} elseif (is_array($predicate)) {
$this->addFilterByArray($predicate, $logic);
}
} elseif (is_array($predicate)) {
$this->addFilterByArray($predicate, $logic);
}
return $this;
}
Expand All @@ -153,12 +185,15 @@ private function addFilter($predicate, $logic)
*/
private function addFilterByArray($predicate, $logic)
{
foreach ($predicate as $key => $value) {
$pred = new PicoPredicate($key, $value);
$pred->setFilterLogic($logic);
$this->specifications[count($this->specifications)] = $pred;
if ($pred->isRequireJoin()) {
$this->requireJoin = true;
if(isset($predicate) && is_array($predicate))
{
foreach ($predicate as $key => $value) {
$pred = new PicoPredicate($key, $value);
$pred->setFilterLogic($logic);
$this->specifications[count($this->specifications)] = $pred;
if ($pred->isRequireJoin()) {
$this->requireJoin = true;
}
}
}
return $this;
Expand All @@ -173,7 +208,7 @@ private function addFilterByArray($predicate, $logic)
*/
private function addSubFilter($predicate, $logic)
{
if ($predicate instanceof PicoSpecification) {
if (isset($predicate) && $predicate instanceof PicoSpecification) {
$specification = new self;
$specification->setParentFilterLogic($logic);
$specifications = $predicate->getSpecifications();
Expand Down Expand Up @@ -254,19 +289,22 @@ public function setParentFilterLogic($parentFilterLogic)
private function getWhere($specifications)
{
$arr = array();
foreach ($specifications as $spec) {
if (isset($spec) && $spec instanceof PicoPredicate) {
$entityField = new PicoEntityField($spec->getField());
$field = $entityField->getField();
$parentField = $entityField->getParentField();
$column = ($parentField === null) ? $field : $parentField . "." . $field;
if ($spec->getComparation() !== null) {
$where = $spec->getFilterLogic() . " " . $column . " " . $spec->getComparation()->getComparison() . " " . PicoDatabaseUtil::escapeValue($spec->getValue());
$arr[] = $where;
if(isset($specifications) && is_array($specifications))
{
foreach ($specifications as $spec) {
if (isset($spec) && $spec instanceof PicoPredicate) {
$entityField = new PicoEntityField($spec->getField());
$field = $entityField->getField();
$parentField = $entityField->getParentField();
$column = ($parentField === null) ? $field : $parentField . "." . $field;
if ($spec->getComparation() !== null) {
$where = $spec->getFilterLogic() . " " . $column . " " . $spec->getComparation()->getComparison() . " " . PicoDatabaseUtil::escapeValue($spec->getValue());
$arr[] = $where;
}
} elseif ($spec instanceof PicoSpecification) {
// Nested specification
$arr[] = $spec->getParentFilterLogic() . " (" . $this->createWhereFromSpecification($spec) . ")";
}
} elseif ($spec instanceof PicoSpecification) {
// Nested specification
$arr[] = $spec->getParentFilterLogic() . " (" . $this->createWhereFromSpecification($spec) . ")";
}
}
return $arr;
Expand Down Expand Up @@ -365,10 +403,13 @@ public function __set($field, $value)
*/
private function addPredicate($field, $value)
{
if ($this->defaultLogic === self::LOGIC_OR) {
$this->addOr(new PicoPredicate($field, $value));
} else {
$this->addAnd(new PicoPredicate($field, $value));
if(isset($field))
{
if ($this->defaultLogic === self::LOGIC_OR) {
$this->addOr(new PicoPredicate($field, $value));
} else {
$this->addAnd(new PicoPredicate($field, $value));
}
}
return $this;
}
Expand Down
Loading