From ae3973a5d034f6ad3e4ab063bf5f3b7062ef4ab0 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 4 Apr 2025 12:36:06 -0400 Subject: [PATCH] 1st --- BcMath.php | 23 +++++++++++++ Books.php | 26 +++++++++++++++ Coffee.php | 36 +++++++++++++++++++++ Dog.php | 9 ++++++ LazyCollection.php | 26 +++++++++++++++ LazyGhost.php | 20 ++++++++++++ LazyObject.php | 21 ++++++++++++ NullCoalescingAssignment.php | 3 ++ huge.txt | 3 ++ libs/Dollars.php | 21 ++++++++++++ libs/LazyCollection.php | 63 ++++++++++++++++++++++++++++++++++++ libs/LazyObject.php | 26 +++++++++++++++ 12 files changed, 277 insertions(+) create mode 100644 BcMath.php create mode 100644 Books.php create mode 100644 Coffee.php create mode 100644 Dog.php create mode 100644 LazyCollection.php create mode 100644 LazyGhost.php create mode 100644 LazyObject.php create mode 100644 NullCoalescingAssignment.php create mode 100644 huge.txt create mode 100644 libs/Dollars.php create mode 100644 libs/LazyCollection.php create mode 100644 libs/LazyObject.php diff --git a/BcMath.php b/BcMath.php new file mode 100644 index 0000000..334e3cb --- /dev/null +++ b/BcMath.php @@ -0,0 +1,23 @@ +formatAsUSD($c); +echo "\n"; +echo $d->intlUSD($c); diff --git a/Books.php b/Books.php new file mode 100644 index 0000000..bd10338 --- /dev/null +++ b/Books.php @@ -0,0 +1,26 @@ + "Authors: David Smith\n"; + } + + public Author $mainAuthor { + get => new Author("Smith"); + set => throw new Exception("Main author cannot be changed."); + } +} + +$book = new Book(); +echo $book->credits; + +$author = $book->mainAuthor; +echo $author->name; diff --git a/Coffee.php b/Coffee.php new file mode 100644 index 0000000..6baba9f --- /dev/null +++ b/Coffee.php @@ -0,0 +1,36 @@ +value; + } +} + + +class Coffee { + public string $flavor { + get { + return $this->flavor . ' Spice'; + } + set(string $value) { + if (strlen($value) > 16) throw new InvalidArgumentException('Input is too long'); + $this->flavor = $value; + } + } + + public function serve(SizeInterface $size): void { + echo "\r\nServing a {$size->getSize()} coffee.\n"; + } +} + +$coffee = new Coffee(); +$coffee->flavor = 'Pumpkin'; +echo $coffee->flavor; + +$coffee->serve(Size::Grande); diff --git a/Dog.php b/Dog.php new file mode 100644 index 0000000..67a5674 --- /dev/null +++ b/Dog.php @@ -0,0 +1,9 @@ +bark(); +echo $dog; diff --git a/LazyCollection.php b/LazyCollection.php new file mode 100644 index 0000000..1b3222c --- /dev/null +++ b/LazyCollection.php @@ -0,0 +1,26 @@ +each(function ($line) { + echo $line; + ob_flush(); + flush(); +}); +ob_end_flush(); diff --git a/LazyGhost.php b/LazyGhost.php new file mode 100644 index 0000000..15b02b0 --- /dev/null +++ b/LazyGhost.php @@ -0,0 +1,20 @@ +__construct(1); +}; + +$reflector = new ReflectionClass(Example::class); +$object = $reflector->newLazyGhost($initializer); + +var_dump($object); +var_dump($object instanceof Example); + +// Triggers initialization, and fetches the property after that +var_dump($object->data); diff --git a/LazyObject.php b/LazyObject.php new file mode 100644 index 0000000..ce875ac --- /dev/null +++ b/LazyObject.php @@ -0,0 +1,21 @@ +num; + } +} + +class Example2 { + public function hi() { echo "Hi!"; } +} + +$object = new LazyObject()->proxy(MyClass::class, 301); +// The object is initialized only when accessed +echo $object->someMethod() . "\n"; + +$objectB = new LazyObject()->proxy(Example2::class); +$objectB->hi(); diff --git a/NullCoalescingAssignment.php b/NullCoalescingAssignment.php new file mode 100644 index 0000000..0f24057 --- /dev/null +++ b/NullCoalescingAssignment.php @@ -0,0 +1,3 @@ +moneyAsFloat($money), 2, '.', ','); + } + + public function intlUSD(Number $money): string { + if (class_exists('NumberFormatter')) { + $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); + return $formatter->formatCurrency($this->moneyAsFloat($money), 'USD'); + } + } +} diff --git a/libs/LazyCollection.php b/libs/LazyCollection.php new file mode 100644 index 0000000..24291ad --- /dev/null +++ b/libs/LazyCollection.php @@ -0,0 +1,63 @@ +source = $source; + } elseif (is_null($source)) { + $this->source = static::empty(); + } else { + $this->source = $this->getArrayableItems($source); + } + } + + public function getIterator(): Traversable + { + return $this->makeIterator($this->source); + } + + protected function makeIterator($source) + { + if ($source instanceof Closure) { + $source = $source(); + } + + if (is_array($source)) { + return new ArrayIterator($source); + } + + return $source; + } + + public static function make($source = null) + { + return new static($source instanceof Closure ? $source() : $source); + } + + public function each(callable $callback) + { + foreach ($this->getIterator() as $key => $value) { + if ($callback($value, $key) === false) { + break; + } + } + + return $this; + } + + protected function getArrayableItems($items) + { + if (is_array($items)) { + return $items; + } elseif ($items instanceof Traversable) { + return iterator_to_array($items); + } elseif (is_object($items) && method_exists($items, 'toArray')) { + return $items->toArray(); + } + + return (array) $items; + } +} diff --git a/libs/LazyObject.php b/libs/LazyObject.php new file mode 100644 index 0000000..3f50d74 --- /dev/null +++ b/libs/LazyObject.php @@ -0,0 +1,26 @@ +isInstantiable()) { + throw new InvalidArgumentException("Class {$className} cannot be instantiated"); + } + $constructor = $reflector->getConstructor(); + + // If the class has a constructor with required parameters + if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) { + $initializer = static function (mixed $i) use ($className) { + return new $className($i); + }; + return $reflector->newLazyProxy(fn() => $initializer($c)); + } else { + $initializer = static function () use ($className) { + return new $className(); + }; + return $reflector->newLazyProxy($initializer); + } + } +}