From 0cacf26ed5197e6ff939ecf5964b7659b44f337c Mon Sep 17 00:00:00 2001 From: Juan Cristobal <65052633+juancristobalgd1@users.noreply.github.com> Date: Thu, 25 Jan 2024 00:53:53 +0100 Subject: [PATCH] update file --- src/Fluent.php | 104 ++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 61 deletions(-) diff --git a/src/Fluent.php b/src/Fluent.php index 6a09b08..35120c7 100644 --- a/src/Fluent.php +++ b/src/Fluent.php @@ -9,6 +9,7 @@ use ReflectionException; use InvalidArgumentException; use Illuminate\Support\Collection; +use RuntimeException; /** * Class Fluent @@ -22,8 +23,8 @@ * * The Fluent class provides a fluent interface to facilitate method chaining and flow control * in your PHP applications. and flow control in your PHP applications. You can use this class to perform a variety - * of operations by chaining methods in a concise and readable way. */ - + * of operations by chaining methods in a concise and readable way. + */ class Fluent { private $obj; @@ -32,19 +33,15 @@ class Fluent private $isReturn = false; private $customMethods = []; + /** * Constructor that accepts a class name or object. * * @param mixed $obj The class name or object to work with. * @throws InvalidArgumentException If the argument is not a valid class name or object. */ - public function __construct($data) + public function __construct(string|object|array $data) { - // Validate that $data is a string, an object or an array - if (!is_string($data) && !is_object($data) && !is_array($data)) { - throw new InvalidArgumentException("Invalid argument. Expected class name, object, or array."); - } - $this->createInstance($data); } @@ -54,15 +51,15 @@ public function __construct($data) * @param mixed $data The argument to be used to create the instance. * @return void */ - private function createInstance($data) + private function createInstance(string|object|array $data) { - if (is_string($data) && class_exists($data)) { - $this->obj = new $data(); - } elseif (is_object($data)) { - $this->obj = $data; - } elseif (is_array($data)) { - $this->obj = new Collection($data); - } + $this->obj = match (true) { + is_string($data) && class_exists($data) => new $data(), + is_object($data) => $data, + is_array($data) => new Collection($data), + }; + + $this->result[get_class($this->obj)] = $this->obj; } /** @@ -72,14 +69,11 @@ private function createInstance($data) * @param bool $return Whether to return the created object or $this. * @return $this|object */ - public function new($data, bool $return = true): Fluent + public function new(string|object|array $data, bool $return = true): Fluent { - if (!is_string($data) && !is_object($data) && !is_array($data)) { - throw new InvalidArgumentException("Invalid argument. Expected class name, object, or array."); - } - if ($this->condition && !$this->isReturn) { $this->createInstance($data); + return $return ? $this->obj : $this; } } @@ -101,9 +95,9 @@ public function get(string $key = null) { if ($key !== null && array_key_exists($key, $this->result)) { return $this->result[$key]; - } else { - return end($this->result); } + + return end($this->result); } /** @@ -158,7 +152,7 @@ public function getProperty($name) return $this->obj->$name; } - throw new Exception("The property '$name' does not exist on the object " . get_class($this->obj)); + throw new InvalidArgumentException(sprintf('The property [ %s ] does not exist on the object [ %s ] .', $name, get_class($this->obj))); } /** @@ -175,7 +169,7 @@ public function setProperty($name, $value) return $this; } - throw new Exception("The property '$name' does not exist on the object " . get_class($this->obj)); + throw new InvalidArgumentException(sprintf('The property [ %s ] does not exist on the object [ %s ] .', $name, get_class($this->obj))); } /** @@ -319,7 +313,7 @@ public function reflect($className, ...$constructorArgs) $reflection = new ReflectionClass($className); $this->result['reflect'] = $reflection->newInstanceArgs($constructorArgs); } catch (ReflectionException $e) { - throw new Exception("Error creating instance of $className: " . $e->getMessage()); + throw new InvalidArgumentException(sprintf('Error creating instance of %s: [ %s ] .', $className, $e->getMessage())); } } @@ -341,7 +335,7 @@ public function loop(callable $callback, $iterations) try { $this->result['loop'] = $callback($i); } catch (Exception $e) { - throw new Exception("Error in loop iteration $i: " . $e->getMessage()); + throw new InvalidArgumentException(sprintf('Error in loop iteration %s: [ %s ] .', $i, $e->getMessage())); } } } @@ -398,8 +392,7 @@ public function write($value = null) } /** - * addCustomMethod the Fluent with custom methods. - * + * The Fluent with custom methods. * @param callable $extension A closure that defines the new method. */ public function addCustomMethod($method, $callback) @@ -445,35 +438,12 @@ public function __call($name, $arguments): Fluent if ($this->condition && !$this->isReturn) { $result = null; - // Check if the method exists in the Collection object - if ($this->obj instanceof Collection) { - if (method_exists($this->obj, $name)) { - $result = call_user_func_array([$this->obj, $name], $arguments); - } else { - throw new InvalidArgumentException("El método '$name' no existe en el contexto Collection."); - } - } - - // Check if the method exists in the custom methods - elseif (array_key_exists($name, $this->customMethods)) { - $result = call_user_func_array($this->customMethods[$name], $arguments); - } + $result = match (true) { + $this->obj instanceof Collection, is_callable([$this->obj, $name]) => $this->callMethod($this->obj, $name, $arguments), + array_key_exists($name, $this->customMethods) => call_user_func_array($this->customMethods[$name], $arguments), - // Check if the method exists in the methods of the Class passed as argument - elseif (is_callable([$this->obj, $name])) { - if (method_exists($this->obj, $name)) { - $result = call_user_func_array([$this->obj, $name], $arguments); - } else { - throw new InvalidArgumentException("El método '$name' no existe en el contexto." . get_class($this->obj)); - } - } - - // Check if the method exists in the Fluent methods - elseif (method_exists($this, $name)) { - $result = call_user_func_array([$this, $name], $arguments); - } else { - throw new InvalidArgumentException("El método '$name' no existe en el contexto Fluent."); - } + default => $this->callMethod($this, $name, $arguments), + }; if ($result !== null) { $this->result[$name] = $result; @@ -483,6 +453,18 @@ public function __call($name, $arguments): Fluent return $this; } + /** + * Method to call a method on a service + */ + private function callMethod(Object $obj, string $method, $arguments) + { + if (method_exists($obj, $method)) { + return call_user_func_array([$obj, $method], $arguments); + } + // If the method does not exist, throw an exception + throw new InvalidArgumentException(sprintf('The method [ %s ] does not exist in the Collection context.', $method)); + } + /** * Magic method to get a service by property access. * @@ -494,8 +476,8 @@ public function __get($name) if (property_exists($this->obj, $name)) { return $this->obj->$name; } - - throw new Exception("The property '$name' does not exist on the object " . get_class($this->obj)); + // If the property does not exist, throw an exception + throw new RuntimeException(sprintf('The property [ %s ] does not exist on the object [ %s ]', $name, get_class($this->obj))); } /** @@ -509,8 +491,8 @@ public function __set($name, $value) if (property_exists($this->obj, $name)) { return $this->obj->$name = $value; } - - throw new Exception("The property '$name' does not exist on the object " . get_class($this->obj)); + // If the property does not exist, throw an exception + throw new RuntimeException(sprintf('The property [ %s ] does not exist on the object [ %s ]', $name, get_class($this->obj))); } /**