Skip to content

Commit 8b83b88

Browse files
ObjectPropertyFinder
1 parent 144b942 commit 8b83b88

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Helper/Object/ObjectPropertyFinder.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,45 @@ public function getValue($key, $defaultValue = null)
124124

125125
return get_object_vars($object)[$key] ?? $defaultValue;
126126
}
127+
128+
129+
/**
130+
* @param null $key
131+
* @param null $defaultValue
132+
*
133+
* @return mixed
134+
*/
135+
public function __invoke($key = null, $defaultValue = null)
136+
{
137+
if (null === $key) {
138+
return $this->getValues();
139+
}
140+
141+
return $this->getValue($key, $defaultValue);
142+
}
143+
144+
145+
/**
146+
* @param array $arr
147+
*
148+
* @return ObjectPropertyFinder
149+
* @throws Exception
150+
*/
151+
public static function createFromArray(array $arr)
152+
{
153+
return new self($arr);
154+
}
155+
156+
157+
/**
158+
* @param object $object
159+
*
160+
* @return ObjectPropertyFinder
161+
* @throws Exception
162+
*/
163+
public static function createFromObject(object $object)
164+
{
165+
return new self($object);
166+
}
167+
127168
}

test/Helper/Object/ObjectPropertyFinderTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ArrayAccess;
1111
use ArrayObject;
1212
use Generator;
13+
use NiceshopsDev\NiceCore\Exception;
1314
use NiceshopsDev\NiceCore\PHPUnit\DefaultTestCase;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516

@@ -319,4 +320,59 @@ public function testGetValues()
319320

320321
$this->assertSame($arrData, $this->object->getValues());
321322
}
323+
324+
325+
/**
326+
* @group unit
327+
* @small
328+
*/
329+
public function testCreateInstanceWithWrongObject()
330+
{
331+
$this->expectException(Exception::class);
332+
$this->expectExceptionMessage("Passed value is not an object or array!");
333+
/** @noinspection PhpParamsInspection */
334+
new ObjectPropertyFinder("foo");
335+
}
336+
337+
338+
/**
339+
* @group integration
340+
* @small
341+
* @uses \NiceshopsDev\NiceCore\Helper\Object\ObjectPropertyFinder::getValues()
342+
* @uses \NiceshopsDev\NiceCore\Helper\Object\ObjectPropertyFinder::getValue()
343+
* @throws Exception
344+
*/
345+
public function testInvokable()
346+
{
347+
$object = ["foo" => "bar", "baz" => null];
348+
349+
$finder = new ObjectPropertyFinder($object);
350+
351+
$this->assertSame($object, $finder());
352+
$this->assertSame("bar", $finder("foo"));
353+
$this->assertSame("bar", $finder("foo", "baz"));
354+
$this->assertSame(null, $finder("baz"));
355+
$this->assertSame("baz", $finder("baz", "baz"));
356+
}
357+
358+
359+
/**
360+
* @group integration
361+
* @small
362+
* @throws Exception
363+
* @uses \NiceshopsDev\NiceCore\Helper\Object\ObjectPropertyFinder::getValues()
364+
*/
365+
public function testStaticFactoryMethods()
366+
{
367+
$arr = ["foo" => "bar", "baz" => null];
368+
$object = new ArrayObject($arr);
369+
370+
$finder = ObjectPropertyFinder::createFromArray($arr);
371+
$this->assertInstanceOf(ObjectPropertyFinder::class, $finder);
372+
$this->assertSame($arr, $finder->getValues());
373+
374+
$finder = ObjectPropertyFinder::createFromObject($object);
375+
$this->assertInstanceOf(ObjectPropertyFinder::class, $finder);
376+
$this->assertSame($arr, $finder->getValues());
377+
}
322378
}

0 commit comments

Comments
 (0)